Skip to Content

Arduino Light Sensor – A Complete Guide

Arduino Light Sensor – A Complete Guide

This article will teach you how to interface an Arduino with a light sensor built on LDR.

LDR stands for ‘light-dependent resistor’.

These light sensors are helpful in various projects such as automated garden lighting, brightness monitoring, home automation and more. 

When you throw light on the LDR sensor, the resistance drops.

You can detect the drop in the resistance by using a simple voltage divider. 

In the following sections, we will learn about circuit diagrams and tips for choosing the right LDR for your applications. 

We will learn the types of light-dependent resistors and how to choose the suitable LDR for your application.

We will start with the step-by-step procedure to connect the LDR module to the Arduino UNO.

In the later sections, you will find the Arduino code and a collection of frequently asked questions about the LDR projects with answers.

Let’s get started!

Components Needed To Build Arduino And Light Sensor

Hardware Components

Software

Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com


Basics of Light Dependent Resistors

In this section, we will learn more about the LDRs, the typical specification of the LDR from a datasheet, and the basic circuits we can use to work with LDRs.

Light-dependent resistors are also known as photocells. The LDRs exhibit photoconductivity.

It means the conductivity of the photocells increases as you increase the intensity of the light falling on the LDR. 

You can use photocells in light-dependent applications such as light detectors, light-activated toggling circuits, and more.

The primary symbol of the photocell is shown below.

primary symbol of the photocell

When the light is incident, the resistance of the LDR can drop to a few 100 ohms.

The resistance is mega ohms when the photocell is in the dark.

generic LDR sensor

You can find the critical parts of a generic LDR sensor in the above image.

The photocell consists of a ceramic base onto which a photoconductive material is placed. 

A small distance separates two electrodes. There will be a clear coating on the top surface. 

In general, you cannot use LDR for applications where the requirement is to detect fast flashing lights.

The reason is that the LDRs have a significant latency of up to 10 ms.

The higher the area of the sensor the better will be the conductivity of the LDR.

Here is an image of three photocells with a reference.

three photocells

Let’s have a look at a datasheet of a photocell. 

  • Dark Resistance – 2 M Ohms – The minimum resistance you would measure when the photocell is subjected to no light. 
  • Illuminated resistance – 10 Lux at 2856 °K – The resistance you measure across the photocell when the photocell has 10 Lux of incident light on the surface.
  • Sensitivity – the sensitivity is listed as Ohms per Lux. The change in the resistance to a unit changes in the intensity of the light. 
  • Rise time – Time taken to switch from dark resistance to illuminated resistance.
  • Fall time – Time taken to switch from illuminated resistance to dark resistance.
  • Wavelength – The photocells are more sensitive to one particular light wavelength. The wavelength depends on the photosensitive material composition used to build the photocell.

You can find the light-sensitive sensor with a built-in comparator.

The comparator level can be adjusted by operating the potentiometer.

light sensor

In the light sensor above, all the critical parts are labeled.

Let us understand the parts in the section below. 

  1. The photocell – The photocell is the heart of the sensor module. The photocell should be aligned with the direction of the light to be detected. The sensor position is vital for the proper angle and overall system setup.
  2. Comparator module – The comparator module converts the resistance to a digital value. The comparator compares the voltage on the potentiometer and the voltage divider formed by the photocell and the resistor
  3. Potentiometer – You can tune the potentiometer to set the light sensitivity of the sensor module.
  4. A0 Pin – The analog output pin of the sensor module. You can use the analog output to take action based on multiple light intensities. For example, you may need to control the room light brightness based on the light brightness. 
  5. D0 Pin – The digital data pin outputs only two states, Logic high and logic low. You can use this to count the number of interruptions or just turn on or turn off garden lights based on the ambient light. 
  6. GND – The ground pin connects ground connections between Arduino UNO and the light sensor module.
  7. VCC pin – The power supply pin. 
  8. Power LED – Indicates the presence of a VCC connection.
  9. D0 LED – Indicates that the light intensity sensed is higher than the set limit

-> Read our guide about What You Can Build with Adruino.

Step-By-Step Instructions To Connect The LDR Sensor Module To The Arduino UNO

In this section, we will build a project using Arduino UNO and the LDR sensor module.

The LDR sensor module I am using provides an analog voltage.

You need to use an analog input pin of the Arduino.

I am using Pin A5 of the UNO to read the voltage value from the sensor.

Pin A5 of the UNO

In the order of labelling, the pins are 

  1. Signal (analog output of the sensor)
  2. Power Pin (connect to 5 V supply if you are using an Arduino UNO)
  3. Ground connection

How To Connect The LDR Sensor Module To The Arduino UNO?

Below is the step-by-step guide to complete the hardware connections needed to connect the Arduino and the LDR sensor module.  

Step 1: Start with the GND connections.

Start with the GND connections

You can connect any GND pins available on the Arduino UNO board.

Connect the ground pin on the LDR sensor to the GND pin of the Arduino. 

Always start with the ground connections. Connecting the GND pins first avoids accidental damage to the boards. 

Step 2: Connect the Signal Pin

Connect the Signal Pin

Connect the Arduino UNO’s pin A5 to the Signal pin of the module.

Step 3: Connect the Power pin

Connect the Power pin

Connect the Arduino UNO’s pin 5 V to the Power Pin of the LDR sensor module. 

Step 4: Verify the complete connections

Verify the complete connections

