Arduino Projects

Photoresistor | LDR Arduino based Projects

Photoresistor Overview:

Photoresistor/LDR Arduino based Projects– Light sensors, as the name suggests, are sensors that allow detecting the presence of light in the environment. They are sometimes called “cells CdS ”(for the material with which they are usually manufactured, cadmium sulfide) or also “Photoresistors” and LDRs (from English “Light Dependent Resistor ”), since it basically consists of a resistor that changes its value depending on the amount of light that is impacting on its surface. Specifically, reduce their resistance as they receive more Light intensity.

They are usually small, cheap, and easy to use; This is why they appear a lot in toys and household devices in general. But they are imprecise: each photoresistor reacts differently to one another. That is why they should not be used to determine exact levels of intensity of light, but rather to determine variations in it, which They can come from the ambient light itself (“dawn or dusk”) or from the presence of an obstacle that blocks the reception of any incident light. You could also have a photoresist system and thus compare which of them receives more light at a certain time (to build for example a robot follower of paths painted white on the ground or of light bulbs, among others many applications).

Another thing to know is that its typical response time is in the order of a tenth of a second. This means that the variation of its value resistive has that delay with respect to light changes. Therefore, in circumstances where the light signal varies rapidly, its use is not very indicated (although It is also true that this slowness in some cases is an advantage, because that way filter out rapid lighting variations).

When purchasing a photoresistor, you must also take into account another series of factors: apart from size and price, above all you also have to look at the maximum and minimum resistance that they can offer. We can do this data Obtain from the datasheet provided by the manufacturer. In fact, in the datasheet not only we can consult these two extreme data but also all the values intermediate resistance, With this information we can know, knowing the light that falls on the LDR, what value resistance offers this (and vice versa: knowing the resistance it offers, we can deduce the amount of light the sensor receives).

It is also mentioned that this variation is “generally logarithmic”; it’s possible that there are cases where it is not, but what is certain is that, by the intrinsic nature of photoresistors, the relationship between amount of light received and Resulting resistance will never be linear. This means, for example, that if the light intensity is 10 lux and resistance of 100Ω is measured when this is 20 lux the resistance need not be 50Ω.

Another piece of information to consult in the datasheet to take into account is sensitivity. Photoresistors do not detect different types of light in the same way. light; specifically, they tend to be more sensitive to changes in green lights than in those of other colors. In addition, there are minimum wavelengths (400nm, normally) and maximum (600nm, normally) beyond which they do not detect any. This information can be found in the form of a graph that shows the photoresistor response as a function of received wavelength.

On the other hand, the voltage accepted by these devices can be practically any (up to 100 V). Because photoresistors are not more than resistors, they are not polarized, so their terminals can be connect in our circuits in both directions.

The easiest way to check that a light sensor works is to connect its terminals to a multimeter in resistance measurement mode and make it influence more or less light. If we see that it responds (you have to watch with the change of scale), we can start designing our projects with him.

Photoresistor or LDR Connection with Arduino: 

The first thing we have to know is how a light sensor is connected to our circuit. What we will do is connect one of the sensor terminals to the supply and the other, through a “pull-down” resistor, to ground. Also, since a point between the photoresistor and the “pull-down” resistor we will connect a “yellow wire ” to an analog input of our Arduino board so that it can read the analog voltage to be measured. This received voltage may range between 0 V and the voltage that supplies the photoresistor: in the following figures we will show the photoresistor fed with the 5 V of the pin “5V” of the Arduino board, but also it could be supplied perfectly with the 3.3 V of the “3V3” pin, for example.

If we use a “pull-down” resistor, the higher voltage we receive through the Analog input from the Arduino board will mean more light is hitting the sensor. Yes, we would have used a “pull-up” resistor, it would be the other way around: the higher the voltage received it would mean there is more darkness. as we have commented, we will use a “pull-down” resistor, so, ultimately, the assembly would be similar to this:

Photoresistor or LDR Arduino based Projects

