Arduino I2C Bus Scanner

I recently bought a SainSmart IIC/I2C/TWI Serial 2004 20×4 Yellow LCD Modules.  Lovely device, however, the documentation was less than lovely.  Many hours of checking & re-checking wiring and code, I still couldn’t get this to work.

If you read the above link, it says that the I2C address is 0x27.  Some forum browsing suggested that this might not be correct and that the Sainsmart documentation had one or two errors.

One of the suggestions was to use an I2C scanner to find the address of the device.  The code and results are below:

 // i2c_scanner
 //
 // This program (or code that looks like it)
 // can be found in many places.
 // For example on the Arduino.cc forum.
 // The original author is not know.
 //
 // This sketch tests the standard 7-bit addresses
 // from 0 to 127. Devices with higher bit address
 // might not be seen properly.
 //
 // Adapted to be as simple as possible by Arduino.cc user Krodal
 //
 // June 2012
 // Using Arduino 1.0.1
 //

#include <Wire.h>

void setup()
 {
   Wire.begin();

  Serial.begin(115200);
   Serial.println("\nI2C Scanner");
 }

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

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

  nDevices = 0;
   for(address = 0; 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(8000);           // wait 8 seconds for next scan
 }

And the results:

Scanning...
I2C device found at address 0x3F !
done

Not quite the same as 0x27 as stated in the documentation.  Once the correct address was known, getting the display working was plain sailing.  More on that in a later post.

10 thoughts on “Arduino I2C Bus Scanner

  1. They have made the modification of the I2C address on both listing and documentation. Maybe they should thank you.

  2. i2c addresses below 0x8 and above 0x77 are reserved and should not be scanned. Depending on the i2c slave devices on the bus, it can cause things to hang.

    — bill

    • Thanks for the information, Bill. I’ll take a look at that next time I’m looking at that code.

      I shamelessly ‘borrowed’ this code from the Arduino forums. It might be good to update the community there with your suggestions. They have a far larger readership than I do! 😉

  3. Hi there thanks in advance for these great information.I have a MPU-6050 gyro-accelo sensor and I run your program to learn i2c adress of my sensor.But after runnig,I see all registers(from 0x00 to 0x7F) on the screen.I mean
    Scanning…
    I2C device found at address 0x00 !
    I2C device found at address 0x01 !
    .
    .
    I2C device found at address 0x7F !
    What do you think about this problem ? I am sure about connections between my sensor and pad.I am about to go crazy because of this problem.

    • I’ve seen the output you’ve posted many times, I’m afraid. In every case it’s been because I made a mistake in my wiring.

Leave a Reply

Your email address will not be published. Required fields are marked *