LDR sensor with Arduino – How to use (with examples)

Learn how to use an LDR (Light Dependent Resistor), also known as a photoresistor. In this post we’ll go over the basics of the Light Dependent Resistor and an example on how to use an LDR to turn on a light when it’s dark.

An LDR or photoresistor is simply a resistor whose resistance varies depending on the amount of light that the LDR itself is exposed to. So in essence, the LDR is a light sensor capable of detecting the amount of light it’s subject to. This can be useful in applications in which we want to take a specific action under a specific lighting condition (turning on a light when it’s dark for example).

LDR Configurations

Light Dependent Resistors can be purchased in its stand-alone form, or as a module. For the stand-alone you will need a resistor to mimic the module, which comes with a built in resistor, allowing you to simply connect to the three pins (GND, Vp, VCC). Either way both configurations will take care of the same function. In this post, I will be using the module configuration.

LDR module

LDR Module Pins

If you are using the LDR module, then it’s important you know the pins.

VCC – Connected to main positive voltage on the circuit.

GND – Connected to ground on the circuit.

Signal (Vp) – Provides the signal associated with the amount of light (or darkness) to which the LDR sensor is exposed. Vp is simply a term I am choosing to use as I show on the circuit shown in the next section.

LDR module pins

LDR Circuit Basics

Applications of LDRs is mainly based on the concept of voltage division.

Voltage division: The main idea in voltage division is to generate different voltage values along a series of resistors. The simplest case is that of two resistances in series, which is actually the case for the LDR module circuit.

In the case of the LDR circuit voltage division, the input voltage is VCC (we will use 5V as the input), and the output will be measured at the location called out as “Voltage Probe” in the image.

We know the voltage drop across a resistor is equal to the resistance multiplied by the current flowing through it. We can use this to determine the equation used to calculate the voltage at Vp.

LDR Resistance Range

Light Dependent Resistors have a resistance that can go as high as 1 Megaohm (1,000,000Ω) when they are in complete darkness. When the LDR is under light, the resistance will drop considerably (more light, less resistance). I used a multimeter to take resistance measurements of the LDR under different set of lighting conditions.

I also when ahead and turned off most of the lights, which resulted in a substantial increase in the resistance of the LDR (it when all the way to 703 komhs).

LDR resistance range

LDR Resistance Range Analysis

Using the range for the LDR resistance of 0 kohms to 1,000 kohms, along with the previous equation, we can determine the expected range for Vp.

Max Light00
No Light1,000 kΩ4.95 V

The graph above assumes VCC = 5V.

LDR Example – Darkness Sensor

In this example we’ll go over how to make a darkness sensor. The idea is to turn on a light automatically when the lights go off.

Components used:
Arduino UNO
LDR Module
LED Flash (if you use a standard LED light, make sure to also add a resistor in front of it).
2N2222 transistor
220kohm resistor
4.7kohm resistor
3.3Volt source
12Volt source

LDR example (darkness sensor)

What is each component need for?

Arduino UNO:

Microcontroller used in this example (others could be used with minor changes). It’s used to take the analog signal coming from the LDR, and send a digital output to the 2N2222 transistor.

LDR module:

As previously mentioned, this component will change its resistance based on the amount of light it receives.

LED Flash:

Simply used to turn on and off based on the lighting conditions (detected by the LDR).

2N2222 Transistor:

In a previous post, I went over how to use the 2N2222 transistor to turn on a bright LED Flash on and off. In that post we turned on the LED through the transistor using a switch (push button) and using an Arduino UNO. Now, we will use the LDR to control the turning on/off of the LED. In this case the transistor is acting as a switch, with the LDR controlling whether or not current should flow through the transistor.

220kohmn resistor:

The main reason for adding this component is to improve the sensitivity of the LDR readings going to the Arduino UNO.

Net, we want to make sure that relatively small changes in brightness do not result in turning on the LED Flash. Take a look at the image showing the relation between Vp and the LDR resistance for the original scenario and the scenario after adding the 220kohm resistor.

The original curve shows the voltage Vp vs. RLDR with a step curve at the beginning, this meant that small changes in resistances (which means small changes in brightness) would have resulted in large changes in Vp voltage. The curve after adding the 220kohm transistor is not as steep, meaning voltage (and a result current too) will increase less abruptly relative to LDR changes.

4.7kohm resistor:

Needed to regulate the flow of current from the IO pin to the transistor base.

5Volt source:

This powers the LDR and allows the LDR output to have a range from 0V to 5V.

12Volt source:

Needed to power up the specific LED Flash that I am using for this example (which required an input voltage between 9V and 14.8V.

LDR example circuit diagram

Click to enlarge

Click to enlarge

LDR with Arduino breadboard diagram

Click to enlarge

*Note that the LED flash used in this example is a ready-to-use component with a built-in resistor, which is why a separate resistor is not needed.

Arduino Code

LDR Darkness Sensor – Sensitivity Control

When we think about sensitivity control, we are thinking about the brightness level at which we would want the LED Flash to turn on. This can be translated into an LDR resistance value at which we would want to turn on the LED Flash, which results in a voltage level for Vp. We then take Vp, and translated into an analog input value for the Arduino.

So, why did I pick 490 and what does that value represent?

The Arduino Analog values range from 0 to 1023, where 0 equals an input voltage of 0V, and 1023 corresponds to an input voltage of 5V. So the voltage for 490 corresponds to 2.39V (I wanted something around 2.4V, and 490 sounded like a better number than 491, which is actually closer to 2.4V).

The graph that was shown earlier shows the relation between Vp and RLDR in the orange curve. You can see that at 200kohms, Vp equals ~2.4V. You can also determine that from the equation we showed earlier (VCC being equal to 5V):

Click to enlarge

Now, why 200 kohms anyways?

Well, we can take a look at the resistance values as the darkness increases (brightness decreases). At a look at the picture when the resistances ~100kohm. It is my personal opinion that such brightness level wasn’t dark enough to warrant turning on the LED, so I picked 200kohm, assuming it would be dark enough.

The main point is, avoid picking a low resistance value, which would lead to the LED turning on when it’s still too bright (i.e. too sensitive).

So, how do you actually adjust the sensitivity?

Go to the Arduino code and change the number circled in the image below. The lower the number the more sensitive the setup will be.

Click to enlarge

Components used in this example

*As an Amazon & Ebay Associate I earn from qualifying purchases.

Arduino UNOhttps://amzn.to/3uYVAMC
Light Dependent Resistor (Module: KY-018)https://amzn.to/32oIjRu
Light Dependent Resistor (stand-alone)https://amzn.to/2RzGmzi
Super bright LED Flashhttps://www.superbrightleds.com/moreinfo/led-wired-bolts/little-dot-smd-led-accent-light/639/
Hilitchi Transistors Assortment Kithttps://amzn.to/3gf2nOl
Breadboard (Elenco 9440)https://amzn.to/3x23dnq
Amprobe AM-510 Multimeterhttps://amzn.to/32mV4Mf
Amprobe TL35B Test Leads with Alligator Clipshttps://amzn.to/3dp5m4M

Leave a Reply

Your email address will not be published. Required fields are marked *