Maker Pro

How to Use an LDR Sensor With Arduino

March 21, 2018 by Arvind Sanjeev

A simple project using an Arduino that automatically turn lights on when an LDR sensor detects darkness.

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. You can even use this as an emergency lighting system. Use it to automatically turn a light on when there isn’t sufficient light in a room.

Related Article: Arduino: Three Powerful Yet Overlooked Uses

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 that 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.

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. It's inexpensive, and you can buy it from any local electronics store or online.

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. Correspondingly, turn on an appliance (light bulb), when the light intensity is low (this 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, 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, you 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, be sure to 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.

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 at 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. Here's a video showing it in action.


Arvind Sanjeev

An interaction designer and engineer. Yahoo-Accenture had also awarded him as the "Most Promising Innovator".

Related Content



You May Also Like