Here I will show how to make Automatic Night Light using LDR Relay ESP8266.
We are nowadays want everything automated. Automatic Night Light is a very popular device. The light will glow in the night or when the area is dark. In the day time or bright condition light will be off. Lets start Automatic Night Light using LDR Relay ESP8266 NodeMCU IoT board.

Components Used:(Click to Buy)

Connection Setup for Automatic Night Light using LDR Relay ESP8266

How to connect 220V Ac with Relay

To Connect with AC line we need a small wire. The neutral line will be connected directly to the bulb. Live wire first go to COM port of Relay. Then one small wire will be connected to the bulb from Normally Open(NO) Port. In the following diagram, we have shown a battery instead of the AC line for your understanding.

How to connect NodeMCU with Relay

Connection with ESP8266 NodeMCU is a very simple one. Just we have to connect 3.3V to the VCC and Ground to the GND. Then we have to connect the data line. Here we are using D4 pin. So we have used pin value as 2.


If you want to control Light via WiFi in NodeMCU, see this post.

Home Automation Using ESP8266 NodeMCU with Relay

Connection Diagram for Automatic Night Light using LDR Relay ESP8266

You can connect seeing the following diagram. Just replace the LED with your 220V bulb & Battery with 220V AC line.

LDR Relay ESP8266

I have connected 10K Ohm register with the LDR as a voltage divider. If you connect 3V led then use 220 Ohm register with the LED. If you use AC line, then no registor needed.

Code Explain of Automatic Night Light using LDR Relay ESP8266

The important thing in this code lies in line no 17. After determining the analogue value in dark condition on line no 24, set that value in the place of 650 on line 17.

The bulb will be on if outside area is dark as the code written on line no 18.

Arduino Sketch for Automatic Night Light using LDR Relay ESP8266

/*
https://somtips.com
https://youtube.com/somtips
*/
const int ledPin = 2;
const int ldrPin = A0;
void setup() {

Serial.begin(9600);

pinMode(ledPin, OUTPUT);

pinMode(ldrPin, INPUT);
}
void loop() {
int ldrStatus = analogRead(ldrPin);
if (ldrStatus < 650) {
  digitalWrite(ledPin, HIGH);
  }
else {
  digitalWrite(ledPin, LOW);
  }
//checking value
Serial.println(ldrStatus);
delay(500);
}

Output LDR Relay ESP8266

LDR Relay Arduino

After all the connection done, you will notice the 220V Bulb will switch on/off depending upon present light condition.