Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

ESP32-Based Vibration and Smoke Detection System with Web Control and Safety Indicators

Image of ESP32-Based Vibration and Smoke Detection System with Web Control and Safety Indicators

Circuit Documentation

Summary

This circuit is designed to monitor environmental conditions and control devices accordingly. It includes a vibration sensor (SW-420) and a smoke sensor (MQ-2) for detection purposes. The circuit uses an ESP32 microcontroller to process sensor data and control an exhaust fan and a DC motor via solid-state relays (SSRs). The ESP32 also hosts a web server to allow remote monitoring and control of the fan and motor. LED indicators provide a visual status of the system, with different colors representing different levels of alert.

Component List

  • SW-420 Vibration Sensor: Detects vibrations and outputs a digital signal.
  • MQ-2 Gas Sensor: Senses smoke and hazardous gases and provides both analog and digital outputs.
  • CCMHC 10A DC Motor Speed Controller (PWM): Controls the speed of a DC motor using PWM signals.
  • POWER SUPPLY 12V 5AMP: Converts 220V AC to 12V-24V DC to power the circuit.
  • Ac Supply: Provides the 220V AC input for the power supply.
  • DC Motor: Operated by the motor speed controller.
  • Exhaust Fan 12": Ventilates the area when activated by the SSR.
  • LEDs (Red, Green, Yellow): Indicate the status of the system.
  • Resistor (200 Ohms): Limits current to the LEDs.
  • SSR-40A & SSR-25A: Solid-state relays used to switch the exhaust fan and DC motor.
  • LM2596 Step Down Module: Steps down voltage to a lower level for the ESP32 and sensors.
  • ESP32 (30 pin): Microcontroller that processes sensor data, controls devices, and hosts a web server.

Wiring Details

SW-420 Vibration Sensor

  • VCC: Connected to the output voltage of the LM2596 Step Down Module.
  • Ground: Connected to the ground net of the circuit.
  • Digital output: Connected to GPIO D19 of the ESP32.

MQ-2 Gas Sensor

  • VCC: Connected to the output voltage of the LM2596 Step Down Module.
  • GND: Connected to the ground net of the circuit.
  • A0: Connected to GPIO D4 of the ESP32.

CCMHC 10A DC Motor Speed Controller (PWM)

  • Power+: Connected to the output voltage of the POWER SUPPLY 12V 5AMP.
  • Power-: Connected to the ground net of the circuit.
  • Motor+: Connected to SSR-40A Lin pins.
  • Motor-: Connected to SSR-40A Lin pins.

POWER SUPPLY 12V 5AMP

  • 220V Positive Pole (AC): Connected to the +ve of the Ac Supply.
  • 220V Negative Pole (AC): Connected to the -ve of the Ac Supply.
  • GND (DC): Connected to the ground net of the circuit.
  • 12V-24V Output (DC): Connected to the input voltage of the LM2596 Step Down Module.

Ac Supply

  • +ve: Connected to the 220V Positive Pole (AC) of the POWER SUPPLY 12V 5AMP.
  • -ve: Connected to the 220V Negative Pole (AC) of the POWER SUPPLY 12V 5AMP.

DC Motor

  • Pin 1: Connected to SSR-40A Lout pins.
  • Pin 2: Connected to SSR-40A Lout pins.

Exhaust Fan 12"

  • Live: Connected to SSR-25A 2-out pin.
  • Neutral: Connected to the -ve of the Ac Supply.

LEDs (Red, Green, Yellow)

  • Cathode (Common): Connected to the ground net of the circuit through a 200 Ohm resistor.
  • Anode (Red): Connected to GPIO D14 of the ESP32.
  • Anode (Green): Connected to GPIO D26 of the ESP32.
  • Anode (Yellow): Connected to GPIO D27 of the ESP32.

Resistor (200 Ohms)

  • Pin1: Connected to the ground net of the circuit.
  • Pin2: Connected to the cathodes of the LEDs.

