It is easy to find the I2C address of a device using an I2C Scanner code. To do this, simply connect an I2C master (such as an Arduino or ESP32) to the I2C bus along with the slave device that you want to find the address of. Then, run the I2C Scanner code.
The I2C Scanner code essentially sends requests for each possible address on the I2C bus. If it receives a response from a slave device, then the code identifies that address as the correct one for the connected device.
Code
Here’s an example of an I2C Scanner code that you can upload to your Arduino or ESP32 to find the address of a connected I2C slave device:
#include <Wire.h>
void setup() {
Serial.begin(9600);
Wire.begin();
}
void loop() {
for (byte address = 1; address < 127; address++) {
Wire.beginTransmission(address);
if (Wire.endTransmission() == 0) { // Check if device acknowledges
Serial.print("I2C device found at address 0x");
Serial.println(address, HEX);
}
}
delay(5000); // Wait 5 seconds before next scan
}