Arduino UNO Tutorial 8 – NightLight

Arduino UNO Tutorial 8 NightLight

In this Arduino UNO tutorial, we are going to use a Light Dependent Resistor (LDR) to create a simple childrens bedroom nightlight which turns on automatically when it gets dark and turns off when it gets light. An LDR’s resistance changes depending upon the amount of light hitting the sensor.  For the LDR we are going to use the resistance reduces as the light falling on the device increases. Used in conjunction with a 4.7K resistor this forms a simple voltage divider where the voltage across the LDR changes dependent upon the light.Arduino UNO Tutorial 8 NightLight

We can then input this into one of the Analog to Digital inputs in the Arduino to measure the voltage. Then its a simple matter of checking whether the value is above or below a threshold value and to turn one of the outputs on or off.

The LDR we are using is available here

The circuit diagram is shown below. As the light increases, the LDR’s resistance drops and hence the voltage across it drops. Thus the voltage across the resistor increases, so the voltage into the Arduino ADC increases. The opposite is true as it gets darker.

Here is the circuit laid out on a breadboard. 5V and 0V are taken from the Arduino. The input goes to pin A0

Below is the Arduino Sketch. In this sketch we are simply turning on the built-in LED if the ADC value drops below a specific value. To make a nightlight, a brighter led (with limiting resistor ~220 ohms) can be connected to the pin 13 output.

In the code you will notice that there are some serial output statements that are commented out. If you uncomment these you will see on the serial monitor the current value of the voltage being read by the Arduino ADC input. This value is between 0 and 1024. Cover the LDR with your hand and shine a light on it to see the effect.

Change the value in the code where the LED is switched on to an appropriate value.

** Nightlight LDR test program
** Created 06 Feb 2010
** This example code is in the public domain.

int sensorPin = A0;            // select the input pin for the ldr
unsigned int sensorValue = 0;  // variable to store the value coming from the ldr

void setup()

 Tutorial 8 NightLight Schematic

Read more: Arduino UNO Tutorial 8 – NightLight

Leave a Comment

Your email address will not be published. Required fields are marked *