scanner-i2c.ino

Arduino Nano (Classic, ATmega328P) · File : scanner-i2c.ino · Language : Arduino · Use case : Trouver l'adresse des périphériques I2C branchés

Before weeping over a mute LCD screen, scan the bus: this program polls the Nano 112 I2C addresses (pins A4/A5) and lists whatever answers. It unmasks a poorly documented PCF8574 at 0x3F just like an inverted SDA/SCL cable.

Hardware

An Arduino Nano classic (ATmega328P at 16 MHz, 30-pin DIL breadboard footprint, mini-B USB, 5 V logic, RESET pin at the lower-left corner). Arduino Nano, connected I2C device (LCD, BME280, PCF8574...); 4.7 kΩ pull-ups if the bus is home-made.

Wiring

SDA→A4, SCL→A5, VCC/GND per module. Ready-made modules already carry the pull-ups.

How it works

endTransmission() returns 0 when an ACK comes back from the tested address. Out-of-range addresses (0x00-0x07, 0x78+) are reserved and not tested.

Running it

In the Arduino IDE, select the Arduino Nano board and ATmega328P processor, pick the serial port, then upload. The board's 5 V rail suits 5 V sensors; otherwise use 3.3 V on compatible pins.

Variants

Keep this sketch as a shortcut: every new I2C screen or sensor deserves a scan before any application code.

scanner-i2c.ino

// Scanner le bus I2C (A4=SDA, A5=SCL) : affiche chaque repondant.
// Indispensable quand l'ecran LCD ou le capteur ne repond pas.
#include <Wire.h>

void setup() {
  Serial.begin(9600);
  Wire.begin();
  Serial.println(F("Scan I2C 0x08-0x77..."));
}

void loop() {
  int trouves = 0;
  for (byte adresse = 0x08; adresse <= 0x77; adresse++) {
    Wire.beginTransmission(adresse);
    byte erreur = Wire.endTransmission();
    if (erreur == 0) {
      Serial.print(F("peripherique a 0x"));
      Serial.println(adresse, HEX);
      trouves++;
    }
  }
  if (trouves == 0) {
    Serial.println(F("RIEN : verifier SDA=A4 SCL=A5, alim, pull-up"));
  } else {
    Serial.print(trouves);
    Serial.println(F(" repondant(s)"));
  }
  // une seule passe : arreter ici pour eviter le spam serie
  while (true);
}

← Back to the board