Logo

ESP32 Motion Detection Alarm System

Introduction to ESP32 Motion Detection Alarm System

In this project, we will create a motion detection alarm system using the ESP32 and a PIR (Passive Infrared) sensor. The system will trigger an alarm (such as a buzzer) when motion is detected within the sensor’s range. This project is useful for home security and automation systems, providing a basic understanding of how to interface sensors with the ESP32.

Components Required

The following components are needed to build the ESP32 motion detection alarm system: ESP32 development board, PIR motion sensor, buzzer, jumper wires, and a breadboard for connections.

  • Connect the PIR motion sensor to the ESP32. The sensor has three pins: VCC, GND, and OUT.
  • Connect the buzzer to one of the digital GPIO pins on the ESP32.
  • Write code to monitor the PIR sensor's output and trigger the buzzer when motion is detected.
  • Upload the code to the ESP32 and test the motion detection and alarm system.

Formula: Motion Detected (HIGH) -> Trigger Alarm (Buzzer ON) / No Motion (LOW) -> No Alarm (Buzzer OFF)

Components List

  • 1 × ESP32 Development Board
  • 1 × PIR Motion Sensor
  • 1 × Buzzer (active or passive)
  • Jumper wires
  • Breadboard

Pin Configuration

  • GPIO 13 (Buzzer): The buzzer is connected to GPIO 13 of the ESP32 to produce a sound when motion is detected.
  • GPIO 14 (PIR Motion Sensor): The PIR sensor’s OUT pin is connected to GPIO 14. The VCC and GND are connected to the 3.3V and ground of the ESP32, respectively.

Ensure that the PIR motion sensor is connected properly with the ESP32 and is placed in an area where it can detect motion effectively.

Wiring and Connections

  • -> GPIO 14
  • -> GPIO 13

Code for ESP32 Motion Detection Alarm System

1#define PIR_PIN 14
2#define BUZZER_PIN 13
3
4int pirState = LOW;  // Initialize PIR state
5int val = 0;        // Variable to store PIR sensor value
6
7void setup() {
8  pinMode(PIR_PIN, INPUT);  // Set PIR pin as input
9  pinMode(BUZZER_PIN, OUTPUT);  // Set Buzzer pin as output
10  Serial.begin(115200);  // Start serial communication for debugging
11}
12
13void loop() {
14  val = digitalRead(PIR_PIN);  // Read PIR sensor value
15
16  if (val == HIGH) {  // If motion is detected
17    digitalWrite(BUZZER_PIN, HIGH);  // Turn on buzzer
18    Serial.println("Motion Detected!");
19  } else {
20    digitalWrite(BUZZER_PIN, LOW);  // Turn off buzzer
21    Serial.println("No Motion");
22  }
23  delay(200);  // Small delay for stability
24}

Code Explanation

  • #define PIR_PIN 14: This defines the pin number for the PIR motion sensor. It is connected to GPIO 14 on the ESP32.
  • #define BUZZER_PIN 13: This defines the pin number for the buzzer. It is connected to GPIO 13 on the ESP32.
  • pinMode(PIR_PIN, INPUT);: This sets the PIR sensor pin (GPIO 14) as an input to receive motion detection signals.
  • pinMode(BUZZER_PIN, OUTPUT);: This sets the buzzer pin (GPIO 13) as an output to activate the buzzer.
  • val = digitalRead(PIR_PIN);: This reads the value from the PIR sensor. If motion is detected, the value will be HIGH (1), otherwise LOW (0).
  • if (val == HIGH) {: This condition checks if motion has been detected (when the PIR sensor output is HIGH).
  • digitalWrite(BUZZER_PIN, HIGH);: If motion is detected, this line turns on the buzzer by setting the buzzer pin to HIGH.
  • digitalWrite(BUZZER_PIN, LOW);: If no motion is detected, this line turns off the buzzer by setting the buzzer pin to LOW.
  • delay(200);: This introduces a short delay to prevent reading the PIR sensor too quickly and to ensure stable readings.

Applications

  • Home security systems where an alarm is triggered by detected motion.
  • Automatic lighting systems that turn on when motion is detected.
  • Surveillance applications for detecting human presence in rooms or buildings.

Conclusion

This ESP32 motion detection alarm system project demonstrates how to use the ESP32 with a PIR sensor to detect motion and trigger an alarm. It is a useful foundation for building more complex security or automation systems. The project provides a good understanding of using sensors and actuators with the ESP32 for practical applications.