Arduino LDR Sensor (Light Sensor) Interfacing

In this tutorial, you’ll learn how to interface Arduino with LDR Sensor (Light Sensor) and use it to detect darkness & light. We’ll create a couple of Arduino LDR code example projects to practice what we’ll learn in this tutorial. Without further ado, let’s jump right into it!

Table of Contents

  1. How LDR Works
  2. Arduino LDR Circuit Diagram (Interfacing)
  3. Arduino LDR Circuit
  4. Arduino LDR Code
  5. Arduino LDR Code Example – Light & Dark Detector
  6. Arduino LDR Code Example – Ambient Light Sensor
  7. Wrap Up

How LDR Works

Arduino-LDR-Sensor-Light-Sensor

An LDR (Light-Dependent Resistor) is an electronic device that is being used for light-intensity sensing applications. The LDR has a relatively low resistance in light, and when the surrounding gets darker the LDR’s resistance significantly increases.

An LDR can have a resistance of 2kΩ in daylight, 4kΩ in room light, and up to 200kΩ-1MΩ in darkness as you can see in the LDR characteristics curve below.

Arduino-LDR-Sensor

Using a DMM (digital multimeter), I managed to find out the resistance of my LDR sensor in average ambient room light and also when it’s completely blocked (darkness). It came around 3.5kΩ in room light, and about 200kΩ when it’s completely blocked (dark).

Arduino LDR Circuit (Voltage Divider)

In this tutorial, we’ll connect the LDR with a fixed-resistor in series to form a voltage divider network. And we’ll use the Arduino’s ADC to read the analog input voltage from the voltage divider’s midpoint using the analogRead() function. This is how we’re going to determine the ambient light intensity level using the LDR sensor with Arduino.

Arduino ADC (Analog Input)

The Arduino UNO has 6 analog input pins labeled from A0 to A5 as shown in the figure below. Those pins can be used with analog peripherals in the Arduino microcontroller such as ADC (A/D Converter) and the Analog Comparator.

Arduino-UNO-Pinout-Analog-Input-Pins-ADC

The Arduino ADC resolution is 10 bits, the digital output range is therefore from 0 up to 1023. And the analog input range is from 0v up to 5v (which is the default analog reference voltage VREF = +5v).

You can use the interactive tool below to set an analog input voltage and see the ADC digital output value that corresponds to the analog input voltage. The output equation for the ADC is as follows: ADC Output = ( Analog input voltage / VREF ) x (2n – 1). Where VREF = 5v and n is the ADC resolution which is 10bits.


0V
0

It’s Highly Recommended to check out the tutorial below to learn more about Arduino ADC. It’s a prerequisite for this Arduino LDR tutorial to help you understand the topic in more detail.

???? Also Read
Arduino ADC analogRead Tutorial

This tutorial is the ultimate guide for Arduino ADC & reading analog input voltages using the analogRead() function. You’ll learn, in-depth, everything about Arduino ADC, how it works, and how to make the best use of it with a lot of tips and tricks all the way through.


Arduino LDR Circuit Diagram (Interfacing)

Here is the wiring diagram for Arduino with an LDR light sensor. Note that I’m using a 10kΩ resistor in series with the LDR. The complete connection diagram is shown below.

Note that I’m using the A0 analog input pin to read the voltage level at the midpoint of the (LDR + 10kΩ Resistor) voltage divider network.

Arduino LDR Circuit

Arduino-LDR-Circuit-Diagram

Arduino LDR Code

Here is a test code example that reads the A0 analog input pin and prints the digital value to the serial monitor over UART. You can use it to check that your wiring and connections are all okay.

The higher the light sensed by the LDR, the higher the ADC reading you’ll see. The darker it gets around the LDR, the lower the ADC’s reading becomes.

❕ Note

To get the same behavior I’m getting you should make sure to connect the (LDR + 10kΩ Resistor) in the same way as shown in the wiring diagram above. You can however reverse the polarity (LDR goes to GND and the 10kΩ Resistor goes to +5v) But the behavior will be also reversed. So keep this in mind.


Arduino LDR Code Example – Light & Dark Detector

In this example project, we’ll use Arduino analog input with an LDR sensor to read the ambient light intensity and compare the result value against a specific threshold to differentiate between Darkness & Light. Accordingly, we’ll turn ON and OFF an output LED.

Code Example

Here is the full code listing for this example.

Code Explanation

First of all, we define the 2 IO pins used for the LED output and the LDR analog input voltage reading.

