LED Light Brightness Regulator

Bill z

Member
Still a new guy here with some questions about a project on my mind. I’m wanting to use a 08M2 to check the ambient light outside and decrease LED brightness as the sun goes down using PWM. See, I want the LEDs to appear to have the same brightness all of the time with respect to bright sunlight, cloudy days or night.

But being new, I don’t know what light sensor to get. It can’t be a binary one of course. Then depending on what sensor I get, can a 08M2 monitor the sensor to adjust the LED brightness? I know the 08M2 can use PWM to adjust brightness, but can it compare the input to affect the output?

I can use any guidance that you have to get me started.
 

lbenson

Senior Member
I want the LEDs to appear to have the same brightness all of the time with respect to bright sunlight, cloudy days or night.
You might have to explain this a little more fully. I don't see how an LED in bright sunlight could ever appear to the eye as bright as it could be at night, and if the LED were illuminating something, it's hard to see how much of anything in the way of greater illumination than direct sunlight would be accomplished without a great deal of power (which could be governed by a picaxe, if you found the LED and power source to do it).
 

Chris Kelly

Well-known member
Hi Bill

I would use a regular Light Dependent Resistor (LDR) and use it in a voltage divider set-up. As the light fades it's resistance will increase, and you can use this in your circuit to send a voltage to your Picaxe. Use the 08M2 ADC input to monitor the voltage as it changes. Calibrate this accordingly and write the code based on the range of voltages you have to work with.

Cheers

Chris
 

AllyCat

Senior Member
Hi,

A Light Dependent Resistor (LDR) should be able to do basically what you want, coupled in to a PICaxe ADC input. The "classic" device was the "ORP12", but others are still available, some quite cheaply. Connected in a "potential divider" configuration with a resistor approximately equal to its average (logarithmic) value (probably around 10k) it can give a reasonably large dynamic range (perhaps 10,000 : 1).

But beware that outdoor light levels vary ENORMOUSLY; direct sunlight is about 100k Lux and moonlight about 0.1 Lux, that's a range of 1,000,000 : 1 , so you may need to use "other" methods such as front (perhaps antireflection) filters. Note that LDRs have a (colour) response similar to the human eye, which is why they were often used for photographic light meters (but they nearly all needed several "coarse" ranges), whilst silicon-based (photo-) diodes (and PV panels) tend to peak in the (near) Infra-Red.

Cheers, Alan.
 

premelec

Senior Member
I would try 2 LDRs of identical nature with one exposed to outside light through a filter [e.g. couple of pieces of white paper]... for single READADC input put the two LDRs in series with them between V+ and V- and the junction between the two to ADC pin. If the LDR goes to really low resistance you might have to add a series resistor to reduce current... Then you regulate the READADC pin to read 1/2 voltage... various delays in changing the LEDs current inyour program will be necessary to prevent too much obvious hunting for current value as things change and make changes in small steps.
 

Bill z

Member
Thanks all for your input.

WOW! All of this information is quite interesting and I never thought of all of the variations. I didn't know what I didn't know.

lbenson, I do see your point, but the LEDs are enclosed and illuminating the sign from inside. Imagine a 'Police Call Box' sign (if they still have those) where the words are white and bright. That is what the LEDs are doing.

Alan, Much of the time the sign is in the shade of a building with the bright light of the sun still around the building.

Currently, the super bright 5 volt LEDs can be seen in the day, but they are an irritant at night they are so bright. The sign needs to be seen but when it is too bright because of night, it is unpleasant to look at.

I see that photo-diodes range from a pack of 10 for $1.22 to $100. each on eBay.

So, I'll order some photo-diodes and start with a 2.2K resister to form a voltage divider and wire the center of the voltage divider to pin C.4 and use that to determine DutyCycle, is that correct?
 

papaof2

Senior Member
Clear letters showing through a dark frame would allow you to place an LDR on the inside of the dark portion, facing the LEDs, and another one on the inside of the clear portion, facing the exterior illumination.

