Home » DIY $5 Wireless Smart Home Motion Sensor: A Comprehensive Guide

DIY $5 Wireless Smart Home Motion Sensor: A Comprehensive Guide

by Ravi Singh
0 comments 7 minutes read
DIY motion sensor powered by battery under 5 us dollars

Summary: Welcome to this comprehensive guide where I’ll show you how to build a wireless, battery-powered motion sensor for your smart home. This project is affordable, easy to assemble, and integrates seamlessly with platforms like Home Assistant, Node-RED, or any MQTT server. By the end of this tutorial, you’ll have a fully functional motion sensor that can enhance your smart home automation setup—all for under $5!

In today’s smart home ecosystem, motion sensors play a crucial role in automating tasks like turning on lights, triggering alarms, or sending notifications when movement is detected. However, most commercial options are either expensive or difficult to customize. That’s where this DIY project comes in.

This guide will help you create a cost-effective, wireless motion sensor using an ESP-01 WiFi module and a PIR sensor. The device operates on battery power and can last for over a month on a single charge, making it ideal for various smart home applications.

Materials Needed

Before diving into the assembly process, let’s gather all the components you’ll need for this project:

  • ESP-01 WiFi Module
    The brain of the sensor.
  • PIR SR602 Motion Sensor
    For motion detection using infrared signals emitted by mammals, such as humans.
  • 2N2222 Transistor
  • Resistors:
    • 10kΩ
    • 5kΩ
    • 1kΩ
  • 1uF 63V Capacitor
    Ensures that the ESP-01 resets correctly when motion is detected.
  • General-purpose PCB
    A small circuit board where all the components will be assembled.
  • 1800mAh Li-Ion Battery (or 16340 Li-Ion cell)
  • USB to Serial Module
    Used for flashing the firmware onto the ESP-01.
  • Li-Ion Charging Module
    Allows you to recharge the battery when needed.
  • 3D-printed case
    To house the sensor and keep it protected. You can download the design from Thingiverse and either print it at home or use a printing service.

Circuit Overview

The circuit you’ll be assembling is designed to keep the ESP-01 in deep sleep mode to conserve power, waking it up only when the PIR sensor detects motion. This significantly reduces power consumption, allowing the device to operate for over a month on a single battery charge, even with 20-30 triggers per day.

Circuit Diagram

Refer to the enhanced circuit diagram in this guide for a visual representation of the connections and component placements.

A more simple diagram of the circuit can be found at Instructables.

Flashing the Firmware

Before the ESP-01 can be installed in the circuit, you need to flash it with the appropriate firmware.

  • Connect to PC using a USB to Serial module to connect the ESP-01 to your computer.
  • Download Code and Libraries within the Arduino IDE. These include PubSubClient and WIFI.
  • Open the Arduino IDE and paste the following code:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
 
// Replace with your Wi-Fi credentials
const char* ssid = "WiFi-SSID";
const char* password = "WI-FI_PASSWORD";
 
// MQTT Server details
const char* mqtt_server = "192.168.0.xxx";
const char* mqtt_user = "USERNAME";
const char* mqtt_password = "PASSWORD";
const int mqtt_port = 1883;
 
// PIR Sensor
int LED = LED_BUILTIN;          // Choose the pin for the LED 
int pirPin = 3;                 // Using the RX Pin as input from the PIR
int val = 0;                    // Variable for reading the pin status
 
WiFiClient espClient;
PubSubClient client(espClient);
 
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (unsigned int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
}
 
void blink(int times, int delayTime) {
  for (int j = 1; j <= times; j++) {
    digitalWrite(LED, LOW);
    delay(delayTime);
    digitalWrite(LED, HIGH);
    delay(delayTime);
  }
}
 
void connectToMQTT() {
  while (!client.connected()) {
    Serial.println("Connecting to MQTT server...");
    if (client.connect("espClient", mqtt_user, mqtt_password)) {
      Serial.println("Connected to MQTT server");
      client.setCallback(callback);
      client.subscribe("inTopic");
    } else {
      Serial.print("Failed to connect, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}
 
void setup() {
  pinMode(LED, OUTPUT);
  digitalWrite(LED, HIGH);
  pinMode(pirPin, INPUT);
  Serial.begin(115200);
  delay(10);
 
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("\nConnected to WiFi");
 
  client.setServer(mqtt_server, mqtt_port);
 
  blink(3, 100);
 
  connectToMQTT();
}
 
void loop() {
  if (!client.connected()) {
    connectToMQTT();
  }
  client.loop();
 
  val = digitalRead(pirPin);
  Serial.print("PIR Sensor Value: ");
  Serial.println(val); // Debug print for PIR sensor value
 
  if (val == HIGH) {
    digitalWrite(LED, LOW);
    Serial.println("Motion Detected!");
    if (client.publish("MainEntrancePir/sensor", "Motion Detected!")) {
      Serial.println("Motion Detected message sent");
    } else {
      Serial.println("Failed to send Motion Detected message");
    }
    delay(10000); // short delay before checking the PIR again
  } else {
    digitalWrite(LED, HIGH);
    Serial.println("No Motion Detected.");
    if (client.publish("MainEntrancePir/sensor", "No Motion Detected.")) {
      Serial.println("No Motion Detected message sent");
    } else {
      Serial.println("Failed to send No Motion Detected message");
    }
    delay(2000);
    blink(1, 200);
    ESP.deepSleep(0);
  }
}
 
  • Save the project.
  • Then select “ESP8266” as the board type and choose the correct COM port in Windows or the serial port in macOS.
  • Press CTRL+U to compile and upload the firmware to the ESP-01.
  • Once the firmware is successfully uploaded, disconnect the ESP-01 from the USB to Serial module and install it onto the circuit board.

Installing the DIY Motion Sensor

Once the ESP-01 is installed on the PCB, connect the 1800mAh Li-Ion battery to power the device. If you’re using a 3D-printed case, place the assembled circuit board inside the case. The design for the case can be downloaded from Thingiverse (currently unavailable), allowing you to print it at home or through a service.

Power the Device and Charge the Battery

The sensor can operate for over a month on a single charge, even with 20-30 motion detections per day.

When the battery runs low, simply remove it from the device and connect it to a Li-Ion charging module. These modules are inexpensive and easy to use, allowing you to recharge the battery using a standard USB cable.

Testing and Integration

To test the sensor, place it in the desired location and walk in front of it. The PIR sensor should detect the motion and wake the ESP-01.

If integrated with Home Assistant or another smart home platform, the ESP-01 should send a message via MQTT, triggering your automation—such as turning on a light.

Real-world Use Case

In my setup, I use the sensor to control the lights on my staircase. When the sensor detects movement, the lights turn on within 2-3 seconds, providing a seamless and responsive smart home experience.

Video Guide – For Detailed Visual Instructions

Conclusion

Congratulations! You’ve just built a wireless, battery-powered motion sensor for your smart home. This project is not only affordable but also highly practical, offering a reliable and long-lasting solution for automating various tasks in your home.

With a battery life that exceeds a month even under regular use, this sensor is an excellent addition to any smart home setup. And with the option to customize the enclosure using a 3D-printed case, it’s both functional and aesthetically pleasing.

If you found this guide helpful, don’t forget to share it with others who might be interested in DIY electronics and smart home projects. And make sure to subscribe to stay updated on more DIY smart home tutorials!

You may also like

Copyright 2024 – All Right Reserved. TechPosts Media

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.