How to control LED using LDR using Arduino | applied electronics engineering


How to control LED using LDR using Arduino

By Applied Electronics - Saturday, January 28, 2017 No Comments
Here we show how to control LED using LDR using Arduino. LDR(Light Dependent Resistor) is a sensor that can be used to detect amount of light. We can use it to detect light amount in room or similar places. To detect whether or not the light amount is above a certain threshold we can use a LED to indicate it. In the previous tutorial we showed How to test photoresistor(LDR) with Arduino Video demonstration. Here we show how to control LED using LDR using Arduino with program code.

Circuit Diagram

The schematic for this is shown below.

circuit diagram for how to control LED using LDR using Arduino

The LDR or photoresistor is connected with another resistor of 10KOhm. These two resistors form a voltage divider network. The voltage at the middle of the network is fed into the analog pin A0 of the arduino. When the amount of light changes in the LDR the voltage will change and this is detected using the ADC(Analog to Digital Converter) inside the Arduino microcontroller. The analog voltage quantized digital values ranges from 0 to 1023 because the ADC is 10 bit. At any instant the value read at the analog pin can be compared with certain value, say 512. If the instant value is greater than 512 then we will turn on a LED. The LED here is connected to the 11th digital pin as shown in the figure above.

Program Code

The program code that allows us to read the analog signal, convert them to digital values, compare and turn ON/OFF the LED is below.

int myLight = 11;
void setup()

void loop()
   int myLDR = analogRead(A0);

   if(myLDR > 512)
    digitalWrite(myLight, HIGH);
      digitalWrite(myLight, LOW);


In the program code above, myLight is set to pin 11. Following that in the setup function we have set this myLight to an output mode because it is used to send the digital signal to the LED. The Serial.begin(9600) is required to set up the serial data transfer from Arduino to PC for serial monitoring the value read into the arduino. In the main loop, we have first declared a variable myLDR. The analogRead(A0) function reads in the analog signal value at pin A0. How to read analog signal into Arduino explains in details how to use this analogRead function. The instantaneous analog signal that are read in are saved into the variable myLDR. Now we can manipulate the variable. The Serial.println(myLDR) for example is used to display the values read into the microcontroller on the serial monitor(see the video below). Then next we have used the myLDR to compare with 512. If it is greater than 512 we will turn on the LED else we will keep it off. This requires us to use the digitalWrite function.

Video Demonstration

Now the following video shows how it actually works. You will see that the LED will turn ON if the amount of light is increased otherwise it will stay off.

Tips and Conclusion

In this way we demonstrated how you can turn on and off a LED according to amount of light using LDR(Light Dependent Resistor). We also gave you the program code or sketch in arduino language for doin this. We can further modify the code and circuit so that we can change the brightness of the LED according to amount of light falling on the LDR. This requires us to use the PWM technique. PWM(Pulse Width Modulation) is used to send different length of digital signal which corresponds to amplitude voltage level. We have shown this for controlling a DC motor in How to control DC motor using PWM from Arduino. This is not only possible for LED but we could use it to control the light from a larger bulb. Such techniques can be used to automatically control brightness in certain places, for example a room or a box. For example, there are certain experiments where constant brightness is required. In such cases this control technique can be used.

No Comment to " How to control LED using LDR using Arduino "