Here we show how to turn ON OFF a LED with LDR and Arduino. This can be used to indicate that the brightness in a room(for example) has exceeded certain threshold amount and needs attention via the LED. Many times we need to a constant lighting condition and in such cases this can be useful.
In previous arduino tutorial Connect and Read Photoresistor with Arduino we showed only how to connect photoresistor(LDR) and how to print it on the monitor using Ardiuno IDE. We did not utilize any LED indicator which is useful in practical sense. So this tutorial is more practically useful. Also the reading of LDR involves analogWrite() function. As such you may want to read How to read analog signal with Arduino.
We will explain this tutorial via simulation in proteus and actual LDR, LED with arduino hardware construction.
The following figure just shows how an LDR(Light Dependent Resistor) looks like.
Circuit diagram to turn ON OFF a LED with LDR and Arduino
The following is the circuit diagram showing the connection of LDR and LED to the Arduino.
We have connected the LDR in the form of resistor network with the 10KOhm resistor. The voltage drop at the connection between the LDR and 10KOhm changes as the light amount falling onto the LDR changes. This voltage change is detected by the analog pin A0 of the Arduino. Internally this analog voltage signal gets converted into digital signal or values. Since the ADC(Analog to Digital Converter) of the Arduino is 10bit, the range of the amplitude is from 0 to 1023. This forms the input to the Arduino.
At the other side, we have connected a LED to the 11th digital pin of the Arduino. This LED is grounded via 220Ohm resistor at the other end. This is the output from the arduino.
Program Code/ Sketch for turning ON OFF a LED with LDR and Arduino
The program code or sketch to turn on/off the LED using the LDR is below.
int LED = 11;
int LDR = analogRead(A0);
if(LDR > 512)
At the top of the program code we have labelled the 11th digital pin as LED for easier program writing. In the setup function, we have made this LED pin as output using the pinMode function. Then we have used the Serial.begin() function and set it to 9600 baud rate. This is done to use the serial monitor for displaying the values(quantized analog magnitude) on the PC. This is not necessary for our main objective but to know what is current value read into the microcontroller and to know when it exceed a certain value and then turn on/off the LED we have used this function.
After that we have the main loop function. In here, we first read in the analog signal from pin A0 using analogRead function and store it in a integer(int) variable called LDR. Once we have the instantaneous value as LDR we can use it to perform action based on the value. In this case, we want to turn on or off the LED if the light amount exceeds certain amount. This light amount translates into voltage read at A0 which then translates to LDR. So we can now specify the condition to turn on or off the LED. This is done using the if else statement. In the program, if the light amount = voltage is greater than 512 then we want to turn on the LED. This is done using the if statement and using digitalWrite function we send HIGH value, in other words 5V to turn on the LED. Else we don’t want to turn on the LED which we specified using the else statement. The important thing to remember here is that we need to use the else statement, otherwise the LED will remain ON after it is turned ON although the value may be lower than 512.
Proteus Simulation for turning ON OFF a LED with LDR and Arduino
Now before we show the actual hardware and how LDR and LED works in real time we show the Proteus simulation for the same.
Video Demonstration of How to turn ON OFF a LED with LDR and Arduino
The following video shows how this actually works with real LDR.