#rem
LED Photometer.bas for the Picaxe08M tjl Aug 5, 2007
Using a LED to measure relative light level.
Reference:
http://www.electronicdesign.com/Articles/Index.cfm?AD=1&AD=1&ArticleID=15980
Some facts: LEDs are sensitive to light somewhat shorter than their design
output, and detect a narrow band. The cited article states that
a yellow-green LED emitting 555 nm detects 525 nm in a 50 nm band.
The LED is charged in a reverse direction, then the voltage is
measured. (Requires pins that can switch direction.) The decay
time to some low level is proportional to the detected light
intensity, with shorter decay times corresponding to brighter
light.
Hookup:
A LED is connected to legs 5 and 6 as shown below:
| R bias
| _____ |\ |
|-----|_____|-----| >|------+
|5 |/ | |
08M |(Out2,PWM2) |
| |
|---------------------------+
|6
|(Out1,ADC1)
|
To turn on the LED, both pins are set as outputs, with Out2 high and
Out1 low. Alternatively, the LED's brightness can be varied using PWM,
with PWM to PWM2 and low on Out1.
To measure the light level, the LED is charged by sending a high on Out1
momentarily with a low on Out2. After a pause, ADC1 reads the decaying
LED voltage. Ten measurements are averaged, then sent to the terminal.
In the program below, adjust ReadDelay empirically for your particular LED
and the expected light levels.
#endrem
symbol ReadDelay = 5 'mS between charging and reading the LED
Main:
do
dirs = %00000110
; Blink LED to show that the test loop has begun
low 1
high 2
pause 50
w5 = 0
w6 = 0
for b0 = 1 to 10
'charge the LED
dirs = %00000110
low 2
high 1
pause 1
'read the LED's decaying voltage
dirs = %00000100
pause ReadDelay
readadc10 1,w5
'sum the measurements
w6 = w6 + w5
next
'average the readings
w6 = w6 / 10
'report
bintoascii w6,b0,b1,b2,b3,b4
sertxd(b1,b2,b3,b4,13,10)
loop
end