setup()

in the setup() function, we’ll set the pinMode to be output for the LED pin.

loop()

in the loop() function, we’ll call the analogRead() function to get the voltage reading of the LDR sensor. If the LdrValue is below a specific threshold (300), we’ll turn ON the output LED (dark detected). Otherwise, the LED will be turned OFF (light detected).

TinkerCAD Simulation

Here is the simulation result for this project on the TinkerCAD simulator. You can run it as is, or make a copy and add your own code and start running the simulation to see how it’s going to behave. But anyway, it does successfully detect Light & Darkness and the output LED is responding as it should be.

You can check this simulation project on TinkerCAD using this link.

Testing Results

Here is a demo video for testing this project on my Arduino UNO board.


Arduino LDR Code Example – Ambient Light Sensor

In this example project, we’ll use Arduino analog input with an LDR sensor to read the ambient light intensity and use it to control the brightness of an output LED. The darker the ambient room light gets, the stronger the LED brightness becomes.

Code Example

Here is the full code listing for this example.

Code Explanation

First of all, we define the 2 IO pins used for the LED output and the LDR analog input voltage reading.

setup()

in the setup() function, we’ll set the pinMode to be output for the LED pin.

loop()

in the loop() function, we’ll call the analogRead() function to get the voltage reading of the LDR sensor. Since the ADC resolution is 10 bits and the LDR reading range is (0-1024), we’ll have to scale it down before sending it to the PWM duty cycle output (which is 8 bits). That’s why I’m dividing the LdrValue by 4 (or left-shifting by 2 instead >>2).

TinkerCAD Simulation

Here is the simulation result for this project on the TinkerCAD simulator.

You can check this simulation project on TinkerCAD using this link.

Testing Results

Here is a demo video for testing this project on my Arduino UNO board.


Parts List

Here is the full components list for all parts that you’d need in order to perform the practical LABs mentioned here in this article and for the whole Arduino Programming series of tutorials found here on DeepBlueMbedded. Please, note that those are affiliate links and we’ll receive a small commission on your purchase at no additional cost to you, and it’d definitely support our work.

Download Attachments

You can download all attachment files for this Article/Tutorial (project files, schematics, code, etc..) using the link below. Please consider supporting my work through the various support options listed in the link down below. Every small donation helps to keep this website up and running and ultimately supports our community.


Wrap Up

To conclude this tutorial, we can say that you can easily interface Arduino with LDR light sensor to read the ambient light intensity and use that information for various Arduino projects. You can build on top of the examples provided here in this tutorial to create so many applications, you’re only limited by your imagination. Just let me know if you’ve got further questions or need any help implementing your own project.

If you’re just getting started with Arduino, you need to check out the Arduino Getting Started [Ultimate Guide] here.

And follow this Arduino Series of Tutorials to learn more about Arduino Programming.

For more projects & ideas, check this Arduino Projects resource page.

???? Also Read
Getting Started With Arduino Programming For Beginners

This is the ultimate guide for getting started with Arduino for beginners. It’ll help you learn the Arduino fundamentals for Hardware & Software and understand the basics required to accelerate your learning journey with Arduino Programming.


FAQ & Answers

What is LDR in Arduino?

An LDR (Light-Dependent Resistor) is an electronic device that is being used for light-intensity sensing applications. The LDR has a relatively low resistance in light, and when the surrounding gets darker the LDR’s resistance significantly increases.
We typically use LDR with Arduino as a light sensor to sense the ambient light intensity of the surrounding environment. It can be used to detect objects and color light reflection which need a little bit more tuning to get such applications to work with just an LDR.

How to use an LDR sensor with Arduino code?

To use LDR with Arduino, you need to connect it to a fixed-resistor in series to form a voltage divider network. Then, you can use the Arduino’s ADC to read the analog input voltage from the voltage divider’s midpoint using the analogRead() function. This is how you can determine the ambient light intensity level using the LDR sensor with Arduino.

How to read LDR with Arduino?

To read LDR light sensor with Arduino, you should follow the next steps:
1- Connect the LDR to a fixed-resistor (10kΩ) in series to form a voltage divider network.
2- Use the Arduino’s ADC to read the analog input voltage from the voltage divider’s midpoint using the analogRead() function.
3- Save the ADC reading in a variable ( LdrValue)
4- Check if the ( LdrValue) is above or below a specific threshold to differentiate between light & darkness. Or you can just use the reading however you want in your application to achieve the desired behavioral requirements

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment