Tag Archives: i2c

Arduino Wire Scanner sur le bus I2C

Aujourd’hui je publie un code qui n’est pas de moi mais qui est bien pratique.

En effet, ce code permet de scanner tous les éléments connectés au port  I2C de votre Arduino.

Vous noterez que ce code utilise la librairie Wire qui permet de communiquer avec les équipements I2C.

Un grand merci à son auteur pour ce scanner Arduino I2C.

// Wire Scanner
// Demonstrates the use of the Wire library
// Ping all valid adresses on the I²C bus
// and get the answer to see if there is a slave
// Print an 8 x 16 array with the results
// Created by fdufnews 2012 december 14th
// This example code is in the public domain.
#include <Wire.h>
void setup()
  Wire.begin();         // join i2c bus (address optional for master)
  Serial.begin(9600);   // start serial for output
  Serial.println();     // Print a banner
  Serial.println("I2C slave scanner");
  Serial.println("   reserved adress");
  Serial.println(".  no slave detected");
  Serial.println("X  slave detected");
  int result;
  Serial.println("   0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F");
  // Scan only valid addresses (8 to 0x7B)
  // 0-7 and 7C-7F are reserved ones
  unsigned char devices=0;                  // holds how many devices found
  for (unsigned char ad=0; ad<0x7C;ad++){
    if(ad%16==0){                           // If at start of a row
      Serial.print(ad>>4,HEX);              // Display high order bit address
      Serial.print(" ");
    if (ad>7){                              // skip address from 0 to 7
      Wire.beginTransmission(ad);           // start transmission
      result = Wire.endTransmission();      // end transmission and store answer
      if (!result) devices++;               // add a device to the count if ACK
      Serial.print(" ");
      Serial.print(result==0?"X ":". ");    // If ACK there is a guy at this address
      Serial.print("   ");                  // Sorry nobody here
    if(ad%16==15) Serial.println();         // end of the row add a new line
  Serial.print(devices, DEC);
  Serial.print(" device");
  Serial.println(" found on the bus");
void loop()