ESP32 I2C Scanner: Detect I2C Devices & Get Their Address!

Having trouble detecting your I2C devices and finding out their addresses?
In this quick guide, I’ll show you how to properly connect your I2C modules and build an ESP32 I2C Scanner to find out their addresses for this popular communication protocol.

Table of Contents

    What You Will Need

    ESP32 I2C Scanner Sketch (Arduino)

    Run the following sketch on your ESP32 to search for connected I2C devices and print their addresses. The sketch will scan every 15 seconds, so you can connect new devices while it runs.

    #include <Wire.h>
    
    void setup() {
      Wire.begin(); // use Wire.begin(SDA_PIN, SCL_PIN); for custom I2C pins
      Serial.begin(115200);
      Serial.println("\nI2C Scanner Running...");
    }
    
    void loop() {
      byte error, address;
      int devices = 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.println(address, HEX);
          devices++;
        }
      }
    
      if (devices == 0)
        Serial.println("No I2C devices found.\n");
      else
        Serial.println("Scan complete.\n");
    
      delay(15000); // Wait 15 seconds before the next scan
    }

    Watch the serial monitor (115200 baud) for all the I2C devices your ESP32 has discovered. The list will be updated every 15 seconds.

    ESP32 I2C Scanner Sketch Serial Monitor Output

    ESP32 I2C Pins & Wiring I2C Modules

    ESP32 I2C Pins

    By default, the ESP32 uses GPIO pin 22 for the SCL line and GPIO pin 21 for the SDA line. However, the location of these pins on the development board may vary depending on the board you use.

    You can also configure the ESP32 to use different GPIOs. To do so, specify the desired GPIO pins in the sketch as follows:

    Wire.begin(SDA_PIN, SCL_PIN);

    Make sure to connect the SCL pin on the I2C module to the SCL pin of the ESP32. The same thing applies to the SDA pin as well.

    You can connect multiple devices on the same SDA/SCL line. Just make sure there’s no address conflict between devices.

    Common I2C Device Addresses

    Here’s a list of the addresses of some of the most popular I2C devices, so you don’t have to find them out yourself.

    Device TypeExample Device7-bit (HEX) Address
    EEPROMAT24C320x50
    RTCDS32310x68
    Temperature SensorLM750x48
    AccelerometerADXL3450x53
    GyroscopeMPU-60500x68 or 0x69
    OLED DisplaySSD13060x3C or 0x3D
    GPIO ExpanderPCF85740x20 or 0x27
    Light SensorBH17500x23 or 0x5C
    Barometric SensorBMP2800x76 or 0x77
    Digital PotentiometerMCP47250x60

    ESP32 I2C Scanner Troubleshooting Tips

    Follow this troubleshooting guideline in case your ESP32 I2C scanner doesn’t recognize any devices:

    Nothing shows up? Check the wiring and make sure SDA/SCL are connected correctly.

    Still no devices? Try different GPIO pins using “Wire.begin(SDA_PIN, SCL_PIN);“.

    Multiple devices connected? Make sure there’s no address conflict between devices with the same I2C address. Each device on the bus must have a unique address. If two devices share the same address, communication can fail. Refer to your device datasheets to configure different addresses if possible.

    Still stuck? Some I2C modules need additional pull-up resistors on SDA and SCL. Try adding an external pull-up resistor (typically 4.7kΩ to 10kΩ) between the SDA/SCL pins of the I2C device and the 3.3V line.

    Wrapping Up

    With this simple ESP32 I2C scanner, you can now quickly identify and connect I2C devices in your projects. It’s an essential tool for anyone working with I2C sensors or other modules.


    Still having trouble? Let me know in the comments below!

    Thanks for reading!

    Links marked with an asterisk (*) are affiliate links which means we may receive a commission for purchases made through these links at no extra cost to you. Read more on our Affiliate Disclosure Page.

    Share this article

    Leave a Reply

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