In this guide we will going to find and detect i2c address on our i2c device. You can scan and find the i2c address used by your devices by find the exact address used by the i2c device for more detail how i2c works please see this link, before we will going to begin you need to have the Arduino IDE and the source code sketch, the code below will scan and find the address which has been used by your i2c device. in this example we will going to used the i2c lcd 16×2 LCD display. upload the source code to your Arduino board and then change the baud rate 115200, and push the reset button then see the result.
Scanned Output
1 2 3 4 |
I2C scanner. Scanning ... Found address: 63 (0x3F) Done. Found 1 device(s). |
Arduino Sketch (i2C Address Scanner)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
/* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 14CORE.com Code Test for I2C Scanner Written by Nick Gammon Date: 20th April 2011 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ */ #include <Wire.h> //Include Wire Code Library void setup() { Serial.begin (115200); // Start Serial Communication at baud rate 115200 while (!Serial) { } Serial.println (); Serial.println ("I2C Address Scanner. Scanning ..."); byte count = 0; Wire.begin(); for (byte i = 8; i < 120; i++) { Wire.beginTransmission (i); if (Wire.endTransmission () == 0) { Serial.print ("Found i2c Device Address: "); Serial.print (i, DEC); Serial.print (" (0x"); Serial.print (i, HEX); Serial.println (")"); count++; delay (1); } // end of good response } // end of for loop Serial.println ("Done."); Serial.print ("Found "); Serial.print (count, DEC); Serial.println (" device(s)."); } // end of setup void loop() {} |
i2C Scanner Sketch Code 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#include <Wire.h> void setup() { Wire.begin(); Serial.begin(9600); Serial.println("14CORE | i2C SCANNER "); Serial.println("========================="); Serial.println("Starting ....") Serial.println("") } void loop() { byte error, address; int devices; Serial.println("Scanning..."); devices = 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(" !"); devices++; } else if (error==4) { Serial.print("Unknow error at address | 0x"); if (address<16) Serial.print("0"); Serial.println(address,HEX); } } if (devices == 0) Serial.println("No I2C devices found\n"); else Serial.println("done\n"); delay(5000); } |
Scanning i2C Device Address on Arduino
I2C scanner,,,,Huh? How did the output make decimal 39 = hex 0x3F
#include
void setup()
{
Wire.begin();
Serial.begin(9600);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println(“\nI2C Scanner”);
}
void loop()
{
byte error, address;
int nDevices;
Serial.println(“Scanning…”);
nDevices = 0;
for(address = 1; address < 127; address++ ) { // The i2c_scanner uses the return value of // the Write.endTransmisstion to see if // a device did acknowledge to the 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("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); // wait 5 seconds for next scan }
It was mistype dec value of 0x3F
This code has an error, i2c address is 7 bit, not a byte.
So you have to multiply by 2 the printed hex and decimal values
Serial.print (2*i, DEC);
Serial.print (” (0x”);
Serial.print (2*i, HEX);
Nop, my mistake, your code is correct, the problem was with my interpretation of the PCF8574 datasheet I was reading.
Although I would change this part, because i have seen devices using address 127:
for (byte i = 0; i < 128; i++)