Using a PIR Motion Sensor with ESP32
Using a PIR Motion Sensor with ESP32
Introduction
This project shows how to detect motion with the ESP32 using a PIR motion sensor. The motivation behind this tutorial is that sometimes we want to discern thermal activity from other types of movement and this type of motion sensor can achieve that. I hope that readers can gain a better understanding of how motion sensors work and how they work together with the ESP32 board.
Learning Objectives
- How IR sensors work
- Implementing code that detects high and low
- Using Arduino with ESP32
Background Information
This circuit uses an IR sensor to detect motion of a heat source and this sensor is connected to a lightbulb, which turns on if motion is detected. Other similar ideas might be to use a buzzer instead of an LED. The pro is that it can detect human motion quite well. A con is that the way the code is set up, it detects the movement for a while so any additional movement won’t be detected if already detected once. This is due to the nature of the delay function.
Getting Started
For the software, all that is used is the Arduino IDE to upload the code to the ESP32 board. A very in depth tutorial is linked below that applies to both Windows and Mac.
For hardware, the components are as follows:
- ESP32 Board
- IR motion sensor
- LED
- resistor
- Board to computer cable
- Jumper wires
- Breadboard
Required Downloads and Installations
Below is the tutorial for installing Arduino IDE and many other features to assist in getting set up. Technically, only part 4 is required (20:50-34:37). Just make sure to match the board you have to your input port as he uses an Arduino board.
https://youtu.be/BLrHTHUjPuw?feature=shared
Required Components
Component Name | Quanitity |
---|---|
ESP 32 Board | 1 |
Chord to connect board to computer | 1 |
PIR motion sensor (HC-SR501) | 1 |
Led | 1 |
Resistor (220Ω used) | 1 |
Female to male jumper wires | 2 |
Female to femal jumper wires for motion sensor | 3 |
Breadboard | 1 |
Required Tools and Equipment
Computer with Arduino IDE installed
Implementation
Introduction
Now that the software is installed and ready to use, we can build the schematic using the required components above and upload the code so that it works.
Schematic
Above shows how the wires are connected to the devboard with red connected to 5V, blacks both connecetd to GND while yellow PIR wire and orange LED wire connected to pin 13 and 14 respectively.
Above is a nice diagram of the connection pins. The red wire connected to 5V goes to the VCC terminal, the yellow pin 13 wire is connected to OUT terminal and black goes from GND to GND. All of these are female to female jumper wires.
Above shows how the ESP32, circuit and sensor are all hooked up together. The resistor and diode are placed in series where the orange pin 14 wire is connected to the positive termainal of the LED and the black wire is connected back to GND. These are both female to male jumper wires.
Code
This is the code for the thingamabobdoohickie
/*This code reads the digital input from a PIR (passive infrared) motion sensor connected to pin 2.
If the digital input is HIGH, it prints the message "Somebody here!" to the serial monitor. */
/* Board: ESP32 Development Board
Component: PIR (passive infrared) motion sensor(HC-SR501) */
// Define the pin number for the PIR sensor
const int PIR_PIN = 13;
const int LED_PIN = 14;
// Declare and initialize the state variable
int state = 0;
void setup() {
pinMode(PIR_PIN, INPUT); // Set the PIR pin as an input
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600); // Start serial communication with a baud rate of 9600
}
void loop() {
state = digitalRead(PIR_PIN); // Read the state of the PIR sensor
if (state == HIGH) { // If the PIR sensor detects movement (state = HIGH)
Serial.println("Somebody here!"); // Print "Somebody here!" to the serial monitor
digitalWrite(LED_PIN, HIGH);
delay(100);
} else {
Serial.println("Monitoring...");
digitalWrite(LED_PIN, LOW);
delay(100);
}
}`
Results
GIF
Above is the final result of the motion sensor going off when it detects motion
Analysis
When everything is built and uploaded, this is what it looks like. The only thing is that by definition of being an infrared detector, it can only detect human or animal motion or anything with a heat signature. Also the range that the scanner actually detects is quite wide.
Additional Resources
Useful links
https://www.mikroshop.ch/pdf/ESP32_Start_Kit.pdf
Above is where I got the IR scanner port image from