Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

LED as Ambient Light Sensor & Indicator

Status
Not open for further replies.

Mike - K8LH

Well-Known Member
Just started experimenting with the bi-directional properties of LEDs hoping to figure out a way to use an LED as both a push button switch and as an indicator. Also wanted to see if I could use an inexpensive LED as a replacement for an LDR in a clock project to detect ambient light level.

My first experiment accidentally produced a simple and fascinating Ambient Light Sensor and Display. I'm using a 16F690 with serial port module and a single red LED with anode connected to RA0 and cathode connected to RA1.

Basically I light the LED for 50 usecs then I reverse bias the LED to place a capacitive charge on the LED junction. Then I set RA1 (cathode) to input while leaving RA0 (anode) at ground and I increment a counter at 1 msec intervals while testing for a change of state from '1' to '0' as the capacitive LED junction discharges. I run the discharge time value through a 16 step FIR filter to smooth the results and then I print the discharge time of 000 to 255 msecs to Hyperterminal.

The results are stunning. The LED starts to flicker at around 40 msecs discharge rate (a moderately lit room) and slows all the way down to about 3 Hz in a dark room (discharge time >200 msecs). Well lighted rooms or sunlit rooms yield discharge times of anywhere from 10 to 30 msecs so the LED appears to be solidly lit. Shining a flashlight into the LED lens at very close range can get the discharge time down to 0-1 msecs. Putting your finger on the LED or casting a shadow on the LED will make the discharge time rise dramatically.

Anyone should be able to duplicate this experiment. Please let me know if anyone wants to see my code and/or schematic and I'll post it after cleaning it up.

Mike
 
I run the discharge time value through a 16 step FIR filter to smooth the results and then I print the discharge time of 000 to 255 msecs to Hyperterminal.
Could u explain a bit on FIR filters and how u implemented it?
 
Filter is pretty much directly from PICLIST sourcecode library. Suggest you research FIR filters on your own.

Here's a program excerpt showing how I'm using it;

Mike

Code:
;
;  capture junction discharge time
;
discharge
        incf    binl,F          ; bump counter                    |B0
        skpnz                   ;                                 |B0
        incf    binh,F          ;                                 |B0
        DelayCy(1*msecs-6)      ; 1 msec -6 cycle loop time       |B0
        btfsc   PORTA,1         ; discharged? yes, skip, else     |B0
        goto    discharge       ; loop again                      |B0
        movlw   255             ;                                 |B0
        btfsc   binh,0          ; 256..511?  no, skip, else       |B0
        movwf   binl            ; limit value to 255 msecs        |B0
;
;  Avg' += ( New - Avg )/16
;
filter16
        movf    avg,W           ;                                 |B0
        subwf   binl,F          ; NEW = NEW - AVE                 |B0
        swapf   binl,W          ;                                 |B0
        andlw   0x0F            ; get /16 int part                |B0
        skpc                    ; result is neg?                  |B0
        iorlw   0xF0            ; yes                             |B0
        addwf   avg,F           ;                                 |B0
        swapf   binl,W          ;                                 |B0
        andlw   0xF0            ; get /16 frac part               |B0
        addwf   avgfrac,F       ;                                 |B0
        skpnc                   ;                                 |B0
        incf    avg,F           ;                                 |B0
 
Last edited:
Anyone should be able to duplicate this experiment. Please let me know if anyone wants to see my code and/or schematic and I'll post it after cleaning it up.
Stop it! :D This shiny thing could distract me from my SD card project. :p:D Must resist! Shutting off browser and back to coding.

Very interesting. I'll definitely have a play with that later.
 
I posted something about this a while ago, using a op amp and a standard red LED to detect colors.
 
Stop it! :D This shiny thing could distract me from my SD card project. :p:D Must resist! Shutting off browser and back to coding.

Very interesting. I'll definitely have a play with that later.

Sorry (grin). If you do get back to it, it's pretty simple. Perhaps it might look something like this in BoostC??? It should even work fine on a 12F683 with that bit-banged BoostC serial I/O demo' code. Or driving an LCD, etc...

Mike

Code:
while(1)
{
  trisa = 0;                // porta all outputs
  porta = 0b00000001;       // light the LED
  delay_us(50);             //   for 50 usecs
  porta = 0b00000010;       // charge led junction
  trisa.1 = 1;              // RA1 (cathode) input
  new = 0;                  // clear counter
  while(porta.1)            // while cathode 'hi'
  {                         //
    new++;                  // bump msec counter
    delay_ms(1);            // delay 1 msec
  }
  if(new > 255) new = 255;  // limit result to 8 bits (255)
  avg += (new - avg) / 16;  //
  PutResult(avg);           // send 000..255 'avg' via RS232
}
 
Last edited:
Hi Gayan,

It looks like I'm doing exactly the same thing as the Gentleman in that link.

---------------

I'm trying to reduce the junction discharge time. I placed a 100k resistor in parallel with the LED and the discharge time went down to zero so I wonder what value resistor might work?

Mike
 
Last edited:
Hi Gayan,

It looks like I'm doing exactly the same thing as the Gentleman in that link.

---------------

I'm trying to reduce the junction discharge time. I placed a 100k resistor in parallel with the LED and the discharge time went down to zero so I wonder what value resistor might work?

Mike

Hi Mike,
I ran some tests a few days ago using a MCP3302 on the PC's parallel port.
Repeated for this post.

Used a standard RED LED, the voltage scaling is a true representation of the LED Vout, no opa.
Light source small hand torch [lamp]

It shows on both traces on my analog logger.

Thought you may be interested.

Regard
Eric.
 

Attachments

  • LED_Light1.gif
    LED_Light1.gif
    17.7 KB · Views: 214
Hi Eric,

Thanks for info'. Looks very nice on your analog logger.

Mike

hi Mike,
I would imagine 5 Leds in series would be enough to be recognised as a logic '1' on a PIC pin, the '0' maybe a problem.????

You may have heard of the guy trying to get a quart into a pint pot, you are the only guy I know
who can get a quart out of a pint pot.

REF: Uncle Charlie...:D
 
Last edited:
Yes Eric, you've caught me trying to squeeze capability out of a PIC again (grin).

I'm trying to avoid using the LED in photovoltaic mode. The reverse biased junction seems to have more than enough capacitance to hold a high charge for a while and it would allow me to matrix several LEDs using very few pins. I won't be able to reduce the number of pins to N(N-1) using a Charlieplexed matrix because a normal Charlieplexed matrix without driver transistors has pairs of LEDs back-to-back which drain the reverse charge but I suspect I can 'borrow' signals from a multiplexed display like that on my Charlie' Clock or perhaps use a Charlieplexed display with driver transistors in a non-traditional fashion.

Wouldn't it be kind of cool using the six LEDs on one of those Blueroom boards as both input switches and as LED status indicators? Or instead of using the arrow keypad on my Charlie' Clock to switch between local time or utc time or to turn the appliance timer on or off, just press those indicator LEDs on the right side of the Clock?

Fun stuff. Mike
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top