🛠 Scanner le bus I2C depuis le microcontrôleur

LattePanda 3 Delta (id: lattepanda-3) · intermediaire · Schéma : IMG-lattepanda-3-intermediaire-2-scanner-i2c.svg — Un capteur I2C connecté aux broches SDA/SCL de l'en-tête Arduino avec résistances de pull-up, l'ATmega32U4 balayant les adresses de 0x08 à 0x77.

Objectives

The Arduino header of the LattePanda 3 Delta exposes an I2C bus (official ADC and I2C tutorials exist in the IO_Playability documentation). Rather than assuming a specific sensor, this tutorial writes a native I2C scanner in C++ on the ATmega32U4: it scans all 7-bit addresses and reports the devices present on the internal serial port. This is an essential diagnostic tool before connecting any sensor, and it once again demonstrates the dual nature of the board: the bus is on the Arduino side, not the PC side.

Hardware

Complete Code

Arduino IDE sketch (board "Arduino Leonardo"):

// Scanner I2C pour l'ATmega32U4 embarqué de la LattePanda 3 Delta.
#include <Wire.h>

void setup {
  Serial.begin(9600);
  while (!Serial) {}               // attendre le port série USB
  Wire.begin;                    // maître I2C
  Serial.println("Scan I2C : 0x08 -> 0x77");
}

void loop {
  byte trouve = 0;
  for (byte addr = 8; addr < 120; addr++) {
    Wire.beginTransmission(addr);
    if (Wire.endTransmission == 0) {   // 0 = ACK reçu
      Serial.print("Périphérique trouvé à 0x");
      Serial.println(addr, HEX);
      trouve++;
    }
  }
  if (trouve == 0) Serial.println("Aucun périphérique I2C.");
  Serial.println("---");
  delay(5000);                     // refaire un scan toutes les 5 s
}

The lines are displayed in the IDE's serial monitor or on the MCU's COM port from the SoC (see the serial bridge tutorial).

Expected Verification

With a module connected, the scan displays its address (for example, address 0x3C for many OLED screens — the actual address depends on the module). With nothing connected but SDA/SCL left floating, many addresses may respond falsely: this is the classic symptom of missing pull-up resistors.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials