← Back to insights
ESP8266ESP32IoTBlynkSensorsRemote MonitoringBeginner

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.

Related Articles

ESP8266Beginner

IoT Home Intruder Detection System Using ESP8266/ESP32 and Blynk

Build an IoT home intruder detection system using ESP8266 or ESP32, PIR and IR sensors, and Blynk for movement detection, entry monitoring, security status, and remote intrusion alerts.

  • ESP8266
  • ESP32
  • Nodemcu
4 min readESP32
Read article
ESP8266Intermediate

Ultrasonic Smart Parking Assistant Using ESP8266/ESP32 and Blynk

Build a two-slot smart parking assistant using ESP8266 or ESP32, two ultrasonic sensors, an I2C LCD, and Blynk to show distance, parking status, and Safe/Warning/Stop zones.

  • ESP8266
  • ESP32
  • Ultrasonic Sensor
3 min readESP32
Read article
ESP8266Beginner

Automatic Night Light with IoT Monitoring Using ESP8266 and Blynk

Build an automatic night light using ESP8266, an LDR sensor, relay, bulb, and Blynk for light-level monitoring and manual override control.

  • ESP8266
  • Nodemcu
  • LDR
2 min readIoT
Read article

Author

Saroj Chaudhary

IoT & Embedded Systems Engineer

Founder-led engineering notes from IoTSolutions, focused on practical device, firmware, and telemetry decisions.

Apply the idea

Need help turning this article into a working prototype plan?

If you are working through device architecture, connectivity, firmware structure, or dashboard scope, IoTSolutions can help turn the question into a cleaner build path.