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.

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.

Arduino Nightlight circuit diagram

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

arduino Nightlight Breadboard

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.
** www.hobbytronics.co.uk
*/

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()
{
  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  
  //delay(500);  
}

Tutorial 7 - Piezo Beep

Tutorial 9 - Power

Shopping Cart
0 items
 
DELIVERY/PAYMENT

   UK ORDERS OVER £50

UK - £2.40 orders below
                    £50.00
EU Airmail from £3.95
Worldwide from £7.95
More Details...

 
Specials
 
Brands
 
 Check out our videos