PROJECT — ESP32 simulation

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

Components

Sketch code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>

#define DHTPIN 4
#define DHTTYPE DHT22

#define TRIG_PIN 5
#define ECHO_PIN 18

#define PIR_PIN 27
#define GAS_PIN 34

#define SERVO_PIN 19

#define BUZZER_PIN 23

#define RED_LED 25
#define GREEN_LED 26

#define BUTTON_PIN 13

// LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);

// DHT
DHT dht(DHTPIN, DHTTYPE);

// Servo PWM
const int freq = 50;
const int channel = 0;
const int resolution = 16;

// Variables
bool alarmState = false;
float temperature = 0;
int gasValue = 0;
int motion = 0;
float distance = 0;

// Servo Function
void setServo(int angle)
{
  int pulse = map(angle, 0, 180, 500, 2500);
  int duty = (pulse * 65535L) / 20000;
  ledcWrite(channel, duty);
}

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

  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  pinMode(PIR_PIN, INPUT);

  pinMode(BUZZER_PIN, OUTPUT);

  pinMode(RED_LED, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);

  pinMode(BUTTON_PIN, INPUT_PULLUP);

  // Servo PWM
  ledcAttach(SERVO_PIN, freq, resolution);

  setServo(0);

  // LCD
  lcd.init();
  lcd.backlight();

  // DHT
  dht.begin();

  digitalWrite(GREEN_LED, HIGH);
  digitalWrite(RED_LED, LOW);

  lcd.setCursor(0, 0);
  lcd.print("AI WORKER SAFE");

  lcd.setCursor(0, 1);
  lcd.print("SYSTEM READY");

  delay(2000);

  lcd.clear();
}

void loop()
{

  // Read Temperature
  temperature = dht.readTemperature();

  // Read Gas Sensor
  gasValue = analogRead(GAS_PIN);

  // Read PIR
  motion = digitalRead(PIR_PIN);

  // Ultrasonic Trigger
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);

  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);

  digitalWrite(TRIG_PIN, LOW);

  long duration = pulseIn(ECHO_PIN, HIGH);

  distance = duration * 0.0343 / 2;

  bool gasDanger = gasValue > 2000;
  bool tempDanger = temperature > 40;
  bool distanceDanger = (distance <= 20 && motion == HIGH);
    // Display sensor values
  lcd.clear();

  lcd.setCursor(0, 0);
  lcd.print("T:");
  lcd.print(temperature, 1);
  lcd.print(" G:");
  lcd.print(gasValue);

  lcd.setCursor(0, 1);
  lcd.print("D:");
  lcd.print(distance, 0);
  lcd.print("cm");

  delay(300);

  // -------------------------
  // GAS LEAK DETECTED
  // -------------------------
  if (gasDanger)
  {
    alarmState = true;

    digitalWrite(RED_LED, HIGH);
    digitalWrite(GREEN_LED, LOW);

    tone(BUZZER_PIN, 1000);

    setServo(90);

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("!!! WARNING !!!");
    lcd.setCursor(0, 1);
    lcd.print("GAS LEAK");

    Serial.println("Gas Leak Detected");

    delay(1000);
  }

  // -------------------------
  // HIGH TEMPERATURE
  // -------------------------
  else if (tempDanger)
  {
    alarmState = true;

    digitalWrite(RED_LED, HIGH);
    digitalWrite(GREEN_LED, LOW);

    tone(BUZZER_PIN, 1500);

    setServo(90);

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("HIGH TEMP");
    lcd.setCursor(0, 1);
    lcd.print("CHECK AREA");

    Serial.println

More projects by mashiyo272