SSR-40A & SSR-25A

  • -: Connected to the ground net of the circuit.
  • +: Connected to GPIOs D25, D33 of the ESP32 for SSR-40A and to the 220V Positive Pole (AC) of the POWER SUPPLY 12V 5AMP for SSR-25A.
  • Lin (SSR-40A): Connected to the Motor+ and Motor- pins of the CCMHC 10A DC Motor Speed Controller (PWM).
  • Lout (SSR-40A): Connected to the pins of the DC Motor.
  • 1-in (SSR-25A): Connected to the 220V Positive Pole (AC) of the POWER SUPPLY 12V 5AMP.
  • 2-out (SSR-25A): Connected to the Live pin of the Exhaust Fan 12".

LM2596 Step Down Module

  • OUT+: Connected to the VCC of the ESP32, SW-420 Vibration Sensor, and MQ-2 Gas Sensor.
  • OUT-: Connected to the ground net of the circuit.
  • IN+: Connected to the 12V-24V Output (DC) of the POWER SUPPLY 12V 5AMP.
  • IN-: Connected to the GND (DC) of the POWER SUPPLY 12V 5AMP.

ESP32 (30 pin)

  • Vin: Connected to the OUT+ of the LM2596 Step Down Module.
  • GND: Connected to the ground net of the circuit.
  • GPIOs: Connected to various components as detailed above.

Documented Code

/*
 * This Arduino Sketch controls a vibration detector and a smoke detector.
 * If vibration is detected, it alerts the user via serial print and web.
 * If smoke is detected, it activates an exhaust fan through a solid-state relay.
 * The exhaust fan can also be controlled via web, and the DC motor can only
 * be controlled via web. It also sets up a web server to monitor the status
 * and control the fan and motor. Additionally, it uses LEDs to indicate the
 * status: green for no danger, yellow for partial danger, and red for full danger.
 */

#include <WiFi.h>
#include <WebServer.h>

// WiFi credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";

// Pin definitions
const int vibrationSensorPin = 15; // Digital output of SW-420
const int smokeSensorPin = 19; // Analog output of MQ-2
const int relayPin1 = 25; // CH1 of Solid State Relay (Exhaust Fan)
const int relayPin2 = 33; // CH2 of Solid State Relay (DC Motor)
const int greenLEDPin = 26; // Green LED
const int yellowLEDPin = 27; // Yellow LED
const int redLEDPin = 14; // Red LED

// Create a web server object
WebServer server(80);

// Variables to store the state of the relays
bool relay1State = false;
bool relay2State = false;

void setup() {
  // Initialize serial communication
  Serial.begin(115200);

  // Initialize sensor pins
  pinMode(vibrationSensorPin, INPUT);
  pinMode(smokeSensorPin, INPUT);

  // Initialize relay pins
  pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);

  // Initialize LED pins
  pinMode(greenLEDPin, OUTPUT);
  pinMode(yellowLEDPin, OUTPUT);
  pinMode(redLEDPin, OUTPUT);

  // Ensure relays and LEDs are off initially
  digitalWrite(relayPin1, LOW);
  digitalWrite(relayPin2, LOW);
  digitalWrite(greenLEDPin, LOW);
  digitalWrite(yellowLEDPin, LOW);
  digitalWrite(redLEDPin, LOW);

  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
  Serial.println(WiFi.localIP());

  // Define web server routes
  server.on("/", handleRoot);
  server.on("/status", handleStatus);
  server.on("/control", handleControl);

  // Start the server
  server.begin();
}

void loop() {
  // Handle client requests
  server.handleClient();

  // Read sensor values
  int vibrationValue = digitalRead(vibrationSensorPin);
  int smokeValue = analogRead(smokeSensorPin);

  // Check if vibration is detected
  if (vibrationValue == HIGH) {
    Serial.println("Alert: Vibration detected!");
  }

  // Check if smoke is detected
  if (smokeValue > 300) { // Threshold for smoke
    // Activate exhaust fan relay
    digitalWrite(relayPin1, HIGH);
    relay1State = true;
    Serial.println("Alert: Smoke detected!");
  } else {
    // Deactivate exhaust fan relay
    digitalWrite(relayPin1, LOW);
    relay1State = false;
  }

  // LED indication logic
  if (vibrationValue == HIGH && smokeValue > 300) {
    // Red LED for full danger
    digitalWrite(greenLEDPin, LOW);
    digitalWrite(yellowLEDPin, LOW