Motion Activated Smart Room Light Using ESP8266/ESP32 and Blynk
Build a motion-activated smart room light using ESP8266 or ESP32, a PIR sensor, relay, bulb, and Blynk with automatic timeout control and remote monitoring.
- ESP8266
- ESP32
- PIR Sensor
- Motion Detection
- Smart Light
- Blynk
- Relay
- IOT
- Automation
- Published
- Reading time
- 2 min read
- Author
- Saroj Chaudhary
- Role
- IoT & Embedded Systems Engineer
This project uses a PIR motion sensor with an ESP8266 or ESP32 to automatically control a room light.
When motion is detected, the controller turns the light ON through a relay. If no new motion is detected for a fixed timeout period, the light turns OFF automatically.
The motion status and light status can also be monitored using Blynk.
Components Required
| Component | Quantity |
|---|---|
| ESP8266 NodeMCU or ESP32 | 1 |
| PIR Motion Sensor | 1 |
| Relay Module | 1 |
| Low-voltage Bulb / Lamp | 1 |
| Suitable Power Supply | 1 |
| Breadboard | 1 |
| Jumper Wires | As required |
For classroom practicals, use a low-voltage DC lamp instead of exposed AC mains wiring.
How It Works
Motion Detected
|
v
PIR Sensor
|
v
ESP8266 / ESP32
/ \
v v
Relay Wi-Fi
| |
v v
Light Blynk
Control logic:
Motion detected -> Light ON
No new motion
for timeout period -> Light OFF
Circuit Connections
PIR Sensor
For ESP8266:
| PIR | ESP8266 |
|---|---|
| VCC | VIN / suitable supply |
| GND | GND |
| OUT | D5 / GPIO14 |
For ESP32:
| PIR | ESP32 |
|---|---|
| VCC | 5V / suitable supply |
| GND | GND |
| OUT | GPIO27 |
Relay
For ESP8266:
| Relay | ESP8266 |
|---|---|
| IN | D1 / GPIO5 |
| GND | GND |
| VCC | According to relay module |
For ESP32:
| Relay | ESP32 |
|---|---|
| IN | GPIO26 |
| GND | GND |
| VCC | According to relay module |
Connect the lamp through the relay’s COM and NO terminals.
Blynk Setup
Create these Virtual Pin datastreams:
| Datastream | Virtual Pin | Purpose |
|---|---|---|
| Motion Status | V0 | Shows motion detection |
| Light Status | V1 | Shows whether light is ON/OFF |
Suggested values:
0 -> No Motion / Light OFF
1 -> Motion Detected / Light ON
ESP8266 / ESP32 Code
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "YOUR_TEMPLATE_ID"
#define BLYNK_TEMPLATE_NAME "Smart Room Light"
#define BLYNK_AUTH_TOKEN "YOUR_AUTH_TOKEN"
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
const int PIR_PIN = D5;
const int RELAY_PIN = D1;
#else
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
const int PIR_PIN = 27;
const int RELAY_PIN = 26;
#endif
char ssid[] = "YOUR_WIFI_NAME";
char pass[] = "YOUR_WIFI_PASSWORD";
const bool RELAY_ACTIVE_LOW = true;
// Light remains ON for 30 seconds after the latest motion.
const unsigned long LIGHT_TIMEOUT = 30000;
bool lightOn = false;
unsigned long lastMotionTime = 0;
BlynkTimer timer;
void setLight(bool state)
{
lightOn = state;
if (RELAY_ACTIVE_LOW)
digitalWrite(RELAY_PIN, state ? LOW : HIGH);
else
digitalWrite(RELAY_PIN, state ? HIGH : LOW);
if (Blynk.connected())
Blynk.virtualWrite(V1, lightOn ? 1 : 0);
}
void checkMotion()
{
bool motion = digitalRead(PIR_PIN);
if (motion)
{
lastMotionTime = millis();
if (!lightOn)
setLight(true);
}
if (lightOn && (millis() - lastMotionTime >= LIGHT_TIMEOUT))
{
setLight(false);
}
if (Blynk.connected())
Blynk.virtualWrite(V0, motion ? 1 : 0);
Serial.print("Motion: ");
Serial.print(motion ? "YES" : "NO");
Serial.print(" | Light: ");
Serial.println(lightOn ? "ON" : "OFF");
}
void setup()
{
Serial.begin(115200);
pinMode(PIR_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
setLight(false);
Blynk.begin(
BLYNK_AUTH_TOKEN,
ssid,
pass
);
timer.setInterval(200L, checkMotion);
Serial.println("Motion Activated Smart Light Started");
}
void loop()
{
Blynk.run();
timer.run();
}
Adjusting the Timeout
The example keeps the light ON for:
const unsigned long LIGHT_TIMEOUT = 30000;
This means:
30000 ms = 30 seconds
For a 1-minute timeout:
const unsigned long LIGHT_TIMEOUT = 60000;
Every new motion detection resets the timeout.
Testing
Motion Detected
Walk in front of the PIR sensor.
Expected result:
Motion Status -> 1
Light -> ON
No Motion
Stop moving and wait for the timeout.
Expected result:
Motion Status -> 0
Light -> OFF after timeout
Common Problems
PIR always shows motion
Allow the PIR sensor some startup time and adjust its sensitivity/delay controls if available.
Light turns OFF too quickly
Increase LIGHT_TIMEOUT.
Relay works in reverse
Change:
const bool RELAY_ACTIVE_LOW = true;
to:
const bool RELAY_ACTIVE_LOW = false;
Blynk does not update
Check Wi-Fi credentials, Blynk credentials, and Virtual Pin configuration.
Final Result
The completed project provides:
- motion detection
- automatic room-light control
- timeout-based light OFF
- automatic timeout reset when new motion is detected
- Blynk motion monitoring
- Blynk light-status monitoring
Motion -> Light ON
No Motion -> Wait
Timeout Complete -> Light OFF
Safety Note
Use a low-voltage DC lamp for normal student practical work.
If an AC mains lamp is used in a future installation, the mains wiring must be properly enclosed and handled only by a qualified person.