Project: Using simple LED as a light sensor.

Filed in PIC , Projects 4 comments

led-christmas

This post shows how to use a simple LED as light sensor, and to dim a 7-segments LED display with PWM automatically in the dark.

1. LED as a sensor.

LED emits light when electricity passes from its anode to its cathode.
LED emits electricity when light is applied to its silicon junction. How much electricity ? Not that much, but enough for our purpose, a few tenth of milliVolts at most. This could easily be read by the ADC of a PIC, by connecting its cathode to the ground and its anode to the input pin of the PIC.

But this is the real trick :

We will reverse the connections of the LED : its cathode will be connected to the input pin of the PIC, and its anode to the ground.

Why ? Shall we have to read a negative voltage value ? Will the PIC be destroyed by the negative voltage ? Will the LED fry, because it is reversed ?

Not at all !

Connecting the diode in reverse polarity to the PIC ADC input will have the effect :

In the dark : the silicon junction does not receive any light, and no voltage is present between its pin. In fact, the diode acts like an open circuit. Due to its internal structure (a charged capacitor), the PIC ADC will read in this case a ‘phantom’ voltage, and its value will be returned by the conversion.

In the light : the silicon junction of the LED is excited and a little voltage appears between its pins. As it is reversed to the ‘phantom’ voltage, the result voltage will be less than zero, and the PIC ADC will return a null value.

Of course, this reverse voltage is far away from being dangerous for your PIC, and no current drives through the LED to fry it : your circuit is safe !

2. Dimming the display

To dim the display, we have to reduce the amount of current that drives through the LED display. To reduce the amount of current, the first idea is to reduce the current itself. Of course it is possible, but it would need some extra analog circuitry. A better idea would be to reduce the period during which the current is supplied to the LEDs. The amount of current received by the display during a time unit will be less, and the display will dim. This can be done with software : 7 segments LED displays are usually time-multiplexed, some adjustments in the software will do the job, and no extra circuitry will be needed.

The software will do a kind of PWM (Pulse Width Modulation) : by adjusting its duty cycle, we control the brightness of the display. With a 0% duty cycle, the display is switched off (the LEDs receive no current), with a 100% duty cycle, the display is turned on (the LEDs receive all the available current), otherwise the LEDs light proportionally to the duty cycle.

The software PWM for LED display is not that hard to code, because the PWM frequency involved is not very high, and not time-critical too.

3. Build the circuit

All that you have to do, is to insert the LED into the DIP28 socket of your EasyPIC4. This socket has the advantage to have the RA5 pin and the Ground pin side by side, so that we don’t need extra circuitry.

Plug the cathode of the diode into the RA5 hole, and the anode into the Ground hole. This should look like this :

096-LED-LIGHT-SENSOR-ledlightsensor

4. The Code

Download the code from here : http://creativeelectron.net/blog/wp-content/uploads/2009/09/Code.c

The code is made in MikroC Compiler. Download from here : http://creativeelectron.net/?p=98

5. Run the program

Just turn off the light of the room and see the result on the display. The LED is a very sensitive sensor : the display will dim only if there is no light at all.

To adjust the brightness of the dimmed display, press the RD0 or RD1 keys : the PWM duty cycle will change and the brightness of the display too.

If you enable PORTC LEDs, the duty cycle is shown as a bar graph.

5. Try other LEDS

Try with all LEDs you have : depending on their diameters and colors, sensitivity may vary.

Happy SenseING =)

Posted by hamzaazeem   @   27 September 2009 4 comments
Tags : , , , , , , ,

Share This Post

RSS Digg Twitter StumbleUpon Delicious Technorati

4 Comments

  • Excellent idea...great job. I'll try to extend it...
  • creativeelectron
    that is cool, do let us know about your project
  • creativeelectron
    that is cool, do let us know about your project
  • Kumail Ahmed
    Good. This is an ongoing project at the mitsubishi research lab.

    http://www.youtube.com/watch?v...
blog comments powered by Disqus
Previous Post
«
Next Post
»
EXULT designed by ZENVERSE  |  In conjunction with Reseller Hosting from the #1 Web Hosting Provider - HostNexus.