LEd as a light sensor

Rickharris

Senior Member
I have been trying to find the thread(s) about this as I have an application but so far have not found what i recall seeing i.e. a long thread about how to get this idea working.

On the same line the search seems to take all the words you put in and search separately even if you use " xx " which isn't useful if you search for "LED sensor" and you get every post with LED and sensor in it but not together.

Sorry having a bad day!

Anyone know where this thread is?
 

Michael 2727

Senior Member
Last edited:

Rickharris

Senior Member
Thanks all - The external link told me what I needed - I was sure there was a longer more detailed investigation perhaps connected to the capacitance of a LED? The nurons fail me.
 

Rickharris

Senior Member
Mmmm still not working

I measure the voltage produced by a green LED - 1.2 Volts with DVM - varies with light level all OK.

I connect the same LED to the analogue input (1) and set up a simple sample prog to check but get nothing??/

Code:
start:
readadc10 (w0)
sertxd  (#w0,13,10)
goto start

terminal receives 0 all the time even under bright lighting.

ideas - I am obviously doing something stupid here.

have investigated some more and with picaxe off the reading is around .8 to 1.2 volts under a bright light. With Picaxe on the reading falls to a few milli volts - that explains why the reading is zero but why the loading effect.

hHs anyone got this working before?
 
Last edited:

Tom2000

Senior Member
I connect the same LED to the analogue input (1) and set up a simple sample prog to check but get nothing??/
Rick,

The way to measure light with a LED is to first charge it (reverse biased), set LED's anode pin as an input, then measure how long it takes for the LED's capacitance voltage to decay to some preset value. (The way some do this is to set the anode pin as a digital input, then wait for the pin to decay below the logic zero threshold.) The decay time is proportional to the amount of light detected.

I tried this with a Picaxe 08M. Since the chip isn't too good at precision timing, I set the anode pin as an ADC, paused for a set period, then measured the voltage.

It seemed to work OK, but not well enough for precision light level measurement. This technique would work for stuff like using a project's existing LED to automatically set a LCD's backlight level, as long as you have a spare pin.

My test code is posted below.

Have fun!

Tom


Code:
#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
 
Last edited:

Michael 2727

Senior Member
Similar to above, try (on an 08M) placing the
LED between 2 programmable pins (I/O)
Then try sampling a split second after swapping
the pins between either High, Low and Tri State
(set as input), you'll have to experiment a little.
Be careful doing this and use a 330 Ohm resistor
just in case.

I don't fully understand the capacitive effects
of this method but it will decay very quickly.

The voltage does drop off fairly quickly using
a DMM, you may be able to beat this by
swapping the pin/s, then quickly doing an ADC
reading.

Good Luck :)
 

boriz

Senior Member
I measure the voltage produced by a green LED - 1.2 Volts with DVM - varies with light level all OK.

I connect the same LED to the analogue input (1) and set up a simple sample prog to check but get nothing??/
It could just be the loading. For a DVM you’re talking tens of Mohms, for a PICAXE ADC it’s maybe a few Kohms. It would prolly work if you used an FET op-amp as a buffer.
 
Last edited:

Rickharris

Senior Member
Tom I have assembled your suggestion but still only get 000 back to the terminal - BUt it strikes me that I am using an 08 + L293 experimenters board I wonder if the L293 inbetween the 08 and the LED is upsetting things. I will try later with a bare 08M
 

Rickharris

Senior Member
Tom I have assembled your suggestion but still only get 000 back to the terminal - BUt it strikes me that I am using an 08 + L293 experimenters board I wonder if the L293 inbetween the 08 and the LED is upsetting things. I will try later with a bare 08M
OK problems caused by the L293 - Toms code works well with a green LED. Now to apply it of which more later if the idea works.
 
Top