This completes the necessary connections. Congratulations.

In the next section, you will verify the connections made using the example Arduino code. 

-> Read our article about How Easy Is It To Learn Arduino?

Arduino Code Example For The Arduino And The LDR Sensor Project

In this section, we will walk through the example Arduino code to test the LDR circuit.

The first project tests the circuit that you have built in the previous section. 

In the later section, you will find one more interesting project that you can try.

Project 1: Display the light intensity on a serial terminal using LDR to sense the light

The Arduino code below reads the analog value. The analog value represents the amount of light falling on the LDR.

The analog voltage read increases as the light in the environment gets brighter. 

The equivalent circuit diagram needed to test the example project is here.

The Analog output of the sensor module is connected to the Pin A5 of the Arduino UNO.

The Arduino code is presented below.

/* Photocell simple testing sketch.
 
Connect one end of the photocell to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground
 
For more information see http://learn.adafruit.com/photocells */
 
int photocellPin = A5;     // the cell and 10K pulldown are connected to a5
int photocellReading;     // the analog reading from the analog resistor divider
 
void setup(void) {
  // We'll send debugging information via the Serial monitor
  Serial.begin(9600);  
}
 
void loop(void) {
  photocellReading = analogRead(photocellPin);  
 
  Serial.print("Analog reading = ");
  Serial.print(photocellReading);     // the raw analog reading
 
  // We'll have a few thresholds, qualitatively determined
  if (photocellReading < 10) {
    Serial.println(" - Dark");
  } else if (photocellReading < 200) {
    Serial.println(" - Dim");
  } else if (photocellReading < 500) {
    Serial.println(" - Light");
  } else if (photocellReading < 800) {
    Serial.println(" - Bright");
  } else {
    Serial.println(" - Very bright");
  }
  delay(1000);
}

Project 2: Simple discrete LDR sensor with LED indication 

The Arduino code below reads the LDR value using an Analog input pin.

The light intensity is measured using the LDR.

The darker the environment gets, the brighter the red LED.

The PWM pin of Arduino UNO is used to drive the LED pin at different brightness. 

Make sure you connect the LED to a PWM-compatible pin.

You can use the Arduino code below to test the functionality of the above circuit.

/* Photocell simple testing sketch.
 
Connect one end of the photocell to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground
Connect LED from pin 11 through a resistor to ground
For more information see http://learn.adafruit.com/photocells */
 
int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
int photocellReading;     // the analog reading from the sensor divider
int LEDpin = 11;          // connect Red LED to pin 11 (PWM pin)
int LEDbrightness;        //
void setup(void) {
  // We'll send debugging information via the Serial monitor
  Serial.begin(9600);  
}
 
void loop(void) {
  photocellReading = analogRead(photocellPin);  
 
  Serial.print("Analog reading = ");
  Serial.println(photocellReading);     // the raw analog reading
 
  // LED gets brighter the darker it is at the sensor
  // that means we have to -invert- the reading from 0-1023 back to 1023-0
  photocellReading = 1023 - photocellReading;
  //now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
  LEDbrightness = map(photocellReading, 0, 1023, 0, 255);
  analogWrite(LEDpin, LEDbrightness);
 
  delay(100);
}

FAQs About The LDR Sensor And Arduino Projects

I have included a list of the most frequently asked questions about projects built using Arduino and the LDR sensor modules.

If you have further questions, kindly post them in the comments section.

I will be glad to answer them. 

1. Can we use LDR with an Arduino?

You can connect an LDR sensor module directly with the Arduino UNO.

You need to connect power, ground, and an analog input pin.

You will also need a resistor to build the working project if you have a discrete LDR. 

2. What are the advantages of an LDR?

The light-dependent resistor is easy to use. You can use the LDR in the following applications.

  • Detection of sunlight (to turn on/off a garden light, To turn on street lights, etc.).
  • Enabling or disabling the security alarms (you can automatically disable the security alarms for the gate in the daytime and activate them at night.
  • Sheep pens gate control
  • Tracking the position of the sun so that the solar panels can be aligned very well with the sunlight for optimal solar power generation
  • Intruder detection
  • Items counter on a conveyor belt, etc. 

3. Does LDR have polarity?

No. LDR does not have polarity. You can treat them just like resistors. The LDR has two pins.

You can connect them in either direction.  

4. How does an LDR work?

The LDR works by changing its resistance when a different amount of light falls on it. The LDR works on the photoconductivity principle.

When you subject a photo-sensitive material to light, the conductivity of the material increases.

The higher the light’s intensity, the more the electrical conductivity will be. 

You use the same physical principle to detect light using LDRs in projects involving Arduino and a few other components to complete the circuit.

> Check out our guide to the Top 12 Best Arduino Online Courses

Conclusion

In this article, we understood the basics of an LDR, how the LDR responds to light, and the types of LDR. 

The LDR is simple, yet very versatile, as we saw in the list of applications where you can find LDR beneficial. 

You should now be able to follow the connection steps and complete a circuit like I showed for our demo project application.

The Arduino code presented is easy to straightforward to understand. 

Please post your questions on the LDR in the comments section.

Please let me know in the comments section if you have any trouble.

I am always happy to answer them.

If you have any feedback to improve the article, please share it in the comments section.

Your valuable input helps me to improve the article quality and bring more helpful articles down the line. 

Please remember to share the article with your fellow Arduino enthusiasts.