Let's Make Robots!

Input and output with a single LED

Use an LED + digital input as light detector
AttachmentSize
LED_sensor_1.bas729 bytes
LED_sensor_2.bas603 bytes

This walkthrough describes how to use an LED connected directly to a digital input as a light sensor instead of using an analog input and an amplifying transistor.

I saw this video some time ago, and I was wondering how it was done. There was some discussion about it and then, after a short while, it was quiet. So, when I saw that video again a couple of days ago, I started searching the web and ended up with a very simple demo setup where an 8mm plate with an LED stuck through a hole, functions as a optical button. 

Basicly an LED can be used as a capacitator. When you reverse the V+ and Gnd connections, the LED will not light up, but the circuit will hold a bit of charge. The time it takes to get rid of this charge decreases when the LED receives more light. So when its dark it takes longer to discharge than when the LED is in lighter conditions and this time difference can easily be measured with a picaxe.

There was some debate (see comments) as to which component holds the actual charge (the LED, a MOSFET-like circuit inside the picaxe or everything in between the V+ and the LED) but for this to work, it doesn't really matter. It's probably not the LED itself as the time it takes for the charge to fade away gets shorter when you add more LEDs in parallel. If the charge was actually held in the LED you'd expect the capacitance of the circuit to increase with more LEDs and the discharge time to increase as well.

LEDSensorSchematic.jpg

This picture is taken from the pdf in the references.

The LED has both + and - connected to pins on the picaxe. When the LED is ON (a) current flows from an output pin that is set high to an input/output pin that is set low. To charge the circuit, both pins are reversed so that the LED is connected the "wrong" way (b). In the final stage (c), the picaxe pin that is connected to the -(negative) of the LED is configured as an input pin.

To do this with a picaxe 08M you can use the following setup:

IR_sensor_08M_setup.jpg

This is the setup I did on the breadboard as shown in the video. The red LED is connected to pins 1 and 2 and the green indicator LED is connected to pin 4.

The attached program lights up the red LED for 1 second and then reverses the polarity and discharges the LED in a loop. If it takes more than 80 loops to discharge the LED (i.e. pin 2 voltage drops from high to low) we light up the indicator LED. The code for taking the reading / discharge stage is as simple as this (from Led_Sensor_1.bas):

    'stage 3 : Measure darkness => discharge
    input triggerpin
    do
        inc darkness                'count how long it takes
    loop while trigger=1     ' for the voltage on the trigger pin to drop below logical 1  / high     

Yes it is as simple as that. No transistors needed, just a small loop.

update: to reduce the flicker in the LED as the program waits for a complete discharge, you can also use the following code (from the attached Led_Sensor_2.bas)

    'stage 3 : Measure darkness => discharge
    input triggerpin
    pause 6
    readadc10 triggerpin, darkness 

Of course, this will only work on a picaxe where you can configure the same pin to switch between being an output and an ADC. If I'm correct thats only the 08M and the X2 series.

I hope this will benefit someone, my head is still buzzing with ideas of fun stuff to do with this.

references:

TODO:

  • Make it discharge faster. You can see the LED turning of for a bit when it takes a reading.  
  • Testing in differtent lighting conditions (intensity and color)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

this is so cool ! the possiblities are endless !

do see 1 small problem, will this work in the dark ? 

If its completely dark it wont work, but then again, you could use another LED to light up the dark.