scanner-i2c.ino
Before writing a driver, check that the module (sensor, display, expander) answers on the bus. Wire sweeps 127 addresses and lists the ones that ACK — the reflex move when a device « does not work ».
Hardware required
An Arduino Mega ADK Rev3 (ATmega2560 + MAX3421E USB host chip with a Type-A port; digital pin 7 is reserved for the MAX3421E reset). 1 or more I2C modules (e.g. PCF8574 LCD, MPU6050, BME280).
Wiring
SDA to pin SDA (20), SCL to SCL (21), VCC 5 V (or 3.3 V) and GND; pull-up resistors are usually already fitted on modules.
How it works
beginTransmission/endTransmission returns 0 when a chip pulls SDA low (ACK): the address prints in hex. Select « Arduino Mega ADK » in the IDE. For USB host/ADK work, install the USB Host Shield 2.0 library; do not use D7. The USB-A port powers the accessory at 5 V.
Going further
Note the found address and try the vendor library with it; 0x27 (LCD) and 0x3C (OLED) are the classics.
scanner-i2c.ino
// Scanner de bus I2C (SDA=SDA (20), SCL=SCL (21)) avec la bibliotheque Wire.
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(9600);
while (!Serial);
Serial.println(F("Scanner I2C — 8 adresses par ligne"));
}
void loop() {
int trouve = 0;
for (byte addr = 1; addr < 127; addr++) {
Wire.beginTransmission(addr);
if (Wire.endTransmission() == 0) {
Serial.print(F("Peripherique a 0x"));
Serial.println(addr, HEX);
trouve++;
}
}
Serial.print(trouve);
Serial.println(F(" peripherique(s) — nouvelle passe dans 5 s"));
delay(5000);
}