Learn how to scan I2C slaves using an Arduino with this step-by-step guide. Use the Wire library to easily communicate with I2C devices, and scan the bus to detect connected devices. Find the addresses of all detected I2C slaves and troubleshoot any connection issues with this comprehensive I2C scanner tutorial.

Disclaimer:

Advertisements

What is I2C:

I2C stands for the inter-integrated controller. This is a serial communication protocol that can connect low-speed devices. It is a master-slave communication in which we can connect and control multiple slaves from a single master. In this, each slave device has a specific address.

In I2C communication, there are two types of devices: the master and the slave. The master initiates the communication, while the slave responds to the master’s requests. Multiple slaves can be connected to a single I2C bus, and each slave has a unique address that the master can use to address it individually.

The I2C protocol is often used in embedded systems, microcontrollers, and other electronic applications where low-speed communication between devices is required. It is also used in many consumer electronics devices such as TVs, DVD players, and smartphones for communication between internal components.

Scanning I2C devices:

Sometimes, we need to scan the connected I2C devices such as sensors or other modules. Sometimes we know the address but sometimes, we really need to scan the address because it doesn’t work.

To scan I2C slaves using an Arduino, you can use the Wire library, which provides functions to communicate with I2C devices. Here are the steps to scan for I2C slaves using an Arduino:

  1. Connect your Arduino to the I2C bus by connecting the SDA and SCL pins of the Arduino to the corresponding pins on the I2C bus.
  2. Open the Arduino IDE and create a new sketch.
  3. Include the Wire library at the top of the sketch by adding the following line of code:#include <Wire.h>
  4. In the setup() function, initialize the Wire library by adding the following line of code:Wire.begin();
  5. In the loop() function, use the Wire library to scan the I2C bus for connected devices. Here’s the code to scan the I2C bus:
void loop() {
    byte error, address;
    int nDevices;

    Serial.println("Scanning...");

    nDevices = 0;
    for(address = 1; address < 127; address++ ) {
        Wire.beginTransmission(address);
        error = Wire.endTransmission();

        if (error == 0) {
            Serial.print("I2C device found at address 0x");
            if (address<16) {
                Serial.print("0");
            }
            Serial.print(address,HEX);
            Serial.println("  !");

            nDevices++;
        }
        else if (error==4) {
            Serial.print("Unknown error at address 0x");
            if (address<16) {
                Serial.print("0");
            }
            Serial.println(address,HEX);
        }    
    }
    if (nDevices == 0) {
        Serial.println("No I2C devices found\n");
    }
    else {
        Serial.println("done\n");
    }
    delay(5000);           // wait 5 seconds for next scan
}
  1. Upload the sketch to the Arduino and open the Serial Monitor to view the output. The sketch will scan the I2C bus and print out the addresses of all detected I2C slaves.

Note that the I2C addresses are shown in hexadecimal format (0xXX). If an I2C slave is not detected, it may not be connected properly, or it may have a different address than the one the scanner is looking for. Also, make sure that the correct value of pull-up resistors is used in the I2C bus.

Conclusion:

If everything is ok, you’ll get a result like this:

So you can scan your connected sensors or modules. See you soon, Happy coding, Thanks.

Read more on:

For Professional Designs or Help:

Loading

MKDas

Mithun K. Das; B. Sc. in EEE from KUET; Head of R&D @ M's Lab Engineering Solution. "This is my personal blog. I post articles on different subjects related to electronics in the easiest way so that everything becomes easy for all, especially for beginners. If you have any questions, feel free to ask through the contact us page." Thanks.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *