added I2CScanner from https://github.com/todbot/arduino-i2c-scanner/
[Arduino] / I2CScanner / I2CScanner.ino
1 /**
2  * I2CScanner.ino -- I2C bus scanner for Arduino
3  *
4  * 2009,2014, Tod E. Kurt, http://todbot.com/blog/
5  *
6  */
7
8 #include "Wire.h"
9 extern "C" { 
10 #include "utility/twi.h"  // from Wire library, so we can do bus scanning
11 }
12
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) ) 
20 {
21   byte rc;
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);
25     callback( addr, rc );
26   }
27 }
28
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,DEC);
35   Serial.print( (result==0) ? " found!":"       ");
36   Serial.print( (addr%4) ? "\t":"\n");
37 }
38
39
40 byte start_address = 1;
41 byte end_address = 100;
42
43 // standard Arduino setup()
44 void setup()
45 {
46     Wire.begin();
47
48     Serial.begin(19200);
49     Serial.println("\nI2CScanner ready!");
50
51     Serial.print("starting scanning of I2C bus from ");
52     Serial.print(start_address,DEC);
53     Serial.print(" to ");
54     Serial.print(end_address,DEC);
55     Serial.println("...");
56
57     // start the scan, will call "scanFunc()" on result from each address
58     scanI2CBus( start_address, end_address, scanFunc );
59
60     Serial.println("\ndone");
61 }
62
63 // standard Arduino loop()
64 void loop() 
65 {
66     // Nothing to do here, so we'll just blink the built-in LED
67     digitalWrite(13,HIGH);
68     delay(300);
69     digitalWrite(13,LOW);
70     delay(300);
71 }