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()
Arduino UNO Tutorial 8 - NightLight Schematic
  pinMode(13, OUTPUT);
  //Start Serial port
  Serial.begin(9600);        // start serial for output - for testing

void loop()
  // read the value from the ldr:
  sensorValue = analogRead(sensorPin);     
  if(sensorValue<400) digitalWrite(13, HIGH);   // set the LED on
  else digitalWrite(13, LOW);   // set the LED on

  // For DEBUGGING - Print out our data, uncomment the lines below
  //Serial.print(sensorValue, DEC);     // print the value (0 to 1024)
  //Serial.println("");                   // print carriage return  


For more detail: Arduino UNO Tutorial 8 – NightLight

This Post / Project can also be found using search terms:

  • arduino uno tutorial project
Clip to Evernote

Leave a Comment

(Spamcheck Enabled)

Read previous post:
1 LED Game with Arduino Uno and an RGB LED

Sometimes I just have those days where I really want to make something neat with the Arduino I have lying...

Scroll to top