I2C address scanner - Scans I2C bus on specified pins and prints all addresses where a active responder is found.
Find address for I2C device
Scans all addreses on a I2C bus and reports those it is able to sucessfully communicate with.
One problem with I2C is that sometimes we receive chips without proper documentation and can not determine what address we should use ot communicate with that chip. This library seeks to identify the chips by attempting to communicate with each address and if it suceeds it assumes there is a chip present.
I have tested this with a clock chip, temperature sensor, OLED device and FRAM chip. It worked for all of them but there is a chance that some chips will not respond without a specific API call so there may be some chips it could not locate.
main.cpp@8:ead6147e31a4, 2016-03-30 (annotated)
- Committer:
- joeata2wh
- Date:
- Wed Mar 30 14:58:05 2016 +0000
- Revision:
- 8:ead6147e31a4
- Parent:
- 7:5962e77516ac
Update to use standard MIT license
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joeata2wh | 0:befb557584fb | 1 | /* Scan I2C bus on specified pins and prints out |
joeata2wh | 6:02a058f2ec6f | 2 | * the all address where an active device responds. |
joeata2wh | 0:befb557584fb | 3 | * |
joeata2wh | 8:ead6147e31a4 | 4 | By Joseph Ellsworth CTO of A2WH |
joeata2wh | 8:ead6147e31a4 | 5 | Take a look at A2WH.com Producing Water from Air using Solar Energy |
joeata2wh | 8:ead6147e31a4 | 6 | March-2016 License: https://developer.mbed.org/handbook/MIT-Licence |
joeata2wh | 8:ead6147e31a4 | 7 | Please contact us http://a2wh.com for help with custom design projects. |
joeata2wh | 8:ead6147e31a4 | 8 | |
joeata2wh | 8:ead6147e31a4 | 9 | |
joeata2wh | 6:02a058f2ec6f | 10 | * Don't forget 3K pull-up resistors on sda,scl |
joeata2wh | 2:e90c47d4f358 | 11 | * |
joeata2wh | 2:e90c47d4f358 | 12 | * I tested this by soldering in a I2C chip known to respond at |
joeata2wh | 2:e90c47d4f358 | 13 | * address dec=120 hex=70 and the utility got the ack as expected. |
joeata2wh | 2:e90c47d4f358 | 14 | * when the chip was de-soldered it was no longer detected. |
joeata2wh | 2:e90c47d4f358 | 15 | */ |
joeata2wh | 0:befb557584fb | 16 | |
joeata2wh | 0:befb557584fb | 17 | #include "mbed.h" |
joeata2wh | 0:befb557584fb | 18 | |
joeata2wh | 0:befb557584fb | 19 | Serial pc(USBTX, USBRX); |
joe ellsworth |
7:5962e77516ac | 20 | Serial pc2(USBTX, USBRX); |
joeata2wh | 0:befb557584fb | 21 | |
joeata2wh | 1:2363995f603f | 22 | #define D_SDA PB_7 |
joeata2wh | 0:befb557584fb | 23 | #define D_SCL PB_6 |
joeata2wh | 0:befb557584fb | 24 | // sda=PB7, scl=PB_6 Pins specific to Nucleo-F303K8 |
joeata2wh | 0:befb557584fb | 25 | // must change pins to match your board. |
joeata2wh | 0:befb557584fb | 26 | |
joeata2wh | 0:befb557584fb | 27 | I2C i2c(D_SDA, D_SCL); |
joeata2wh | 0:befb557584fb | 28 | |
joeata2wh | 0:befb557584fb | 29 | DigitalOut myled(LED1); |
joeata2wh | 0:befb557584fb | 30 | |
joeata2wh | 4:ad1195c10812 | 31 | int ack; |
joeata2wh | 6:02a058f2ec6f | 32 | int address; |
joeata2wh | 0:befb557584fb | 33 | void scanI2C() { |
joeata2wh | 0:befb557584fb | 34 | for(address=1;address<127;address++) { |
joeata2wh | 0:befb557584fb | 35 | ack = i2c.write(address, "11", 1); |
joeata2wh | 0:befb557584fb | 36 | if (ack == 0) { |
joeata2wh | 0:befb557584fb | 37 | pc.printf("\tFound at %3d -- %3x\r\n", address,address); |
joeata2wh | 0:befb557584fb | 38 | } |
joeata2wh | 0:befb557584fb | 39 | wait(0.05); |
joeata2wh | 0:befb557584fb | 40 | } |
joeata2wh | 0:befb557584fb | 41 | } |
joeata2wh | 0:befb557584fb | 42 | |
joeata2wh | 0:befb557584fb | 43 | int main() { |
joeata2wh | 0:befb557584fb | 44 | pc.baud(9600); |
joeata2wh | 0:befb557584fb | 45 | pc.printf("I2C scanner \r\n"); |
joeata2wh | 0:befb557584fb | 46 | scanI2C(); |
joeata2wh | 0:befb557584fb | 47 | pc.printf("Finished Scan\r\n"); |
joeata2wh | 5:a4fddd74263c | 48 | // just blink to let us know the CPU is alive |
joeata2wh | 0:befb557584fb | 49 | while(1) { |
joeata2wh | 0:befb557584fb | 50 | wait(5.0); |
joeata2wh | 0:befb557584fb | 51 | myled = !myled; |
joeata2wh | 0:befb557584fb | 52 | } |
joeata2wh | 0:befb557584fb | 53 | } |
joeata2wh | 0:befb557584fb | 54 |