Esp32/esp32 i2c scanner: Difference between revisions

From Federal Burro of Information
Jump to navigationJump to search
No edit summary
No edit summary
Line 57: Line 57:
   delay(5000);           
   delay(5000);           
}
}
<pre>
</pre>
 
 
[[category:scripts]]

Revision as of 04:01, 20 November 2022


/*********
  Rui Santos
  Complete project details at https://randomnerdtutorials.com  
*********/
#include <Adafruit_NeoPixel.h>
#include <Wire.h>

#define I2C_SDA 0
#define I2C_SCL 1
#define PIN        8 // On Trinket or Gemma, suggest changing this to 1
#define NUMPIXELS 1 // Popular NeoPixel ring size

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  Wire.begin(I2C_SDA, I2C_SCL);
  Serial.begin(115200);
  Serial.println("\nI2C Scanner");
  pixels.begin();
}
 
void loop() {
  pixels.clear();
  pixels.show();
  // pixels.setPixelColor(0, pixels.Color(0, i, 0));
  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.println(address,HEX);
      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);          
}