Skip to content

Instantly share code, notes, and snippets.

@MarcoCiau
Created September 20, 2023 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarcoCiau/db58b59a4cae2a464f2e1bca09f7a731 to your computer and use it in GitHub Desktop.
Save MarcoCiau/db58b59a4cae2a464f2e1bca09f7a731 to your computer and use it in GitHub Desktop.
Universal Arduino I2C Finder
#include <Arduino.h>
#include <Wire.h>
void setup() {
Serial.begin(9600);
Wire.begin();
}
void loop() {
byte error, address;
int nDevices = 0;
delay(5000);
Serial.println("Scanning for I2C devices ...");
for(address = 0x01; address < 0x7f; address++){
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0){
Serial.print("I2C device found at address: ");
Serial.println(address, HEX);
nDevices++;
} else if(error != 2){
Serial.print("Error: ");
Serial.print(error);
Serial.print("at Address: ");
Serial.println(address, HEX);
}
}
if (nDevices == 0){
Serial.println("No I2C devices found");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment