LDR with Arduino

In this LDR code does the value of variable res which is read using analog pin is equals to the resistance of LDR or it represents the Light Intensity?
When i upload code to Arduino board and shed light on LDR this value increases.
Generally the resistance of LDR decreases on High intensity light.
Plz help.

int res = 0; /* declaring the variable that will store the value of photoresistor*/
int sensor =A0;/* assigning Arduino pin for photoresistor*/
int led= 5;/* assigning Arduino pin for LED*/
void setup() {
Serial.begin(9600); /*setting the baud rate for serial communication*/
pinMode(5, OUTPUT); /* assigning mode to LED pin */
}

void loop() {
    // put your main code here, to run repeatedly:
res = analogRead(sensor); /* getting the value of photoresistor*/

Serial.println(res); /* displaying the photoresistor value on serial monitor */

    if(res > 100) { /* when the value of sensor is less than 100 */
Serial.println(" Low intensity ");
digitalWrite(5,LOW); /* keep the LED off*/

    }
    else { /* otherwise turn the light on */
Serial.println("High Intensity ");
digitalWrite(5,HIGH); /* turn the LED on*/

    }
delay(1000);
}```

The LDR is a variable resistance.
The light incoming will REDUCE the value of the resistance (the incoming photons trigger the current flow throught the resistance).

In the dark: few photons, few flow current: a VERY large resistance value
in the daylight: a lot of photons, a "lot" of current flow: a WEAK value of resistance.

for safety of the code, you should add in Setup():
pinMode(A0, INPUT);
(and maybe try another input pin than A0)

So I am surprised with the line

a weak resistor value means few light, so >100 will be dawn at last, not night (what I see at home with my weather station)

Get yourself a multimeter, plug it on the 2 wires of your LDR and make some tests!
Can you confirm your LDR is direclty plugged to your board (no divider bridge with resistor)?
I also wish to propose you power the LDR through a digital pin of the board rather than with the 5V (or 3.3V). This way, you dont power the LDR all time but only activate the OUTPUT pin when needed.

What is the difference between the two anyway?

How are you connecting the LDR to the analogue input? What value of resistor are you adding to the analogue input?

An analogue input will not measure resistance it only measures voltage. You convert resistance into a voltage by having a voltage divider circuit and having the LDR as one of the resistors. If it is in the top leg the voltage read goes up the more light there is. In the bottom leg it goes the other way round.

i have connected LDR in divider bridge.
issue is when i put some light on LDR the output value increases. If this is the value of resistivity than according to rule it should be decreases on High light.

A bridge has four resistors in it, a divider has two resistors in it. What yo you have?

1 Like


this is my configuration

My bad here.

"bridge" was my personnal translation (from french) of "pont diviseur" wich includes 2 resistors only.

1 Like

invert the resistor and the LDR (it is my config at home) so the LDR is connected to input pin and GND whereas the resistor is connected between 5V (or output pin) and input pin.

EDIT: bad eyes, I made confusion with the LED and the LDR drawing:
something strange in your schema:

Led is supposed to be piloted with pin 5, and the LDR with pin AO.
it is the inverse in your schema.

1 Like

From this fritznuts thingy, When the light increases the resistance of the LDR decreases leaving more V's for the A0 to detect; more light more V's. The circuit is working properly as wired.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.