ESP32: I2C Scanner (Arduino IDE) – Finding the Address of I2C Devices

This is a quick guide that shows how to find the address of I2C devices with the ESP32 programmed using Arduino IDE.

ESP32 I2C Scanner Arduino IDE Finding the Address of I2C Devices

Want to learn more about I2C with the ESP32? Check this tutorial: ESP32 I2C Communication: Set Pins, Multiple Bus Interfaces and Peripherals (Arduino IDE).

ESP32 – Default I2C Pins

When using the ESP32 with Arduino IDE, the default I2C pins are:

  • GPIO 22 (SCL)
  • GPIO 21 (SDA)

You can configure your code to use any other pins. You can – check this tutorial to learn how to use different I2C pins.

I2C Scanner Sketch – Arduino IDE

If you want to find the I2C address of a specific sensor, display, or any other I2C peripheral, connect it to the ESP32 I2C pins and then run the I2C scanner sketch provided.

Copy the following code to the Arduino IDE and upload it to the ESP32 board.

/*********
  Rui Santos
  Complete project details at https://randomnerdtutorials.com  
*********/

#include <Wire.h>
 
void setup() {
  Wire.begin();
  Serial.begin(115200);
  Serial.println("\nI2C Scanner");
}
 
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.println(address,HEX);
      nDevices++;
    }
    else if (error==4) {
      Serial.print("Unknow 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);          
}

View raw code

After uploading the code, make sure you have your I2C peripheral properly connected to your board on the right I2C pins (SCL=GPIO22 ; SDA =GPIO21).

If you’re using different pins, make sure you adjust that on the code. Replace:

Wire.begin();

With the following line, in which I2C_SDA corresponds to the GPIO number you’re using as SDA and I2C_SCL to the GPIO number you’re using as SCL.

Wire.begin(I2C_SDA, I2C_SCL);

Open the Serial Monitor at a baud rate of 115200.

Open Arduino IDE Serial Monitor baud rate of 115200

Wait a couple of seconds, and the I2C address of your peripheral will be printed in the Serial Monitor. If you have more than one device connected to the same I2C bus, it will display the address of all devices.

ESP32 I2C Scanner Arduino IDE Serial Monitor

Wrapping Up

In this guide, you learned how to quickly find the address of I2C devices. You just need to connect your I2C peripheral to the ESP32 board and upload the I2C scanner sketch provided.

We hope you’ve found this guide useful. To learn more about I2C with the ESP32, we recommend reading the following article: ESP32 I2C Communication: Set Pins, Multiple Bus Interfaces and Peripherals (Arduino IDE).

Learn more about the ESP32 with our resources:

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

1 thought on “ESP32: I2C Scanner (Arduino IDE) – Finding the Address of I2C Devices”

  1. Hi Rui,

    This was super helpful to help me diagnose my waveshare BME280 which the instructiosn stated it was 0x77 at 5 volts and 0x76 at 3 volts but it actually is addressed at 0x77 at both voltages

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.