How to Build a Night Light Circuit Using an Arduino

In this project, we will go over how to build a night light circuit using an arduino.
A night light circuit is a circuit which will turn on when nighttime comes, which is when it gets dark and the place could use some illumination.

It is a common household device that is sold by pretty much all the major retailers.
The device can sense when it gets dark and will switch on a light so that there can be some illumination.
The device we will build in this project will be automatic in nature. As soon as it gets dark, the LED attached to our arduino board will turn on right away, automatically.
For this project, the main component we will use is a photoresistor. When we place a photoresistor in series with a resistor, a voltage divider is formed. Voltage will be allocated to the 2 components based on the each one’s respective resistance. More voltage will fall across the component with the greater resistance, according to ohm’s law, V=IR. We will exploit this principle in this circuit to determine the light level that our circuit is exposed to and this will determine whether our LED turns on or offz

Components Needed for the Night Light Circuit

• Photoresistor
• 10KΩ Resistor
• Arduino

Night Light Circuit Schematic

The schematic for the night light circuit is shown below:

Code

The code to turn on an LED when the light level falls below a certain threshold is shown below.

const int ledPin=13; //the code will flash the LED connected to pin 13
const int sensorPin= 0; //Sensor pin connects to analog pin A0

int level; //the variable that will hold the light level reading

const int threshold=150; //this represents the threshold voltage. If voltage is below 150, this triggers the LED to turn on

void setup() {
pinMode (ledPin, OUTPUT); //sets digital pin 13 as output
Serial.begin(9600); //sets the baud rate at 9600 so we can check the values the sensor is obtaining on the Serial Monitor
}

void loop(){