Arduino LDR Sensor: Best Tutorial for Beginners

Arduino Beginners LDR Project

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. Check this link to learn more about LDR.

Required Materials

Hardware:

  1. Arduino or an Arduino clone board (freeduino), or make your own custom Arduino board with this tutorial.
  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

Software:

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. For more info on the LDR, check this out: LDR/photoresistor.

Arduino LDR Sensor

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.

Step 1: 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 Beginners LDR Project

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.

Step 2: Test 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
delay(100);
}

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

Arduino LDR Sensor – Serial Monitor

 

Step 3: Connect the Relay to the Arduino

A relay is an electromechanical switch; it can be used to turn an applianceON/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. You can learn more about it in detail here.
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, check this link. The overall circuit is shown below:

 

Arduino LDR sensor project

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 19 comments
  • Charlie Sampson
    Reply

    i love boys

  • Brandon Simmons
    Reply

    im gay

  • bitsonhj
    Reply

    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
      Reply

      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);
      }
      delay(500);
      }

  • Jayanth
    Reply

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

  • mohammed
    Reply

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

  • Abhishek Kumar
    Reply

    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
    Reply

    https://www.youtube.com/watch?v=S-QddCWt5l4 THIS VIDEO IS FOR BEGINNER

  • E
    Reply

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

  • madhu mms
    Reply

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

    • Arvind Sanjeev
      Reply

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

      • madhu
        Reply

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

  • fazz
    Reply

    hello. how to make this circuit in a multisim?

    thanks.

  • balu
    Reply

    nice information .thanks for providing this information

  • mr's ag
    Reply

    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
      Reply

      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
    Reply

    Hi Arvind,
    I am a regular visitor of http://diyhacking.com/. 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?

    Thanks,
    Hemant

    • Arvind Sanjeev
      Reply

      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
  • […] This system senses the light intensity by using photoresistors or LDR (Light Dependent Resistors) on the grove light sensor. The edison recognises the intensity of the light coming from the sesnsor via the grove shield. Which is then tweeted via twitter. For more info on photoresistors, click here . And to build something using it, check this tutorial. […]

Leave a Comment

9 + 1 =

Start typing and press Enter to search