Create a Wi-Fi Controlled Motion Sensor Light with ESP8266 – A Beginner's Guide
Ever walked into a dark hallway and fumbled for a switch? A motion‑activated light that turns on with a whisper of Wi‑Fi can make those moments disappear. Plus, building it yourself gives you a tiny taste of smart‑home power without a pricey hub. Let’s dive in and make a light that wakes up when you walk by and can be switched on or off from your phone.
What You’ll Need
Parts List
- ESP8266 development board (NodeMCU or Wemos D1 Mini work fine)
- PIR motion sensor (the 5V type is the most common)
- Small LED strip or a 5V/12V lamp module
- N‑channel MOSFET (IRLZ44N or similar) – to drive the lamp
- 10 kΩ resistor – pull‑down for the sensor output
- Breadboard and jumper wires
- 5 V power supply (USB wall adapter or a 5 V phone charger)
- Optional: a 3D‑printed or laser‑cut enclosure for a tidy look
Tools
- Soldering iron (if you want a permanent build)
- Wire cutters/strippers
- A computer with Arduino IDE installed
All of these parts can be found on a local electronics store or online for under $20 total. I started with a spare NodeMCU from a previous project, so the cost was practically zero.
How the Circuit Works
The PIR sensor watches for infrared changes – basically, it senses the heat of a moving body. When it detects motion, it pulls its output pin HIGH (3.3 V). We feed that signal to the ESP8266’s GPIO pin, which then tells the MOSFET to let current flow to the lamp. The MOSFET acts like a switch that can handle the lamp’s current without heating the ESP8266.
Why a MOSFET? The ESP8266’s pins can only source about 12 mA, far less than a LED strip or a small lamp needs. The MOSFET lets the heavy current go straight from the power supply to the lamp, while the ESP8266 just tells it when to open.
Step‑by‑Step Build
1. Wire the PIR Sensor
- Connect the sensor’s VCC to the ESP8266’s 3.3 V pin.
- Connect GND to GND.
- Connect the OUT pin to GPIO 14 (D5 on a NodeMCU).
- Add a 10 kΩ resistor between OUT and GND – this keeps the line low when the sensor is idle.
2. Set Up the MOSFET Switch
- Source pin of the MOSFET goes to GND.
- Drain pin connects to the negative side of your lamp.
- Positive side of the lamp connects to the 5 V supply.
- Gate pin connects to GPIO 12 (D6).
- Put a 220 Ω resistor between GPIO 12 and the gate – it protects the ESP8266 from spikes.
3. Power the ESP8266
Plug the ESP8266 into the USB power supply. If you are using a 5 V lamp, make sure the power supply can deliver enough current for both the lamp and the board (at least 1 A is safe).
4. Test the Wiring
Before loading any code, power everything up and watch the sensor’s LED (most PIR modules have a tiny indicator). Wave your hand in front of it – the LED should flash. If the lamp flickers, double‑check the MOSFET connections.
Programming the ESP8266
We’ll use the Arduino IDE because it’s simple and works on Windows, macOS, and Linux.
Install the Board Package
- Open File → Preferences and add
http://arduino.esp8266.com/stable/package_esp8266com_index.jsonto the Additional Boards Manager URLs. - Go to Tools → Board → Boards Manager, search for esp8266, and install the latest version.
Sketch Overview
The code does three things:
- Connects to your Wi‑Fi network.
- Starts a tiny web server that lets you turn the light on or off from any browser.
- Listens to the PIR pin and automatically turns the light on for a set time when motion is detected.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
const int pirPin = D5; // GPIO14
const int lampPin = D6; // GPIO12 (MOSFET gate)
const unsigned long lightTime = 30000; // 30 seconds
ESP8266WebServer server(80);
unsigned long motionStart = 0;
bool manualOverride = false;
void handleRoot() {
String html = "<h1>ESP8266 Motion Light</h1>"
"<p><a href=\"/on\">Turn ON</a> | "
"<a href=\"/off\">Turn OFF</a></p>";
server.send(200, "text/html", html);
}
void handleOn() {
digitalWrite(lampPin, HIGH);
manualOverride = true;
server.sendHeader("Location", "/");
server.send(302, "text/plain", "");
}
void handleOff() {
digitalWrite(lampPin, LOW);
manualOverride = false;
server.sendHeader("Location", "/");
server.send(302, "text/plain", "");
}
void setup() {
pinMode(pirPin, INPUT);
pinMode(lampPin, OUTPUT);
digitalWrite(lampPin, LOW);
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected, IP: " + WiFi.localIP().toString());
server.on("/", handleRoot);
server.on("/on", handleOn);
server.on("/off", handleOff);
server.begin();
}
void loop() {
server.handleClient();
if (digitalRead(pirPin) == HIGH && !manualOverride) {
digitalWrite(lampPin, HIGH);
motionStart = millis();
}
if (!manualOverride && motionStart && millis() - motionStart > lightTime) {
digitalWrite(lampPin, LOW);
motionStart = 0;
}
}
Replace YOUR_SSID and YOUR_PASSWORD with your Wi‑Fi details. Upload the sketch, open the Serial Monitor, and note the IP address that appears. Type that address into any phone or laptop browser – you’ll see a tiny page with ON/OFF links.
5. Fine‑Tuning
- Delay time – Change
lightTimeto keep the light on longer or shorter after motion stops. - Sensitivity – Most PIR modules have two tiny potentiometers. One adjusts range, the other adjusts how long the sensor stays active after detecting motion. Turn them slowly and test each change.
Testing and Tweaking
Walk through the sensor’s field of view. The light should turn on automatically, stay lit for the set period, then fade. Open the web page on your phone and click “OFF” – the lamp should go dark immediately, ignoring any further motion until you click “ON” again. This manual override is handy for late‑night bathroom trips.
If the lamp flickers or the ESP8266 restarts, check the power supply. The ESP8266 can draw spikes of up to 300 mA when connecting to Wi‑Fi, so a weak charger will cause brown‑outs. A small 5 V 2 A wall wart solves the problem.
Tips and Common Mistakes
- Don’t power the lamp directly from the ESP8266’s 3.3 V pin. It will fry the board. Always use the MOSFET switch.
- Pull‑down resistor on the PIR output keeps the GPIO from floating when the sensor is idle. Without it you may get random triggers.
- Keep the code simple at first. Once the basic motion‑on/off works, you can add features like MQTT integration or Alexa control.
- Enclosure matters. If you plan to mount the unit on a wall, a small plastic box with a hole for the sensor works well. I printed a case that fits the NodeMCU and MOSFET snugly – it looks neat and protects the wires.
- Safety first. If you decide to drive a 120 V lamp, you must use a proper relay or a certified smart‑plug module. The MOSFET method is only for low‑voltage LED strips or 12 V lamps.
That’s it – a fully functional Wi‑Fi motion sensor light you built with your own hands. The next time you step into a dark room, the light will greet you like an old friend, and you’ll have the satisfaction of knowing you made it happen.
- → DIY Tower Stack Light with Smart Home Integration @stacklightchronicles
- → Integrating LED Stack Lights with Home Assistant: A Practical DIY Tutorial @stacklightchronicles
- → How to Choose and Set Up a Beginner‑Ready Smart Home Hub Without Breaking the Bank @smarthomestarter
- → Converting a Standard Light Switch into a Smart Switch with Basic Tools @tinkertool
- → How to Add a Smart Thermostat to an Older Home Without Rewiring @homeinnovate