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
- Light Sensor
- Automatic Night Light
- Blynk
- Relay
- IOT
- Automation
- Published
- Updated
- Reading time
- 2 min read
- Author
- Saroj Chaudhary
- Role
- IoT & Embedded Systems Engineer
This project uses an ESP8266 and LDR sensor to detect ambient light and automatically control a lamp.
When the surroundings become dark, the ESP8266 turns the light ON through a relay. When enough daylight is available, the light turns OFF.
The current LDR value and light status are also sent to Blynk, where the user can monitor the system and manually override the light when needed.
Components Required
| Component | Quantity |
|---|---|
| ESP8266 NodeMCU | 1 |
| LDR / Light Sensor Module | 1 |
| Relay Module | 1 |
| Low-voltage DC Bulb / Lamp | 1 |
| Suitable DC Power Supply | 1 |
| Breadboard | 1 |
| Jumper Wires | As required |
For classroom testing, use a low-voltage DC lamp instead of exposed AC mains wiring.
How It Works
Ambient Light
|
v
LDR
|
v
ESP8266
/ \
v v
Relay Wi-Fi
| |
v v
Light Blynk
Automatic mode:
Dark environment -> Light ON
Bright environment -> Light OFF
Manual mode:
Blynk switch -> User controls light directly
Circuit Connections
LDR Sensor to ESP8266
| LDR Module | ESP8266 |
|---|---|
| VCC | 3.3V |
| GND | GND |
| AO | A0 |
This project uses the analog output (AO) so the ESP8266 can measure different light levels.
Check the supported A0 voltage range of your specific ESP8266 board before connecting an analog signal.
Relay to ESP8266
| Relay | ESP8266 |
|---|---|
| IN | D1 / GPIO5 |
| 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 |
|---|---|---|
| LDR Value | V0 | Displays light level |
| Light Status | V1 | Shows lamp ON/OFF |
| Manual Light | V2 | Manual lamp control |
| Automatic Mode | V3 | Selects Auto/Manual |
Suggested logic:
V3 = 1 -> Automatic Mode
V3 = 0 -> Manual Mode
ESP8266 Code
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "YOUR_TEMPLATE_ID"
#define BLYNK_TEMPLATE_NAME "Automatic Night Light"
#define BLYNK_AUTH_TOKEN "YOUR_AUTH_TOKEN"
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char ssid[] = "YOUR_WIFI_NAME";
char pass[] = "YOUR_WIFI_PASSWORD";
const int LDR_PIN = A0;
const int RELAY_PIN = D1;
const bool RELAY_ACTIVE_LOW = true;
// Adjust this value after testing your LDR.
const int DARK_THRESHOLD = 600;
bool autoMode = true;
bool manualLightRequest = false;
bool lightOn = false;
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 readLight()
{
int ldrValue = analogRead(LDR_PIN);
if (autoMode)
{
// Change the comparison direction if your LDR module behaves opposite.
if (ldrValue >= DARK_THRESHOLD)
setLight(true);
else
setLight(false);
}
else
{
setLight(manualLightRequest);
}
if (Blynk.connected())
Blynk.virtualWrite(V0, ldrValue);
Serial.print("LDR: ");
Serial.print(ldrValue);
Serial.print(" | Mode: ");
Serial.print(autoMode ? "AUTO" : "MANUAL");
Serial.print(" | Light: ");
Serial.println(lightOn ? "ON" : "OFF");
}
BLYNK_WRITE(V2)
{
manualLightRequest = param.asInt();
if (!autoMode)
setLight(manualLightRequest);
}
BLYNK_WRITE(V3)
{
autoMode = param.asInt();
if (!autoMode)
setLight(manualLightRequest);
}
void setup()
{
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
setLight(false);
Blynk.begin(
BLYNK_AUTH_TOKEN,
ssid,
pass
);
timer.setInterval(1000L, readLight);
Serial.println("Automatic Night Light Started");
}
void loop()
{
Blynk.run();
timer.run();
}
Calibration
Open Serial Monitor and observe the LDR reading in:
Bright condition
Dark condition
Then choose a threshold between the two values.
Example:
const int DARK_THRESHOLD = 600;
If your sensor works in the opposite direction, reverse the comparison inside readLight().
Testing
Bright Condition
Expected result:
Light -> OFF
Blynk -> LDR value visible
Dark Condition
Cover the LDR or reduce the surrounding light.
Expected result:
Light -> ON
Blynk -> Light Status = 1
Manual Mode
Set:
Automatic Mode -> OFF
Then use the Manual Light switch in Blynk to turn the lamp ON or OFF.
Common Problems
Light turns ON during daytime
Adjust DARK_THRESHOLD or reverse the comparison logic.
Relay works in reverse
Change:
const bool RELAY_ACTIVE_LOW = true;
to:
const bool RELAY_ACTIVE_LOW = false;
LDR value does not change
Check the A0 connection, sensor power, and Serial Monitor readings.
Blynk does not update
Check Wi-Fi credentials, Blynk credentials, and Virtual Pin configuration.
Final Result
The completed project provides:
- automatic day/night detection
- automatic light control
- live LDR monitoring
- light-status monitoring
- manual override from Blynk
- Auto/Manual operating modes
Daylight -> Light OFF
Darkness -> Light ON
Blynk -> Monitor + Manual Override
Safety Note
Use a low-voltage DC lamp for 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.