2 * I2CScanner.ino -- I2C bus scanner for Arduino
4 * 2009,2014, Tod E. Kurt, http://todbot.com/blog/
10 #include "utility/twi.h" // from Wire library, so we can do bus scanning
13 // Scan the I2C bus between addresses from_addr and to_addr.
14 // On each address, call the callback function with the address and result.
15 // If result==0, address was found, otherwise, address wasn't found
16 // (can use result to potentially get other status on the I2C bus, see twi.c)
17 // Assumes Wire.begin() has already been called
18 void scanI2CBus(byte from_addr, byte to_addr,
19 void(*callback)(byte address, byte result) )
22 byte data = 0; // not used, just an address to feed to twi_writeTo()
23 for( byte addr = from_addr; addr <= to_addr; addr++ ) {
24 rc = twi_writeTo(addr, &data, 0, 1, 0);
29 // Called when address is found in scanI2CBus()
30 // Feel free to change this as needed
31 // (like adding I2C comm code to figure out what kind of I2C device is there)
32 void scanFunc( byte addr, byte result ) {
33 Serial.print("addr: ");
34 Serial.print(addr,HEX);
35 Serial.print( (result==0) ? " found!":" ");
36 Serial.print( (addr%4) ? "\t":"\n");
40 byte start_address = 0x01;
41 byte end_address = 0x7F;
43 // standard Arduino setup()
49 Serial.println("\nI2CScanner ready!");
51 Serial.print("starting scanning of I2C bus from ");
52 Serial.print(start_address,HEX);
54 Serial.print(end_address,HEX);
55 Serial.println("...");
57 // start the scan, will call "scanFunc()" on result from each address
58 scanI2CBus( start_address, end_address, scanFunc );
60 Serial.println("\ndone");
63 // standard Arduino loop()
66 // Nothing to do here, so we'll just blink the built-in LED
67 digitalWrite(13,HIGH);