Arduino I2C Scanner

Below is the code for scanning and finding the I2C address of the device attached to Arduino. It's very helpful when working with I2C devices such as I2C LCD display (some of them use 0x27, other use 0x3F).

// 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()

   Serial.println("\nI2C Scanner");

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


  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.
     error = Wire.endTransmission();

    if (error == 0)
       Serial.print("I2C device found at address 0x");
       if (address<16)
       Serial.println(" !");

     else if (error==4)
       Serial.print("Unknow error at address 0x");
       if (address<16)
   if (nDevices == 0)
     Serial.println("No I2C devices found\n");

  delay(8000);           // wait 8 seconds for next scan

After the code is compiled and uploaded to Arduino. Launch the Serial Monitor to see the result (here it shows my 2004A LCD is using 0x27 as its I2C address).

SainSmart 20 x 4 LCD wanting to use I2C from Arduino Uno

No comments:

Post a Comment