The trick to understanding this montage in depth is to see that as you the resistance of the photoresistor decreases (because more light falls on it), the resistance The total set of photoresistor + pull-down series resistors also decreases. By Ohm’s Law, this will cause (holding a fixed supply voltage at the whole circuit –5 V–) the intensity of the current increases through all that circuit. But since the pull-down resistance is fixed, by the same Ohm’s Law, if the current through it has increased, so will the voltage between its terminals. Which is in fact what we measure with the “yellow wire”: the voltage between the terminals of the pull-down resistor. If the resistance of the photoresistor were negligible because there was a lot of light, that measured voltage would be 5 V; Yes the resistance of the photoresistor was so great that it would open the circuit interrupting the passage of electrons -being very dark-, the measured voltage would be 0 V. Between both extreme cases we will have intermediate measures.

photoresistor or LDR explained  By Ohm’s Law:

What is explained in the previous paragraph can be summarized in the following formula, obtained from Ohm’s Law and from the fact that the intensity through both resistors is the same:

Vmed = (Rpull / (Rpull + Rphoto)) · Vsource

where Vsource is the voltage supplied by the power supply, Vmed is the voltage received by the input pin.

analog input (that is, the one between the terminals of the “pulldown” resistor, which can be between 0 V and Vsource), Rpull is the value of the “pulldown” resistor (fixed) and Rphoto is the value of the resistance of the photoresistor. From here you can check what we have already said: that when the light intensity increases (as the resistance of the photoresistor decreases), the measured voltage also increases, and vice versa.

However, in reality, Vmed is not the value with which we work in our Arduino board, because it always uses an analog-digital converter to perform a mapping of all received analog values ​​(which can range from 0 V to 5 V assuming that the voltage provided by the source is 5 V) at values digital (ranging between 0 and 1023).

These digital values ​​are what the Arduino board really understands and with whom we will work in our sketches. The correct The news is that the conversion of analog to digital values ​​can be expressed by a simple proportionality rule like this: Vconverted = Vmed · 1023/5. From here, yeah We substitute this expression in the formula of the previous paragraph, and we clear from there Rphoto, we arrive at the following expression: Rphoto = (Rpull · 1023 / Vconverted) – Rpull, which we finally allows to know what is the current value of the resistance of the photoresistor to starting from the digitized voltage obtained by the Arduino board.

To know the appropriate value of the pull-down resistor we have to place in our circuit, we would have to know (from the datasheet) the different specific numerical values ​​that our photoresistor can acquire at over different illuminations (Rphoto) and use them, together with the Rpull value obtained, in the formula already seen on the previous page Vmed = (Rpull / (Rpull + Rphoto)) · Vsource to see what hypothetical Vmed we would get. By doing this, we will see that in the In the vast majority of cases, Rphoto ‘s behavior with respect to lighting received causes high Rpull values ​​(for example, 10 KΩ) to quickly saturate readings in bright environments.

That is, it causes the sensor to measure the top of 5 V in relatively low light and is therefore not capable of distinguish between a well-lit environment from a very well-lit one. Instead, lower values ​​of Rpull (such as 1KΩ), will allow detecting changes in the brightest light but will make it impossible to distinguish differences in levels dark. Therefore, depending on the environment where we place our project, we must choose a Rpull of 10KΩ (for dark environments) or 1KΩ (for environments illuminated), or use some type of adjustable potentiometer.

Amazon Purchase links


LDR Module 

Arduino Uno:

Mega 2560: 

lm7805 Voltage Regulator:

330-ohm resistors pack: 

female DC power jack socket: 

470uf capacitors: 

5×7 cm Vero board: 

female headers: 

connection wires: 

Super Starter kit for Beginners: 

Jumper Wires: 

Bread Board: 

12v Adaptor: 

PCB plate: 

Variable Supply: 

Digital Multimeter: 

Vero Board / stripboard: 

Soldering iron kit: 

Solder wire: 

Wire Stripper: 


PCB drill machine: 


Please Note: these are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Photoresistor/LDR Arduino based Projects

project1: how to use photoresistor / LDR with Arduino:

Let’s see how a photoresistor behaves when it is connected to an Arduino board. For this we can assemble the following circuit; Note that the LED has to be connected to a PWM pin.

Photoresistor or LDR Arduino based Projects

Or  you can used ldr module, because ldr module have built-in resistor so no need to wire the resistors

