Arduino LDR Sensor: Best Tutorial for Beginners

 

Arduino Beginners LDR Project

Arduino Beginners LDR Project

Wouldn’t it be cool if we could eliminate darkness? In this beginner arduino project, I have posted a very simple project that focuses on eliminating darkness.
What it does is that- whenever a room gets dark due to a fused bulb or any other factors, a light bulb automatically turns ON. This can be used even as an emergency lighting system. It can be used to automatically turn a light ON whenever there isn’t sufficient light in a room.
Here, in order to detect the intensity of light or darkness, we use a sensor called 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 high intensity of light. And passes a low voltage (high resistance) whenever it is dark. We can take advantage of this factor of the LDR and use it in our Arduino LDR sensor DIY project. Check this link to know more about LDR.

What are the stuff required to do this project? 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 very cheaply)
  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. Here, the sensor that can be used for detecting 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, greater the intensity of light, greater will be the corresponding voltage from the LDR. Since the LDR gives out an analog voltage, it is connected to the analog input pin of the arduino. The arduino, with its inbuilt ADC (Analog to Digital Converter) then converts the analog voltage (from 0-5V) into a digital value in the range of (0-1023). Thus, 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 the 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 of 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

Here, one leg of the LDR is connected to VCC (5V) on the arduino. And the other to the analog pin 0 of the arduino. A 100K resistor is also connected to the same leg and grounded.

Step 2: Testing the code for Arduino LDR Sensor

Now, after connecting the LDR to arduino, we can check for the values coming from the LDR via arduino. For this, connect the arduino via USB to your PC and open up the arduino IDE or software. Now, paste this code and upload it to 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 on 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, block its surface from light and see what all values you get on the serial monitor. This is how it looks:

Arduino LDR Sensor

Arduino LDR Sensor – Serial Monitor

 

Step 3: Connecting the relay to arduino

A relay is an electro mechanical switch; it can be used to turn ON/OFF an appliance working on AC/DC. Here, when the arduino supplies HIGH voltage (5V) to the relay, it turns it ON (the switch is ON), else it remains OFF. You can learn more about it in detail here.
In this project, we are using a 5V SPDT (Single Pole Double Throw) relay. One terminal of the relay coil is connected to arduino digital pin 2, and the other end to GND. We are connecting 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 now as shown:

Arduino LDR sensor project

Arduino LDR Sensor and Relay – Connection Diagram

 

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

In this sketch, we have set a threshold light value as 700. But it can vary for your projects, you need to find out the particular value below 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. And when it is above 700, it turns the light bulb OFF. Finally a video showing it in action:

 

 

Recommended Posts
Showing 17 comments
  • 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

38 − 35 =

Start typing and press Enter to search