Using an LDR Sensor with Arduino: A Tutorial for Beginners

By Arvind Sanjeev

Wouldn’t it be cool if we could eliminate darkness? In this Arduino project, I have posted a very simple project that focuses on eliminating darkness. Whenever a room gets dark due to a fused bulb or any other factors, a light bulb automatically turns ON. This can even be used as an emergency lighting system. It can be used to automatically turn a light ON whenever there isn’t sufficient light in a room.

In order to detect the intensity of light or darkness, we use a sensor called an LDR (Light Dependent Resistor). The LDR is a special type of resistor which allows higher voltages to pass through it (low resistance) whenever there is a high intensity of light, and passes a low voltage (high resistance) whenever it is dark. We can take advantage of this LDR property and use it in our DIY Arduino LDR sensor project.

Required Materials


  1. Arduino or an Arduino clone board (Freeduino), or make your own custom Arduino board.
  2. LDR (you can buy it online or from a local electronics store)
  3. 5V SPDT Relay
  4. 9V Battery and connector
  5. Connecting wires
  6. 100K resistor


Arduino IDE

How Does it Work?

This system works by sensing the intensity of light in its environment. The sensor that can be used to detect light is an LDR. You can buy it from any local electronics store or online for a very cheap price.
The LDR gives out an analog voltage when connected to Vcc (5V), which varies in magnitude in direct proportion to the input light intensity on it. That is, the greater the intensity of light, the greater the corresponding voltage from the LDR will be. Since the LDR gives out an analog voltage, it is connected to the analog input pin on the Arduino. The Arduino, with its built-in ADC (Analog to Digital Converter), then converts the analog voltage (from 0-5V) into a digital value in the range of (0-1023). When there is sufficient light in its environment or on its surface, the converted digital values read from the LDR through the Arduino will be in the range of 800-1023.


Arduino LDR Sensor working


Furthermore, we then program the Arduino to turn ON a relay. And correspondingly turn ON an appliance (light bulb), when the light intensity is low (can be done by covering the surface of the LDR with any object). That is, when the digital values read are in a higher range than usual.

Arduino LDR Sensor Connections

First of all, you need to connect the LDR to the analog input pin 0 on the Arduino. You have to use a voltage divider configuration to do this. The connection diagram for the Arduino is as given below.


Arduino LDR connections


One leg of the LDR is connected to VCC (5V) on the Arduino and the other to the analog pin 0 on the Arduino. A 100K resistor is also connected to the same leg and grounded.

Testing the Code for the Arduino LDR Sensor

After connecting the LDR to your Arduino, we can check for the values coming from the LDR via the Arduino. To do this, connect the Arduino via USB to your PC and open up the Arduino IDE or software. Next, paste this code and upload it to your Arduino:

