IOT — ESP32 simulation

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

Components

Sketch code

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// ================= PIN DEFINITIONS =================

#define LED_PIN     26
#define BUTTON_PIN  14
#define PIR_PIN     27
#define POT_PIN     34
#define BUZZER_PIN  25

#define OLED_SDA    21
#define OLED_SCL    22

// ================= OLED =================

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(
  SCREEN_WIDTH,
  SCREEN_HEIGHT,
  &Wire,
  -1
);

// ================= ALARM VARIABLES =================

bool alarmActive = false;
bool alarmReset = false;

unsigned long previousBeepTime = 0;

bool beepState = false;

// Beep timing
const unsigned long BEEP_ON_TIME = 500;   // 0.5 second
const unsigned long BEEP_OFF_TIME = 1000; // 1 second


// ================= BUZZER =================

void buzzerON() {
  ledcWriteTone(BUZZER_PIN, 2000);
}

void buzzerOFF() {
  ledcWriteTone(BUZZER_PIN, 0);
}


// ================= SETUP =================

void setup() {

  Serial.begin(115200);

  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(PIR_PIN, INPUT);

  digitalWrite(LED_PIN, LOW);

  // Buzzer setup
  ledcAttach(BUZZER_PIN, 2000, 8);
  buzzerOFF();

  // OLED
  Wire.begin(OLED_SDA, OLED_SCL);

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {

    Serial.println("OLED NOT FOUND!");

    while (1);
  }

  // Startup screen
  display.clearDisplay();

  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(2);

  display.setCursor(10, 5);
  display.println("SYSTEM");

  display.setCursor(25, 30);
  display.println("READY");

  display.display();

  delay(2000);
}


// ================= LOOP =================

void loop() {

  int motion = digitalRead(PIR_PIN);
  int button = digitalRead(BUTTON_PIN);
  int potValue = analogRead(POT_PIN);


  // =================================================
  // BUTTON PRESSED
  // =================================================

  if (button == LOW) {

    alarmActive = false;
    alarmReset = true;

    digitalWrite(LED_PIN, LOW);
    buzzerOFF();

    beepState = false;

    Serial.println("ALARM STOPPED");

    delay(300);
  }


  // =================================================
  // WAIT FOR PIR CLEAR
  // =================================================

  if (alarmReset == true) {

    if (motion == LOW) {

      alarmReset = false;

      Serial.println("SYSTEM READY");
    }
  }


  // =================================================
  // MOTION DETECTION
  // =================================================

  if (motion == HIGH && alarmReset == false) {

    alarmActive = true;

    Serial.println("MOTION DETECTED");
  }


  // =================================================
  // ALARM OUTPUT
  // =================================================

  if (alarmActive == true) {

    // LED stays ON
    digitalWrite(LED_PIN, HIGH);

    // Slow beep pattern
    unsigned long currentTime = millis();

    if (beepState == false) {

      // Currently silent