I2C Scanner

This is a basic example which will display via the serial monitor the address of any devices connected to your Wemos.

Its the same as one used on Arduino’s.

Code

[codesyntax lang=”cpp”]

#include <Wire.h>


void setup()
{
  Wire.begin();

  Serial.begin(9600);
  Serial.println("\nI2C Scanner");
}


void loop()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  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("  !");

      nDevices++;
    }
    else if (error==4) 
    {
      Serial.print("Unknow error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}

[/codesyntax]

4 COMMENTS

  1. For some reason, if I want it to work on my ESP8266 ESP-01 I have to replace

    Wire.begin();
    With:
    Wire.begin(0,2);

    Hope it helps someone 😉

    • Wire.begin() assumes SDA and SCL are certain pins, but you can set them to any other pin by calling Wire.begin([SDA], [SCL]). Which is what you have done.

  2. […] Nach dem alles soweit verkabelt ist sollte der Sensor funktionieren. Da ich mir immer nicht sicher war ob auch wirklich alle I2C Geräte funktionieren habe ich im Internet nach einen I2C Scanner für den ESP8266 NodeMCU gesucht und auch ein entsprechendes Programm gefunden. Das Programm ist auf der sehr guten Seiten http://www.esp8266learning.com unter dem folgenden Link zu finden. http://www.esp8266learning.com/i2c-scanner.php […]

LEAVE A REPLY

Please enter your comment!
Please enter your name here