Monitoring the output of a light-dependent resistor, or photoresistor, allows the Arduino to know how light or dark it is. When the light falls below a certain level, the Arduino turns on a couple of LEDs.

A light-dependent resistor, or photoresistor, is a sensor whose resistance decreases as the amount of light falling on it increases. When it is dark, the resistance of a photoresistor may be as high as a few MΩ. When it is light, however, the resistance of a photoresistor may be as low as a few hundred ohms. 

In this experiment, we will connect a photoresistor to an Arduino analog input and read the value with the analogRead() function. Depending on the value the Arduino reads, the program will then set pin 3 HIGH or LOW to turn on or turn off the LED night lights. The threshold value is 150. When the analog value read is less than 150, the Arduino will turn the LEDs on. When the analog value it reads is below 150, the Arduino will turn the LEDs off.


Hardware Required

  • 1 x photoresistor
  • 2 x LED
  • 2 x 470 ohm resistors
  • 1 x 1 kohm resistors
  • 1 x Arduino Mega2560
  • 1 x breadboard
  • jumper wires

Wiring Diagram

You connect the components as shown in the diagram below. Connect the LEDs to pin 3 of the Arduino. The two 470 ohm resistors are current limiting resistors. One lead of the photo resistor is connected to 5V, the other to one lead of the 1 kohm resistor. The other lead of the 10 kohm resistor is connected to ground. This forms a voltage divider, whose output is connected to pin A1 of the Arduino. As the light impinging on the photoresistor gets stronger, the resistance decreases, and the voltage output of the divider increase. The reverse happens, when the impinging light gets weaker.

Here is a photo ​of the setup:


Setup

  1. Connect one of the two terminals of LDR to 5V and other one to the GND through a one kilo ohm resistor.
  2. Connect one end of a wire to analog pin A1 and other to 1K resistor’s non-grounded terminal.
  3. Connect two LEDs in parallel, both through a 470 ohm resistor and with negative terminal grounded as shown in above circuit diagram.
  4. Connect pin 3 to the positive terminals of LEDs through 470 ohm resistors.
  5. Connect all the grounded terminals to Arduino’s GND pin.
  6. Connect the Arduino using Arduino USB cable and upload the program to Arduino using Arduino IDE software.
  7. Provide power to the Arduino board using power supply, battery or USB cable.
If the room is lighted, the LEDs should not light. Try getting them to turn on it by covering the photoresistor with your hand. Remove your hand and observe that they turn off again.


Code

                    const int led=3; // variable which stores pin number

void setup() 
{
  pinMode(led, OUTPUT);  //configures pin 3 as OUTPUT
}

void loop() 
{
   int sensor_value = analogRead(A0);
  if (sensor_value < 150)// the point at which the state of LEDs change 
    { 
      digitalWrite(led, HIGH);  //sets LEDs ON
    }
  else
    {
      digitalWrite(led,LOW);  //Sets LEDs OFF
    }

}
                  

  Download Code  


Video


Comments

1 Comment


  • chopnhack 2016-11-18

    It obviously works, however, most mcu’s ADC inputs require no more than 10k ohm impedance for optimum results. Shouldn’t a buffer, or a comparator be used between the mcu and input? Why did you omit that in your design?
    Thanks.

    • Robert Keim 2016-11-22

      This is a valid point, but in this case a buffer is not necessary because the project does not require a high analog-to-digital sampling rate.