ESP32 Environmental Monitor — ESP32 simulation

Smart environmental monitor and motor control system with ESP32, LCD, and sensors.

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

Components

Sketch code

// Pin definitions (matching your circuit wiring)
const int RED_LED = 19;
const int GREEN_LED = 18;
const int GAS_PIN = 35;

// Variables
int gasBaseline = 1000;
const int GAS_DANGER_DELTA = 300;
bool badAir = false;

int readGas() {
  return analogRead(GAS_PIN);
}

void setup() {
  Serial.begin(115200);
  pinMode(RED_LED, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
}

void loop() {
  // ===============================
  // READ MQ135 GAS SENSOR
  // ===============================
  int gasRaw = readGas();
  int gasDifference = abs(gasRaw - gasBaseline);

  // ===============================
  // DETECT BAD AIR
  // ===============================
  if (gasDifference >= GAS_DANGER_DELTA) {
    badAir = true;
  } else {
    badAir = false;
  }

  // ===============================
  // RED AND GREEN LED CONTROL
  // ===============================
  if (badAir == true) {
    digitalWrite(RED_LED, HIGH);
    digitalWrite(GREEN_LED, LOW);
  } else {
    digitalWrite(RED_LED, LOW);
    digitalWrite(GREEN_LED, HIGH);
  }

  // ===============================
  // DEBUG OUTPUT
  // ===============================
  Serial.print("Gas Raw: ");
  Serial.print(gasRaw);
  Serial.print(" | Baseline: ");
  Serial.print(gasBaseline);
  Serial.print(" | Difference: ");
  Serial.print(gasDifference);
  Serial.print(" | Air: ");
  Serial.println(badAir ? "BAD" : "GOOD");

  delay(1000);
}

More projects by piyushtamdeokawale2004