Introduction: Interfacing Light Dependent Resistor With Arduino Uno
The light dependent resistor (LDR)or photoresistor are a special type of variable resistor whose resistance changes depending on the lighting levels. In a dark environments, the resistance is very high, while it decreases as the light levels increases.
LDRs are useful in applications like detecting black spots, lighting condition dependent control systems, and even security systems. This project focuses on the fundamental use of a light dependent resistor and provides the freedom to the user to use it as they please. It will be use to control a pseudo-night-light control system that gets feedback from the surrounding environment's lighting levels. Then, given a dark room, turns on an LED light. In this case, a mellow blue colour.
Step 1: Tools and Materials
- Arduino Uno
- LDR
- 10kΩ Resistor
- 5mm Blue LED
- Jumper Cables
- 100Ω Resitor
Step 2: Connecting the LDR and LED to the Arduino
The LDR is the analog input that will be feeding sensor data into the Arduino. While, the LED is the user feedback, i.e the digital output that is controlled by the micro controller given certain input signals provided by the LDR.
In order to use the changes in resistance as a signal for the Arduino, we will be taking advantage of Ohm's law, the relationship between voltage, current, and resistance -- V = IR using a voltage divider.
- To make a voltage divider, connect the one of the LDR's pin to A0 of the Arduino.
- Then connect this same pin to a 10kΩ resistor in series to the common ground rail of the breadboard.
- Supply power to the LDR by connecting the other pin to the red rail of the breadboard.
- Then connect the LED's cathode, negative pin to the common ground rail of the breadboard.
- Connect the anode, positive pin, of the LED to the red rail of the breadboard.
- Lastly, power up the circuit by connecting the 3.3V power from the Arduino tot he red power rail of the bread board and complete the circuit by connecting the GND pin of the Arduino tot bhe common ground rail in the breadboard.
Step 3: Code
const int sensorPin = 0;<br>
const int ledPin = 9;
int lightCal; int lightVal;
void setup() { // set up the LED pin to be an output. pinMode(ledPin, OUTPUT); lightCal = analogRead(sensorPin); //we will take a single reading from the light sensor and store it in the lightCal //variable. This will give us a prelinary value to compare against in the loop }
void loop() {
//Take a reading using analogRead() on sensor pin and store it in lightVal lightVal = analogRead(sensorPin);
//if lightVal is less than our initial reading (lightCal) minus 50 it is dark and //turn pin 9 HIGH. if(lightVal - lightCal < 50) { digitalWrite (ledPin, HIGH); }
//else, it is bright, turn pin 9 LOW else { digitalWrite (ledPin, LOW); }
}
Step 4: Demo
When the room is dark in this case using the shadows produced by my hand to block the light, then LED turns on, otherwise, it remains off. This behaviour is exactly the same as a closed loop automatic night light system.