0
\$\begingroup\$

I want to write the code to turn the LED on when the light intensity that falls on the photoresistor decreases. So in daylight LED is off, and when it's getting dark LED goes on. I know that resistance depends on light intensity. When is bright resistance is low and it goes higher when it's getting darker. Also, I get that voltage changes with resistance. So I need to turn the LED on when the voltage coming from the photoresistor is low. The problem is I don't know how to calculate exact values. How to calculate voltage value when the Sun is going down and the light is low? Also, how can I compare this value with an integer when writing if loop.

Can I use voltage value in code or do I have to convert it?

\$\endgroup\$
4
  • \$\begingroup\$ List all the variables for Lux, then estimate the ranges for sunny, cloudy and use a cheap calibrated white light log voltage sensor (Vishay or Infineon if they still make them) Use two at 45 deg and add to get a broader view? . The logic might also need hysteresis. Don't use a poor accuracy LDR Even a PD is more accurate But DO define how much you are willing to tolerate. for error. \$\endgroup\$ Commented Jan 23, 2022 at 16:40
  • \$\begingroup\$ Why do you need to calculate it? Can't you have the light go on and off at some values that do the job? Because, it can't be calculated exactly anyway, as you don't mention the other resistance value or how the LDR reacts to light, and would you know the light level you want so you could calculate the resistance. Components also have some tolerance. Comparison can be performed between two integers in many programming languages, you just did not mention which language. \$\endgroup\$
    – Justme
    Commented Jan 23, 2022 at 16:42
  • 5
    \$\begingroup\$ Can't you just build the circuit and observe what values you get? Don't talk about exact values because you won't get them. \$\endgroup\$ Commented Jan 23, 2022 at 16:43
  • 2
    \$\begingroup\$ Don't calculate, measure. Build the circuit. Adjust the threshold levels until it changes at the light levels you want. \$\endgroup\$
    – Neil_UK
    Commented Jan 23, 2022 at 17:58

2 Answers 2

1
\$\begingroup\$

Perhaps calibrating LDR light-versus-resistance is unnecessary...

Think of maintaining a constant light level, be it from daylight or LED source. Thus, the LDR will be maintained at a fixed value of resistance. LED will provide all the light in darkness, and no light during daylight.

LED provides some light during day/night transitions. This might be accomplished efficiently with PWM, fast enough that LDR resistance doesn't change much during PWM on/off periods.

schematic

simulate this circuit – Schematic created using CircuitLab


The software adjusts PWM duty cycle to maintain LDR resistance at a constant value. The microcontroller's ADC would then measure (and maintain) a constant voltage.

Some adjustment of R1 and R2 may be necessary for your unspecified LDR - this circuit and light-adjusting algorithm serves as a circuit-concept rather than definitive, sure-to-work idea. The distance between LED (D1) and LDR1 must be carefully adjusted so that during daylight, LED light is off, or nearly off. R3 limits LED current through D1, limiting its light output...if the LED is too dim at night, move it further from LDR1: the amount of light-coupling from LED D1 -to- LDR1 affects circuit "gain"...some moving-about is required.

A green LED is suggested, since both eye and LDR have peak response in the green-yellow part of the spectrum, but any colour LED should work.


A microcontroller may not be necessary - this is a feedback idea that can be done with a linear DC-coupled amplifier. In darkness, LDR1's increasing resistance will raise its DC voltage...which in turn should increase LED current to compensate:

schematic

simulate this circuit

\$\endgroup\$
1
  • \$\begingroup\$ I was just thinking about a very similar idea two days ago for a different application, though I was thinking strictly about analog and not MCU-driven feedback at the time. Very weird-feeling to see it show up so soon after and it's nice to now think about MCU aspect as another consideration. The MCU may offer a dithering-around option so that integration over time may offer some improvement in resolution, though this only now crosses my mind and I'll need to think more about it. Interesting catch! +1 \$\endgroup\$
    – jonk
    Commented Jan 23, 2022 at 19:04
0
\$\begingroup\$

To expand on an engineered solution with specs as I hinted in comments.

As most know by now show me a good product and it will have great details in the datasheet. Likewise for DIY projects. The better you measure what you want, and tolerate, the better the outcome. Otherwise, it's trial and error (repeat).

Light levels

Here are some examples of the illuminance provided under various conditions:

Illuminance 
[lux]           Surfaces
========        =============================================
0.0001          Moonless, overcast night sky (starlight)
0.002    .....  Moonless clear night sky with airglow
0.05–0.3        Full moon on a clear night
3.4      .....  Dark limit of civil twilight under a clear sky 
20–50           Public areas with dark surroundings 
50 ...........  Family living room lights (Australia, 1998) 
80              Office building hallway/toilet lighting 
100 ..........  Very dark overcast day 
150             Train station platforms 
320–500 ......  Office lighting  
400             Sunrise or sunset on a clear day. 
1000 .........  Overcast day; typical TV studio lighting 
10,000–25,000   Full daylight (not direct sun) 
32,000–100,000+ Direct sunlight

Log Light Sensor

enter image description here

Just using a load resistor defines the voltage.
Then decide if you want to measure it with an ADC or use a precision comparator with a band-gap Vref or just use a 3.3V logic IC and rely on Vdd/2 or add % hysteresis. Add a cap to filter flashes of light or darkness etc.

You define how you want it to behave or not misbehave. ;)

Cheap, easy to use and accurate

and designed long ago by the best in the industry

enter image description here

Then Sharp licensed the designs to Vishay, now packaged on a PCB by Adafruit

enter image description here

Pick your target level lux, then voltage threshold then R.

Don't forget to not have the light near the sensor or it may toggle on and off.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.