How to know I2C address of sensor/device
How to know I2C address of sensor/device?
Answer
Datasheet/manual usually specify the I2C address. You can find the the I2C address on datasheet/manual.
However, there is a faster way, use I2C Scanner by doing the following step:
- Connect I2C sensor/device to Arduino (SCL, SDA, VCC, GND pins)
- Connect Arduino to PC via USB cable
- Open Arduino IDE, select the right board and port
- Copy the below I2C Scanner code and open with Arduino IDE
// I2C address scanner program
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(9600);
Serial.println("I2C 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.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 found");
else
Serial.println("done");
delay(5000); // wait 5 seconds for next scan
}
- Click Upload button on Arduino IDE to upload code to Arduino
- Open Serial Monitor
- See the result on Serial Monitor:
COM6
Scanning...
I2C device found at address 0x3F !
done
Scanning...
I2C device found at address 0x3F !
done
Autoscroll
Clear output
9600 baud
Newline
Buy Arduino
1 × Arduino UNO Buy on Amazon | |
1 × USB 2.0 cable type A/B Buy on Amazon |
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.