int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
Serial.begin(9600); //sets serial port for communication
void loop() {
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.println(sensorValue); //prints the values coming from the sensor on the screen

After uploading the code, click the button on the Arduino IDE called “Serial monitor”. This will open a new window, which prints different values on the screen. Now, test out the sensor by blocking its surface from light and see what values you get on the serial monitor. This is how the serial monitor looks:


Arduino LDR Sensor – Serial Monitor


Connecting the Relay to the Arduino

A relay is an electromechanical switch; it can be used to turn an appliance ON/OFF working on AC/DC. When the Arduino supplies HIGH voltage (5V) to the relay, it turns it ON (the switch is ON), otherwise, it remains OFF.
In this project, we used a 5V SPDT (Single Pole Double Throw) relay. One terminal of the relay coil is connected to the Arduino’s digital pin 2 and the other end to GND. We connected a light bulb to it as well. Since we are dealing with high power AC voltages, do take proper precautions. If you are still confused about connecting a relay to an appliance, read this article on Relay logic. The overall circuit is shown below:


Arduino LDR Sensor and Relay – Connection Diagram


After connecting the Arduino as shown above, we need to test it by uploading the final code to our Arduino. The final sketch can be found here: Arduino code.

In this sketch, we set a threshold light value as 700, but it can vary for your projects. You will need to find out the particular value which the light bulb should turn ON. This needs to be done after testing it empirically. So basically, the Arduino turns ON the light bulb (via the relay) whenever the light intensity falls below 700. When it is above 700, it turns the light bulb OFF. Below is a video showing it in action:








Recommended Posts
Showing 22 comments
  • shikhar

    hello everyone can i get some help on that when the ldr detects darkness instead of bulb growing a beep sound or a some pattern of beep sounds arise up please suggest me the code and the way the what should i attach to my arduino and what the things are to be done so as on darkness beep sound appears up
    thanks in advance

  • Onur

    Hi, how can use arduino rgb led with sound sensor and LDR together. For example baby is cry at night it is working rgb led but daylight not working also sound sensor
    Please can you help me

    • pdegroote

      think of this pseudo-code:

      Switch off led;

      If i hear baby cry
      if it is dark then light led;
      switch off led;

      Now that should be easy to convert into some arduino logic.


  • bitsonhj

    Hi there..thnks for this great tutorial :).. I dont know if am doing something wrong..But here is the thing : When am testing the system without connecting anything via the relay, the system works fine..But when i connect a load(240V light), the light turn on/off at the same frequency as the rate of the LDR sensor input(which i see on the serial monitor).. I have try to remove the’delay’ which is at the end of ur sketch u have given us, but tht also wont change anything.
    Thnks in advance for getting back to me :)

    • Niels De Bock

      It blinks because calling digitalWrite(2,HIGH) when pin 2 is already HIGH, will make it turn off for a short period and then on again. To solve this only call digitalWrite(2,HIGH) when pin 2 is low.

      Example code:
      (I use a led light connected to pin 2 instead of a relay but same idea)

      int sensorPin = A0; // select the input pin for ldr
      int sensorValue = 0; // variable to store the value coming from the sensor

      int ledPin = 2; //Pin where my led light is connected
      bool prevLow; //Boolean indiciting if last value of ledPin was LOW

      void setup() {
      Serial.begin(9600); //sets serial port for communication
      pinMode(ledPin, OUTPUT);
      prevLow = true;

      void loop() {
      sensorValue = analogRead(sensorPin); // read the value from the sensor
      Serial.println(sensorValue); //prints the values coming from the sensor on the screen
      if(sensorValue 950){
      prevLow = true;
      digitalWrite(ledPin, LOW);

  • Jayanth

    How to covert the digital value we got in to candela? Thanks :)

  • mohammed

    can we program the arduino ,if i want to calculate how many hours that the LDR was taking light??

  • Abhishek Kumar

    When relay is turn on then ldr sensor value increase and decrease fast when relay is not connected then ldr sensor value is normal

  • Sanjay Kumar

  • E

    Is it safe to connect relay directly to arduino without transistor/diode?

  • madhu mms

    hlo sir … its works well for 1ldr then how do use more than 4 ldr in a single arduino

    • Arvind Sanjeev

      Just repeat the same steps as above with new pin numbers.

      • madhu

        hi boss I too need coding for 2digit 7segment arduini timing display and also circuit connection plz help me

  • fazz

    hello. how to make this circuit in a multisim?


  • balu

    nice information .thanks for providing this information

  • mr's ag

    My values at serial monitor are not constant, when i covered my ldr the value of light intensity at serial monitor are not constant, it’s continuously change to the impossible value. Why it’s happen and how to solve it?

    • Arvind Sanjeev

      Hello, this sounds like its a connection problem. Please double check your connections and the pin number you are using in the arduino code.

  • Hemant

    Hi Arvind,
    I am a regular visitor of I have also downloaded the ebook How to be a maker.
    Thanks a lot for sharing these projects (Arduino and Raspberry Pi) .
    I have a question regarding the Arduino clone board (Freeduino) I tried to search for Arduino Uno R3 compatible board but could not find it.
    Do you have any references?


    • Arvind Sanjeev

      Hello Hemant, thanks a lot for following DIY Hacking, also I couldn’t understand your question, the freeduino would work just like a normal arduino. Why would you want an R3 compatible board?

pingbacks / trackbacks

Leave a Comment

89 − 81 =

Start typing and press Enter to search