I2C address of the device and how to find it

I2C address of the device and how to find it

Daniil Zhuk
For connecting various modules to Arduino, for example, sensors, displays, and ADCs, it is convenient to use an I2C (Inter-Integrated Circuit) bus.

Each connected device has its address, up to 119 devices can be connected to one bus (127 addresses in total, 8 of which are reserved service addresses).

The most common problem with I2C is not knowing the address of the device being connected (although the manufacturer usually indicates them). This is especially noticeable when using cheap Chinese devices from different manufacturers, for example, a character LCD with an I2C adapter (their addresses are usually 0x27 or 0x3F). The solution to this problem we will analyze in the article.

First, we connect the device under test to the Arduino board:
  • VCC to 5v;
  • GND to GND, earth;
  • SDA to SDA or A4 (UNO / Nano) or 20 (Mega) and a 10 kΩ pull-up resistor to the power supply (optional);
  • SCL to SCL or A5 or 21 and a 10 kΩ pull-up resistor to the power supply.

To check the availability of I2C devices connected to the Arduino, you simply need to scan each available address from 8 to 127. Successful transmission of data is a sure sign of the presence of a slave device on the bus. So, we connect the Arduino board to the computer and load the following sketch:

#include <Wire.h>
void setup(){
Wire.begin();
Serial.begin(9600);
while (!Serial);
Serial.println("nI2C Scanner");
}

void loop(){
byte error, address;
int nDevices;

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

nDevices = 0;
for(address = 8; 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 foundn");
else
Serial.println("donen");

delay(5000);
}

Open the port monitor in the Arduino IDE and set the speed to 9600 baud / s. If you find the device on the I2C bus in the port monitor, you will see something like the following message:

If the device fails, you will receive the message:
Unknown error at address ...
In the absence of devices:
No I2C devices found

Add a comment

* Comments must be approved before being displayed.