Saturday, March 9, 2013

Scanning Arduino's I2C Bus

Prototyping I2C circuits is not one of the easiest tasks for Arduino users. Sometimes we are racking our brains trying to make our sketch works without even realizing if the device is working properly or well connected to the bus line. Another bad situation is when we are not sure about device address.

Versão em português desse artigo: Escaneando o barramento I2C do Arduino.

Trying to see myself free of this problems I have write a tool to tell me all devices that is properly connected to the I2C bus. Take a look in the following source code:

#include <Wire.h>

void setup()
  byte Return;
  Serial.println("Scanning I2C bus...");
  for(byte I2CAddress = 1; I2CAddress <= 127; I2CAddress++)
    if (I2CAddress<16)
    Serial.print(I2CAddress, HEX);
    Serial.print(" (");
    if (I2CAddress<10)
      Serial.print("  ");
    else if (I2CAddress<100)
      Serial.print(" ");
    Serial.print("): ");
    Return = Wire.endTransmission();
    if (Return == 0)
      Serial.print("   ");
    if (I2CAddress % 5)
      Serial.print("    ");

void loop() { }

This code is simple and uses the standard Arduino's Wire library. The only things to do are: copy and paste the code to a new sketch, upload to you Arduino board and finally see what the serial monitor shows to you (don't forget to set the same line speed from source code). Look at the example below:

This example shows two devices connected to the bus:
  • 0x38: It is a smart battery device address (OpenUPS), what the vendor told that was 0x70. I suppose that this difference is due the vendor was considering the TWGCE bit in its master address;
  • 0x68: A DS1307 RTC device.
I hope it be useful! ;)


  1. hi, very nice job.

    1, How to write a code to scan just 3 or 4 specific adress only.

    2, If 1 device is not connected, i need a message on my LCD what device is not connected.

    hope u can help me.


  2. Hi, I like the idea of I2C scanning. I tried that with my openups with no success. It returns many addresses alive, even there is only arduino uno and open ups connected.
    I need to find out proper I2C address of openups and get some data out of it. Could you give me some advice please?
    Thanks, PetrN