-->
当前位置:首页 > DayDayUp > 正文内容

esp32 搜索I2C地址

Luz9个月前 (10-26)DayDayUp1331
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define SDA_PIN 21
#define SCL_PIN 22
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {
  Serial.begin(115200);
  delay(3000);
  /*while(!display.begin(0x3C,SDA_PIN, SCL_PIN)) {
    Serial.println(F("SSD1306 allocation failed"));
    delay(1000);
  }
  Serial.println(F("11111"));
  display.display(); // 清除屏幕缓冲
  delay(2000);      // 稍等一下
  display.clearDisplay();
  */
}
/*
void loop() {
  display.setTextSize(10);
  Serial.println(F("22222"));
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(20,20);
  display.drawRect(10, 10, 50, 30, SSD1306_BLACK);
  display.println(F("Hello, World!"));
  display.display();
  delay(1000);
  display.clearDisplay();
}
*/



void loop(){
  byte error, address;
  int nDevices;
  Serial.println("Scanning...");
  nDevices = 0;
  for (address = 1; address < 127; address++ ){
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the 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
}

image.png

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。