Build Your Own Smart Motion Detector That Actually Works! (ESP32 + PIR Tutorial)

Mounir - Feb 24 - - Dev Community

Want to protect your home with a DIY solution? Create your own smart motion detector that works as effectively as commercial systems. Perfect for beginners in IoT and home automation!

What You'll Create:

  • A reliable motion detection system that sends instant alerts
  • Your own IoT device that works with smart home platforms
  • A professional-grade security sensor you built yourself
  • Automated email notifications when motion is detected

Prerequisites :

Required Hardware:

  • ESP32 board
  • PIR motion sensor
  • Jumper wires
  • 1kΩ resistor
  • Breadboard
  • Micro USB cable

Quick Assembly:

  1. Connect PIR VCC → ESP32 5V
  2. Connect PIR GND → ESP32 GND
  3. Connect PIR DATA → ESP32 GPIO2 (with 1kΩ resistor)

PIR sensor and ESP32 diagram

Note: The ESP32 GPIOs require 3V3 signals (not 5V tolerent). For quick hack, you can only add a serial resistor of 1K between the PIR DATA pin and the ESP32 GPIO pin to protect the ESP32 GPIO from damage. However, for production, a 5V/3V3 level shifter is needed to guarantee long term circuit reliability. You can check this page to get a 5V/3V3 level shifter module.

Setup Instructions:

1. Prepare Your Environment

https://dl.espressif.com/dl/package_esp32_index.json,http://arduino.esp8266.com/stable/package_esp8266com_index.json

  • Now, open boards manager (Tools > Board > Boards Manager), search for ESP32 and click the install button for the “ESP32 by Espressif Systems”.

2. Code

  • Install the MQTT PubSubClient Library for the Arduino IDE if you haven’t already.
  • Download this demo from the AskSensors Github page.
  • The provided code simply reads the status of the PIR sensor and send this information to AskSensors over MQTT. You need to modify the following variables:
  • Your network credentials, so that ESP8266 can establish a connection with existing network
  • The Api Key In and username to set the MQTT Topic:username/apiKeyIn
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";
const char* username = "YOUR_USERNAME";
const char* pubTopic = "publish/username/apiKeyIn";
Enter fullscreen mode Exit fullscreen mode

3. Upload and Test

  • Connect ESP32 via USB
  • Check your Sensor data stream on AskSensors.
  • You can cross-check the AskSensors graph readings with the values being printed on your ESP32 Serial Terminal.
  • You can customize a Binary graph to plot your data as showing by next pictures.

PIR sensor on

PIR sensor off

4. Configure Email Alerts

  1. Log into your AskSensors Iot platform & select your sensor
  2. Click the 'Add Alert' button
  3. Select 'Email Alert' as the notification type
  4. Set your desired threshold values:
    • Choose 'Max Value' = 1 to detect motion
  5. Enter your email address
  6. Save the configuration

Now you'll receive instant email notifications whenever motion is detected!

Perfect For:

  • Home security
  • Smart lighting automation
  • Pet monitoring
  • Visitor counting
  • Energy saving
  • Remote property monitoring

This project takes only 30 minutes to complete and can be a great addition to your smart home security system. With email alerts, you'll never miss any unauthorized movement again!

.