#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(115200);
Wire.begin();
// Initialize the LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("I2C Scanner");
Serial.println("\nI2C Scanner");
}
void loop() {
byte error, address;
int devicesFound = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Scanning...");
Serial.println("Scanning for I2C devices...");
for (address = 1; address < 127; address++) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address < 16) Serial.print("0");
Serial.print(address, HEX);
Serial.println("!");
lcd.setCursor(0, 1);
lcd.print("Found: 0x");
if (address < 16) lcd.print("0");
lcd.print(address, HEX);
devicesFound++;
delay(2000); // Display each device for 2 seconds
} else if (error == 4) {
Serial.print("Unknown error at address 0x");
if (address < 16) Serial.print("0");
Serial.println(address, HEX);
}
}
if (devicesFound == 0) {
Serial.println("No I2C devices found.\n");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("No devices");
lcd.setCursor(0, 1);
lcd.print("found.");
} else {
Serial.println("Done.\n");
lcd.setCursor(0, 1);
lcd.print("Scan complete!");
}
delay(5000); // Wait 5 seconds before next scan
}