Photoresistor or LDR Arduino based Projects


What we want is to use the value of the voltage read at the input pin analog (number 0 in this case) to illuminate the LED accordingly: how much The less light the photoresistor detects, the brighter the LED will illuminate (that’s why It is necessary for the LED to receive an analog signal as well, through a PWM pin, the 5 in this case). In addition, the values ​​obtained by the photoresistor.


Program explanation:

To Obtained the ldr value I initialize

then I initialize int variable for Value sent to the LED

 In addition to printing “ldr value” as is, you could also have checked if this is less than or greater than a given quantity, and have printed a message such as “Dark”, “Normal”, “Bright”, etc

 The value obtained “cds value” will be higher the brighter the environment. Instead, the LED has to illuminate more the darker it is the environment. In other words, “LED brightness” must be higher the lower the value of “cds value”. Therefore, we have to invest ” ldrVal ” so that its value goes from a scale of 0 to 1023 to another of 1023 to 0.

And now, we have to map “cds value” to fall within the range allowed for the PWM output. That is, pass a value that is between 0 and 1023 to another which is between 0 and 255.

project2: how to calibrate Photoresistor using mapping:

The following sketch is intended to calibrate a photoresistor, although The procedure is generalizable with hardly any changes to any other analog sensor. The required circuit is the same as in the previous project1: a photoresistor connected to an analog input pin (we will assume 0) accompanied by a 10KΩ pull-down resistor, and an LED connected to a PWM output pin (we will assume 5) accompanied by its corresponding 220Ω voltage divider.

Photoresistor or LDR Arduino based Projects

Or you can use ldr module, because LDR module have a built-in resistor so no need to wire the resistors 

Photoresistor or LDR Arduino based Projects

What the sketch basically does is read during its first five seconds of run a series of values ​​from the analog sensor to establish what its reading with minimum value and which will be the one with the maximum value. It is evident that during those five seconds, we must subject the sensor to both circumstances extremes to see how it reacts (in the case of a photoresistor, illuminating it with the maximum light foreseen in the project and with the minimum). Once the calibration is done, the rest of the code is very similar to the one seen above: the key is in the function map (), first of all, because it performs the mapping in inverted ranges itself (for now explained to illuminate the LED when low light is detected), but mainly because the mapping sets it to the range of calibrated values, not the typical 0 and 1023.


Programming explanation:

 If a value greater than the current maximum is read, I save it as the new maximum value

 If a value less than the current minimum is read, I save it as the new minimum value

then I apply the calibration to the reading just read to the transformation that must suffer cds value to be used in analogWrite()

In case the reading just read falls out of range set during calibration…

at last, I lighted the LED using the new calibrated value

project3: how to change the buzzer sound using photoresistor with Arduino:

As you can see the circuit shown in the below figure a photoresistor or ldr with 10ohm pull-down resistance and a buzzer with a voltage divider 100ohm resistor. In this project, we can make that as the light detected by the Ldr, the frequency of the buzzer sound changes.


Photoresistor or LDR Arduino based Projects


Program explanation:

In  loop() class first I define the variables

Then I read the LDR value using Arduino built-in function analogRead and store the value in user define variable

In this case, the LDR calibration gives values ​​between 400 and 1000, but this may vary. The output ranges are, in Hz, the minimum and maximum frequencies of the sound that we want to emit

For stability between readings use delay

project4: Arduino based solar tracker using LDR or photoresistor:

Arduino based solar tracker using LDR / photoresistor- In this project, you will learn how to make your own Arduino based solar tracking system using LDR. This solar tracker project is based on Arduino, LDR, and H-bridges. The LDR’s will be used for sunlight sensing.

Arduino based solar tracker circuit diagram:

Photoresistor or LDR Arduino based Projects

Atmega328 PCB board file

Relay H-Bridge PCB board file


Program explanation

First I define two integer type variables that are used for detecting the light.

then define two integer type variables for relays in an H-bridge are controlled using the Arduino’s pins 7 and 8.

this condition is checks if the light is only falling on one LDR, then rotates the solar panel

this condition checks if the light is falling on the other LDR, then rotates the solar panel in the opposite direction.


Related Articles

Leave a Reply

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

Back to top button