Arduino-Controlled Light Sensor
2016-07-26 | By All About Circuits
License: General Public License Arduino
Courtesy of All About Circuits
Light-dependent resistors (also known as photoresistors, photocells, or LDRs) are sensors that decrease their resistance as the amount of light that they’re exposed to increases. Essentially, the LEDs will get brighter as it gets darker around it and vice versa.
In this project, we are going to build a light sensor with an Arduino. The photoresistor will be connected to the Arduino’s analog input, allowing you to read the value with the analogRead() function. The LED will turn on and off based on the value read by the Arduino. PIN 3 will be set by the program to HIGH or LOW in order to turn the LEDs on and off. The threshold value for turning the LEDs on and off is 150. If the analog value is less than 150, the Arduino will turn the LEDs on. If the analog value is greater than 150, then the LEDs will turn off.
Wiring
Use the diagram below to connect the components accordingly. Pin 3 on the Arduino should connect to the LEDs through the breadboard. The 470 ohm resistors are used to limit the current. Connect one of the photo resistor’s leads to 5V. The other photo resistor’s second lead connects to the 1 kohm resistor. The other lead on the 1 kohm resistor will be connected to ground.
This will form a voltage divider, the output of which will be connected to pin A1 on your Arduino. As the light hitting the photoresistor increases, the resistance will decrease. Resistance will increase as less light hits the photoresistor.
Setup
1. Connect one of the two LDR terminals to 5V. Connect the other one to the GND through a 1 kohm resistor.
2. Connect one end of a wire to the analog pin labeled A1 and the other to the 1kohm resistor’s non-grounded terminal.
3. Connect the two LEDs so they sit parallel to each other on the breadboard, both through the 470 ohm resistor. The negative terminal should be grounded as shown in the circuit diagram above.
4. Connect pin 3 to the positive terminals on the LEDs through the 470 ohm resistors.
5. Connect all the grounded terminals to the GND pin on the Arduino.
6. Use the Arduino’s USB cable to a computer to upload the program using the Arduino IDE software.
7. Use the power supply, battery, or USB cable to power the Arduino board.
If the room is well-lit, the LEDs should not illuminate. Try getting them to turn on by covering the photoresistor with your hand. When you take your hand off of the photoresistor, the LEDs should turn back off.
You now have your own automatic lighting!
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
}
}
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum