Introduction
In this article, we’ll explore how to create a motion detection system using two ESP32 boards and the ESP-NOW protocol. One ESP32 board will be equipped with a PIR (Passive Infrared) sensor to detect motion, while the other will feature an OLED display to showcase the detection status. We’ll delve into the workings of ESP-NOW, a protocol specifically designed for low-power, peer-to-peer communication between ESP32 devices. By the end of this guide, you’ll have a fully functioning motion detection system that can alert you to any movements within its vicinity.
Working Principle
The working principle of our motion detection system revolves around the ESP32 boards communicating with each other using the ESP-NOW protocol. The ESP32 board equipped with the PIR sensor constantly monitors for any motion. When motion is detected, it sends a signal to the other ESP32 board via ESP-NOW. The receiving ESP32 board then displays the motion detection status on the OLED display, providing real-time updates on any movements.
ESP-NOW Protocol
ESP-NOW is a communication protocol developed by Espressif Systems specifically for the ESP32 and ESP8266 microcontrollers. It enables direct, peer-to-peer communication between ESP32 devices without the need for a traditional Wi-Fi network. This protocol is ideal for scenarios requiring low-latency and low-power communication, making it perfect for our motion detection system.
Code
To find the MAC address of both the ESP32’s. Full Tutorial on youtube.
#include "WiFi.h"
void setup(){
Serial.begin(115200);
WiFi.mode(WIFI_MODE_STA);
Serial.println(WiFi.macAddress());
}
void loop(){
}
Circuit Diagram
The circuit diagram for our motion detection system will include the following components:
- ESP32
- OLED display (SSD1306)
- PIR sensor
Code for the ESP32’s. Make sure you add the respective broadcasting MAC address into the Code.
#include <esp_now.h>
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// PIR sensor pin
#define PIR_PIN 2
// REPLACE WITH THE MAC Address of your receiver
uint8_t broadcastAddress[] = {0x0C, 0xB8, 0x15, 0xC3, 0xA2, 0xD8};
//uint8_t broadcastAddress[] = {0x0C, 0xB8, 0x15, 0xD6, 0x0E, 0xE0};
// Variable to store motion detection status
String detectionStatus;
// Callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
if (status ==0){
detectionStatus = "Motion Detected!";
}
else{
detectionStatus = "No Motion Detected";
}
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
// Init OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
}
void loop() {
// Check for motion
int motion = digitalRead(PIR_PIN);
// Send motion detection status via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &motion, sizeof(motion));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
// Update OLED display with motion detection status
updateDisplay(motion);
delay(1000); // Delay for stability
}
void updateDisplay(int motion) {
// Display motion detection status on OLED Display
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Motion Sensor");
display.setCursor(0, 15);
display.print("Status: ");
display.println(motion == HIGH ? "Detected" : "Not Detected");
display.setCursor(0, 30);
display.println(detectionStatus);
display.display();
// Print motion detection status to Serial Monitor
Serial.print("Motion: ");
Serial.println(motion == HIGH ? "Detected" : "Not Detected");
}
By following the circuit diagram and implementing the provided code snippets, you can create your motion detection system using ESP32 boards. With the power of ESP-NOW, you’ll have a reliable and efficient communication channel between the sensor and display, ensuring timely updates on any detected motion.
Whether it’s for home security, automation projects, or simply tinkering with IoT devices, this motion detection system opens up a world of possibilities for ESP32 enthusiasts. Happy tinkering!