I²C Scanner test — ESP32 simulation

Maruthavel T

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by maruthavelssiet.

Components

Sketch code

#include <Wire.h>
#include <U8g2lib.h>

#define SDA_PIN 21
#define SCL_PIN 22

#define LED_PIN 26
#define BUZZER_PIN 27

byte sensors[] = {0x3C, 0x68, 0x55};
const char* names[] = {"OLED", "IMU", "BME280"};

// Grove OLED 128x64 I2C
U8G2_SSD1306_128X64_NONAME_F_HW_I2C
display(U8G2_R0, U8X8_PIN_NONE);

void setup() {
  Serial.begin(115200);

  Wire.begin(SDA_PIN, SCL_PIN);

  pinMode(LED_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);

  display.begin();

  display.clearBuffer();
  display.setFont(u8g2_font_6x10_tf);
  display.drawStr(0, 12, "I2C HEALTH MONITOR");
  display.drawStr(0, 28, "System Starting...");
  display.sendBuffer();

  delay(1500);
}

bool deviceFound(byte address) {
  Wire.beginTransmission(address);
  return Wire.endTransmission() == 0;
}

void loop() {
  bool allOK = true;
  bool oledOK = deviceFound(0x3C);
  bool imuOK = deviceFound(0x68);
  bool bmeOK = deviceFound(0x76);

  if (!oledOK || !imuOK || !bmeOK) {
    allOK = false;
  }

  // Serial Monitor output
  Serial.println();
  Serial.println("--- Sensor Health Status ---");

  Serial.print("OLED [0x3C]: ");
  Serial.println(oledOK ? "OK" : "SENSOR NOT FOUND");

  Serial.print("IMU [0x68]: ");
  Serial.println(imuOK ? "OK" : "SENSOR NOT FOUND");

  Serial.print("BME280 [0x76]: ");
  Serial.println(bmeOK ? "OK" : "SENSOR NOT FOUND");

  Serial.print("SYSTEM STATUS: ");
  Serial.println(allOK ? "HEALTHY" : "FAULT DETECTED");

  // LED and buzzer alert
  digitalWrite(LED_PIN, allOK ? LOW : HIGH);
  digitalWrite(BUZZER_PIN, allOK ? LOW : HIGH);

  // OLED display
  display.clearBuffer();
  display.setFont(u8g2_font_6x10_tf);

  display.drawStr(0, 10, "SENSOR HEALTH STATUS");

  display.drawStr(0, 24, "OLED:");
  display.drawStr(42, 24, oledOK ? "OK" : "FAULT");

  display.drawStr(0, 36, "IMU:");
  display.drawStr(42, 36, imuOK ? "OK" : "FAULT");

  display.drawStr(0, 48, "BME280:");
  display.drawStr(42, 48, bmeOK ? "OK" : "FAULT");

  display.drawStr(0, 62, allOK ? "SYSTEM: HEALTHY" : "SYSTEM: FAULT");

  display.sendBuffer();

  delay(2000);
}

More projects by maruthavelssiet