Detect I2C Devices with ESP-01


 

This example scans the I2C bus and prints the addresses of detected devices to the serial monitor.

 

 

#include <Wire.h>

void setup() {
  Wire.begin(0, 2);
  Serial.begin(115200);
}

void loop() {
  scanI2CDevices();
  delay(5000);  
}

void scanI2CDevices() {
  for (byte address = 1; address < 127; ++address) {
    Wire.beginTransmission(address);
    if (Wire.endTransmission() == 0) 
      Serial.println("Device found at 0x" + String(address < 16 ? "0" : "") + String(address, HEX));
  }
}



The ESP-01 has no dedicated I2C pins, so we use GPIO0 for SDA and GPIO2 for SCL.
 

ESP-01 I2C address detecting circuit


ESP-01