I2CScanner.pde: Arduino as I2C bus scanner

One of the challenges of working with I2C (aka “two-wire” or “TWI” or “Wire”) devices is knowing the I2C address of the device. Older devices have a fixed address, or a “choose one-of-four” approach. But newer I2C devices have fully programmable addresses, leading to cases of not knowing what address a device is at.

Fortunately, there’s a technique one can use to “scan” an I2C bus and determine these addresses. Conceptually it’s very similar to a network “ping”. Below is an Arduino sketch “I2CScanner.pde” that turns an Arduino into an I2C bus scanner.

- I2CScanner.pde — Turn Arduino into I2C bus scanner

When loaded up on an Arduino, the sketch will immediately scan the I2C network, showing which addresses are responding.

i2cscanner-out

For example, the above output is from an I2C bus with four slave devices on it (one BlinkM MaxM, three regular BlinkMs).
I2CScanner with BlinkMs
(Notice the 2 pull-up resistors on SDA & SCL. This is needed for longer bus lengths)

One thing to notice about the I2CScanner output is that although there are four devices on the bus, only three addresses were detected. This is because unlike IP networks and “ping”, you can’t tell if two devices have the same address. They’ll both respond to commands sent to them just fine, you just can’t read back data from them.

How it works

In I2C, the first byte transmitted/written by the master to a slave is the address of the slave. If there is a slave at that address, the slave will signal the I2C bus, otherwise it leaves it alone. We can use this to implement a bus scanner.

The Arduino “Wire” library utilizes a set of C functions called “twi.c”. One of those functions is “twi_writeTo()”. This function is used to both send the address of the slave down the bus and also to write data to slaves. It returns 0 if it was able to successfully transmit a byte or non-zero if it couldn’t. Since the very first write to a slave is its address, a very simple bus scanner using it would be:

void scanI2CBus(byte from_addr, byte to_addr) {
  byte data = 0; // not used, just a ptr to feed to twi_writeTo()
  for( byte addr = from_addr; addr < = to_addr; addr++ ) {
    byte rc = twi_writeTo(addr, &data, 0, 1);
    if( rc == 0 ) {
      Serial.printl("device found at address ");
      Serial.println(addr,DEC);
    }
  }
}

In the I2CScanner sketch, this function is extended a bit to support a callback function. The callback function is called with the result of every address scan. In I2CScanner, this callback function is called "scanFunc()" and just prints out "found!" or nothing, but it could be modified to do more complex tasks like doing additional I2C transactions to figure out what kind of device it is, or setting all the devices to a known state, etc.

9 comments to I2CScanner.pde: Arduino as I2C bus scanner

  • Andre

    Is that multidrop cable custom made ? Whats are the details.

  • Hi Andre,
    The cable is made from standard 0.05″ pitch ribbon cable and standard 0.1″ pitch IDC crimp connectors. The cables used in your PC for IDE disk drives are the same (but with more connectors). In this post, I list the exact parts I used:
    http://todbot.com/blog/2008/06/17/get-on-the-blinkm-bus-with-a-blinkm-cylon/

  • that’s awesome! thanks for sharing this..

  • I had to change the end address to 127 to have my DS1307 show up in the scanner. If your device isn’t showing up, you might try increasing the end address accordingly.

  • Hi,

    You noticed the two resistors on the SDA and SCL line, but what are the values?

    Stephen

  • Hi Stephan,
    The normal value one sees for pull-up resistors on an I2C bus is 4.7k. But if you’re running many devices or a long bus cable, you should lower that to 2.2k or even 1.0k.

  • David

    Hi,
    I am trying to use your sketch to confirm the address of a modified Electronic speed control unit. I can control these units ( I have 3 of them) but I would like to be able to confirm addresses of units which have an unknown address. My problem is that I cannot get your sketch to detect the units. Using the known, working units which have addresses of &h52,54 &56 respectively, the sketch runs and no known units are found. I have tried with and without pull up resistors. Any suggestions? I’m afraid my understanding of I2C and Arduino is limited so any pointers would be appreciated.
    Thanks
    David

  • Rob Smith

    Awesome little sketch, just what i needed to get my sensors going!!

  • Thanks Rob. What sensors are you using? I’m interested in all things I2C.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>