Advertisement
Guest User

I2C Scanner for Heltec ESP32 V2

a guest
May 1st, 2021
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>  //include Wire.h library
  2. #define SCL_PIN 15
  3. #define SDA_PIN 4
  4. void setup() {
  5.     Wire.begin(SDA_PIN, SCL_PIN);  // doesnt work
  6.     //Wire.begin();  // works for SDA:21, SCL:22, but cant read Batterycharge
  7.     Serial.begin(115200);          
  8.     while (!Serial)
  9.         ;  // Waiting for Serial Monitor
  10.     delay(750);
  11.     Serial.println("\nI2C Scanner");
  12. }
  13.  
  14. void loop() {
  15.     byte error, address;  // variable for error and I2C address
  16.     int nDevices;
  17.  
  18.     Serial.println("Scanning...");
  19.  
  20.     nDevices = 0;
  21.     for (address = 2; address < 127; address++) {
  22.         // The i2c_scanner uses the return value of
  23.         // the Write.endTransmisstion to see if
  24.         // a device did acknowledge to the address.
  25.         Wire.beginTransmission(address);
  26.         error = Wire.endTransmission();
  27.  
  28.  
  29.         if (error == 0) {
  30.             Serial.print("I2C device found at address 0x");
  31.             if (address < 16) Serial.print("0");
  32.             Serial.print(address, HEX);
  33.             Serial.println("  !");
  34.             nDevices++;
  35.         } else if (error == 4) {
  36.             Serial.print("Unknown error at address 0x");
  37.             if (address < 16) Serial.print("0");
  38.             Serial.println(address, HEX);
  39.         }
  40.     }
  41.     if (nDevices == 0)
  42.         Serial.println("No I2C devices found\n");
  43.     else
  44.         Serial.println("done\n");
  45.  
  46.     delay(5000);  // wait 5 seconds for the next I2C scan
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement