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>

// ---------------- PIN DEFINITIONS ----------------
#define SDA_PIN 21
#define SCL_PIN 22

#define LED_PIN 26
#define BUZZER_PIN 27

// ---------------- I2C ADDRESSES ----------------
#define OLED_ADDRESS 0x3C
#define IMU_ADDRESS  0x68
#define BME_ADDRESS  0x55

// ---------------- OLED DISPLAY ----------------
U8G2_SSD1306_128X64_NONAME_F_HW_I2C display(
  U8G2_R0,
  U8X8_PIN_NONE
);

// ---------------- DEVICE CHECK FUNCTION ----------------
bool deviceFound(byte address) {
  Wire.beginTransmission(address);

  byte error = Wire.endTransmission();

  if (error == 0) {
    return true;
  } 
  else {
    return false;
  }
}

// ---------------- SETUP ----------------
void setup() {

  Serial.begin(115200);

  // Start I2C communication
  Wire.begin(SDA_PIN, SCL_PIN);

  // Configure output pins
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);

  // Initially OFF
  digitalWrite(LED_PIN, LOW);
  digitalWrite(BUZZER_PIN, LOW);

  // Set OLED address
  display.setI2CAddress(OLED_ADDRESS * 2);

  // Start OLED
  display.begin();

  // Starting message on OLED
  display.clearBuffer();
  display.setFont(u8g2_font_6x10_tf);

  display.drawStr(0, 15, "I2C SENSOR MONITOR");
  display.drawStr(0, 32, "Checking Sensors...");
  display.drawStr(0, 49, "Please Wait");

  display.sendBuffer();

  delay(1000);

  // ---------------- CHECK SENSORS ----------------
  bool oledOK = deviceFound(OLED_ADDRESS);
  bool imuOK  = deviceFound(IMU_ADDRESS);
  bool bmeOK  = deviceFound(BME_ADDRESS);

  bool allOK = oledOK && imuOK && bmeOK;

  // ---------------- SERIAL MONITOR ----------------
  Serial.println();
  Serial.println("==============================");
  Serial.println("   I2C SENSOR HEALTH STATUS");
  Serial.println("==============================");

  Serial.print("OLED [0x3C]: ");

  if (oledOK) {
    Serial.println("OK");
  } 
  else {
    Serial.println("SENSOR NOT FOUND");
  }

  Serial.print("IMU [0x68]: ");

  if (imuOK) {
    Serial.println("OK");
  } 
  else {
    Serial.println("SENSOR NOT FOUND");
  }

  Serial.print("BME280 [0x76]: ");

  if (bmeOK) {
    Serial.println("OK");
  } 
  else {
    Serial.println("SENSOR NOT FOUND");
  }

  // ---------------- SYSTEM STATUS ----------------
  if (allOK) {

    Serial.println("SYSTEM STATUS: HEALTHY");

    digitalWrite(LED_PIN, LOW);
    digitalWrite(BUZZER_PIN, LOW);

  } 
  else {

    Serial.println("SYSTEM STATUS: FAULT DETECTED");

    digitalWrite(LED_PIN, HIGH);

    // Buzzer kept OFF for simulator stability
    digitalWrite(BUZZER_PIN, LOW);
  }

  Serial.println("==============================");

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

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

  // OLED status
  display.drawStr(0, 23, "OLED:");

  if (oledOK) {
    display.drawStr(42, 23, "OK");
  } 
  else {
    display.drawStr(42, 23, "FAULT");
  }

  // IMU status
  displa

More projects by maruthavelssiet