Let's Make Robots!

Input and output with a single LED

Use an LED + digital input as light detector
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.


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:


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
        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.



  • 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.
well actualy i think those big TV's at shopping centers or racetracks are made up off LED's, you can normaly tell ecause the picture qualitty is a bid odd

Super walkthrough......

The LED array video stuck in my mind too.....makes me wonder if it works on a 7segment LEDarray- then i have an application.

... thanks for the impulse........

NB. dont question how it works........... it works so just use it.......

A light emitting diode is a photo-transistor.. 
A wire is a capacitor..
Whoa, it's all connected !

Thanks for the splendid walk-through minivelt

now you just need 1000 LEDs in a giant red eye :)

Almost right on the first point - an LED is a photodiode, a 2-region device. A phototransistor has higher gain due to it's 3-region construction.

I used a different aproach in my original laser rangefinder in which the LED was used as a photodiode. In that case it was permanently reverse bias with a very small amount of current trickling through. When reflected light from the laser hit it more current flowed.

I want to incorporate this idea into a LED sign but I'm not certain how feasable it will be with 1024 LEDs.At least with the capacitive approach I wont need 32 analog inputs.

1024 LEDs. That is quite a lot. Are you going to make a 32 x 32 LED touch screen? Anyway: I don't think you'll get that one up and running with a single 08M ;D

I was originally asked to design a 32x32 sign and given 1024 high intensity white LEDs (16000mcd). I would like to add this touch capability but I am not sure of the feasability yet.

I suspect in that original video that a 8x8 bi-colour array was used as I have one that looks the same with orange/green LEDs. Since the green LEDs are wired reverse bias to the orange then it could work as you have demonstrated where the orange LED provides light while the green LED acts as a capacitor that varies with the amount of reflected light from the object being detected.

It must be tried.

I've read spmewhere that in the original video, there is a mirror above the LEDs to reflect the light. I've also read that the guy in the video uses ADC inputs. dont know if thats true though. 

A 32x32 touch screen made out of LEDs? Is that even possible? If so, I'm more than intrigued.