Set the minimum brightness (PWM level) at whatever is acceptable for night use and then increase the PWM level to try to keep the interior and exterior LDRs at the same value. That won't actually happen during the day unless you have very high power LEDs, but it appears that the current LED brightness is OK for daytime use.
 
Last edited:

lbenson

Senior Member
While you're waiting for the photo-diodes you ordered, you actually have some in hand--normal LEDs. In this post from 2007, wilf_nv showed how to use LEDs as photo-diodes

It worked quite well for me with just my hand shading interior lighting or a bright flashlight. It might not give you the resolution that a real photo-diode would, but I would suspect would be adequate for your purposes.

You might also test with a pot to determine by experiment what level of brightness looks best to you through dusk into darkness.
 

Bill z

Member
While you're waiting for the photo-diodes you ordered, you actually have some in hand--normal LEDs. In this post from 2007, wilf_nv showed how to use LEDs as photo-diodes
Sounds like a great idea and I'll try.

What command do I use to check input voltage on pin C.4? I assume it will be between 0 and 5 volts and may not be linear. Is it -> readadc 4,b1 ?

Then I can loop and pause, using a formula to set DutyCycle. Does that sound right?

I'm guessing that the editor can't simulate the input.
 

lbenson

Senior Member
Sorry for the delay. It turns out I had 4 versions of this, unchanged since 2009. I needed to check what worked. This one, dated January 29, 2007 works (I added the #PICAXE 08M2 and #TERMINAL 4800 directives).
Code:
' TEST06E led as light detector (yellow led)--08M
'           ___      LED
' pin1-   -|___|-----|<--.
'           330          |
' pin2-   ---------------'
#PICAXE 08M2
#terminal 4800

symbol cathode=1
symbol anode=2
high anode     ' flash test led
low cathode
pause 100
low anode

main:
  high cathode
  low anode
  input anode        ' tristate anode (isolate anode pin)
  nap 7              ' nap 2 sec
  readadc anode, b2  ' get the voltage
  low cathode        ' pulse cathode
  nap 3      
  b4 = b2
  b5 = 0 
  w2 = w2 * 45 / 255 ' x = 4.5V * adcVal /255
  b6 = w2 / 10       ' whole volts
  b7 = w2 % 10       ' tenths of a volt
  sertxd ("ADC value: ", #b2, "; LED voltage*10: ", #w2, "; ", #b6, ".", #b7, "volts",13,10)
  sleep 1      ' sleep 1 sec
  goto main
The LED is reverse biased, and then the voltage is read on the anode. At 5V, ambient household light in the evening gave me a reading around 168. A bright LED flashlight gave 255. I could shade to get ADC readings in between. The LED was yellow, which I had found best 13 years ago. I don't believe I had any blue or white LEDs to test with then. I omitted the 330 ohm resistor shown in the diagram in the program--no resistor at all. YMMV.

The display of the voltage is based on 4V5--3-AA batteries. I didn't change the calculation to account for the 5V I used (from a USB/serial module).
 

AllyCat

Senior Member
Hi,
I'll order some photo-diodes and start with a 2.2K resister to form a voltage divider and wire the center of the voltage divider to pin C.4 and use that to determine DutyCycle, is that correct?
You can't put a photo-DIODE into a "divider" (you might measure its reverse leakage or forward PV current over a limited range). Read again what we have said about Light Dependent RESISTORS. To read the ADC input voltage on a pin, use the READADC {10} command.

You probably will need a non-linear conversion from the LDR (resistive divider) to (PWM) output current into the LEDs. What I described in #4 will actually give an "S" shaped curve over its full range. Maybe you won't need a dynamic range of many thousands, but probably still in the hundreds.

Cheers, Alan.
 

premelec

Senior Member
There is a simple and very sensitive device to compare two light sources: a couple of 1/2 inch thick blocks of paraffin wax [a milky whitish wax often used to seal fruit preserves] . Separate the two blocks with a sheet of aluminum foil - expose one side to one light and other side to other light and look at [with LDR or eyeball] each side end diffused light. I don't know what this is called but it is very clear which side is brighter... Probably same structure would work with milky plastic as light diffusers...
 

Bill z

Member
Ibenson, How do I test what voltage will come from the LED I select? Is there a way to get the sertxd information from the chip? If not, I don't know how to get the 08M2 to report on what it sees between C.3 & C.4. Yes, I can measure volts but do volts measured equal what the chip values are?

Yes, all new guy questions.
 

lbenson

Senior Member
The program I provided, with a yellow LED mounted with the cathode (bar end, shorter pin) on 08M2 picaxe pin 1 (leg 6) and anode on pin 2 (leg 5) will report with SERTXD (to a connected PC) the ADC reading it is getting from pin 2, and also the calculated voltage (if the picaxe power voltage is 4.5 volts--3-AA batteries (approximately)). (There are now ways to calculate the voltage that are independent of the picaxe power voltage--or rather, that provide an internal means to determine that voltage.)

Don't know what you mean by "between C.3 & C.4"--C.3 can't be connected to the LED, since it is input-only--both LED connections must be to pins which can be outputs.
 

Bill z

Member
What program do I use to receive the SERTXD report from the 08M2? Is there a setting or option in PICAXE Editor?
 

Bill z

Member
Most all of the images and help I'm finding use this attached picture. Aren't C.4 and C.3 mostly used as inputs and C.2 & C.1 used as outputs? Maybe I'm guessing here.

I was planing on using C.2 as my PWM output.

Using C.4 & C.3 in the picture, connecting a yellow LED, I was able to get 1.1volts with low light and 2.3 volts with florsent light.
 

Attachments

Last edited:

lbenson

Senior Member
What program do I use to receive the SERTXD report from the 08M2? Is there a setting or option in PICAXE Editor?
Sertxd output shows in the Serial Terminal associated with the Program Editor. "#TERMINAL 4800" should evoke it with the baud rate correct for the standard frequency ("SETFREQ M4"). You may need "#COM {portno}" in your program, or if the Serial Terminal opens and shows no COM port number, click the down arrow to select the port.

Since you need C.2 for the PWMOUT, you can connect the anode of the yellow LED to C.4 and change the pin definition to "symbol anode=4". You may need averaging to get fairly consistent readings, e.g., read 10 times summing in a word variable and then divide by 10.

I'm not sure how the LED could work as a photo-detector with either anode or cathode connected to C.3, since that is neither an output pin nor an ADC pin. I just tried that (eliminating references to "cathode", and got a difference, but very little dynamic range.

You might experiment with different colored LEDs.

On the 08M2, C.1, C.2, and C.4 may be inputs or outputs or ADC. C.3 is input only and C.0 is output only. C.5 may be used for input in special circumstances.
 
Last edited:

premelec

Senior Member
The voltage from an LED is small- you can put a bunch in series - or find a multi cell PV unit from a garden light or calculator and get several volts.
 

lbenson

Senior Member
The voltage from an LED is small
Here are samples of what I'm getting from wilf's method--one 3mm yellow LED:
Code:
ADC value: 183; LED voltage*10: 32; 3.2volts
ADC value: 252; LED voltage*10: 44; 4.4volts
ADC value: 201; LED voltage*10: 35; 3.5volts
ADC value: 192; LED voltage*10: 33; 3.3volts
ADC value: 77; LED voltage*10: 13; 1.3volts
ADC value: 81; LED voltage*10: 14; 1.4volts
ADC value: 97; LED voltage*10: 17; 1.7volts
ADC value: 127; LED voltage*10: 22; 2.2volts
ADC value: 31; LED voltage*10: 5; 0.5volts
ADC value: 112; LED voltage*10: 19; 1.9volts
ADC value: 248; LED voltage*10: 43; 4.3volts
ADC value: 255; LED voltage*10: 45; 4.5volts
ADC value: 241; LED voltage*10: 42; 4.2volts
ADC value: 92; LED voltage*10: 16; 1.6volts
ADC value: 198; LED voltage*10: 34; 3.4volts
ADC value: 188; LED voltage*10: 33; 3.3volts
ADC value: 164; LED voltage*10: 28; 2.8volts
ADC value: 147; LED voltage*10: 25; 2.5volts
ADC value: 129; LED voltage*10: 22; 2.2volts
ADC value: 137; LED voltage*10: 24; 2.4volts
ADC value: 170; LED voltage*10: 30; 3.0volts
ADC value: 157; LED voltage*10: 27; 2.7volts
ADC value: 130; LED voltage*10: 22; 2.2volts
ADC value: 137; LED voltage*10: 24; 2.4volts
ADC value: 162; LED voltage*10: 28; 2.8volts
ADC value: 171; LED voltage*10: 30; 3.0volts
ADC value: 165; LED voltage*10: 29; 2.9volts
ADC value: 255; LED voltage*10: 45; 4.5volts
ADC value: 255; LED voltage*10: 45; 4.5volts
ADC value: 227; LED voltage*10: 40; 4.0volts
ADC value: 182; LED voltage*10: 32; 3.2volts
ADC value: 180; LED voltage*10: 31; 3.1volts
ADC value: 180; LED voltage*10: 31; 3.1volts
ADC value: 175; LED voltage*10: 30; 3.0volts
 

premelec

Senior Member
I have to admit I haven't tried tried an LED in a couple of decades... Are your LED volt values with no load on ADC pin? With pin bypassed with small capacitor? I'm wondering if the adc pin is effectively picking up noise and is in effect floating and perhaps LED acting as diode rectifier.. I don't know the wilf method referred to..
 

AllyCat

Senior Member
Hi,

It looks as if the "wilf method" uses the (light-dependent) reverse leakage current of the LED to discharge the "stray" capacitance of the LED and the PICaxe pin(s), etc.. The voltage being measured is that remaining after the current has flowed for a preset period of time. Note that with PV panels and Photodiodes, etc. it is the CURRENT that is (linearly) proportional to the light level. Not the voltage which is largely constant in the forward direction, and might be almost anything (less than the breakdown voltage) in the reverse direction ! ;)

I believe that some modern "high brightness" LEDs (particularly white) use phosphors to convert the light (including UV) to a different "colour", and maybe even a laser action, which is NOT reversible (i.e. in converting light back into a "voltage"). Many "photodiodes" actually have a built-in transistor to amplify the tiny photo-currents. Personally, I consider the "wilf method" rather like riding a Unicycle, it's "clever" that it can be done at all, but certainly not the most practical way to travel from point A to point B if the distance is large. :)

It appears that your prototype is "saturating" with the light from the flashlight (i.e. ADC value = 255), so will it be able to discriminate bright daylight? I still think you (may) need a logarithmic sensor, such as a LDR. Of course there are also quite sophisticated silicon sensors, with I2C interfaces, etc, but there are good reasons why LDRs are still available. ;)

Cheers, Alan.
 

lbenson

Senior Member
OP has ordered and is awaiting his parts--I was merely indicating a method he could play with until they arrive. Yes, it saturates with the flashlight close up and direct (ADC reading of 255), and also in direct sunlight (through a skylight).

It could discriminate between sunny and overcast and dark, but it would take experimentation to determine what finer levels of adjustment would be possible, and whether they would suit the needs of the OP. I don't know if it could discriminate between moonless dark and full moon. If we ever have a cloudless night again, I'll see.

Wilf_nv's project was a firefly effect--LEDs configured like this would (in a darkened room), detect other LEDs lighting using this method, and then, with random wait times, would themselves light and trigger other LEDs. I thought it was a neat effect, and did a little experimentation.
 

premelec

Senior Member
I had forgotten the wilf_nv discussion - fun and lacking immediate practicality ;-0 Real fireflies out currently...
 

Bill z

Member
The only way possible is to leave the LEDs off.
That certainly leans heavily to wilf_nv's mission -> "my mission in technology is to do more with less and ultimately, by reductio ad absurdum, to do everything with nothing. "

Minimal number of components your way would be zero.
 
Top