Created
July 23, 2016 12:55
-
-
Save geekman/334cd758c747bb344ef44e5b7679fae8 to your computer and use it in GitHub Desktop.
Arduino sketch to scan for I2C addresses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* i2c-detect.ino | |
* just like the Linux utility | |
* | |
* 2016.01.09 darell tan | |
*/ | |
#include <Wire.h> | |
int led_pin = 13; | |
int start_addr = 0; | |
void setup() { | |
Wire.begin(); | |
Serial.begin(9600); | |
Serial.println("begin!"); | |
pinMode(led_pin, OUTPUT); | |
// lets you open up the console | |
delay(2000); | |
} | |
void loop() { | |
// use built-in LED to indicate working status | |
digitalWrite(led_pin, (start_addr % 2) == 0); | |
Wire.beginTransmission(start_addr); | |
Wire.write(0x00); | |
int n = Wire.endTransmission(); | |
if (n == 0) { | |
Serial.print("found addr! 0x"); | |
Serial.print(start_addr, HEX); | |
Serial.println(); | |
} | |
start_addr++; | |
// once start_addr becomes 8-bit, hang here | |
if (start_addr == 0x80) { | |
Serial.println("finished scanning."); | |
start_addr = 0; // reset | |
} | |
delay(50); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment