June 19th, 2024

Two ways to use an LED as a light sensor with Arduino

John Graham-Cumming shared methods to use an LED as a light sensor with Arduino. One method uses photovoltaic effect, the other treats LED as a capacitor. Arduino code snippets demonstrate implementation. Alternative connection method suggested.

Read original articleLink Icon
Two ways to use an LED as a light sensor with Arduino

John Graham-Cumming shared two methods to use an LED as a light sensor with an Arduino. The first method involves using the LED's photovoltaic effect by reading the voltage across the LED and logging it. By setting the reference voltage with a resistor, the LED can act as a light sensor. The second method utilizes the LED as a capacitor when reverse biased. By connecting the LED between two digital pins and measuring the discharge time, the brightness of light can be determined. The provided Arduino code snippets demonstrate how to implement these methods effectively. Additionally, the post includes a reader's comment suggesting an alternative connection method for using an LED as a light sensor. These techniques offer practical solutions for monitoring light levels using simple components and Arduino programming.

Link Icon 23 comments
By @dragontamer - 5 months
Note: a photodiode is an expressly measured diode that does this job.

An LED is still a photodiode, as this blogpost noted. But you'll get more consistent results with a real photodiode (or phototransistor, if you want a bit of amplification added in)

----------

Obviously the diode works here and for this project. But given higher levels of sophisticated photosiodes (but still a similar circuit), you can choose photosiodes that detect infrared light

We humans light up like a light bulb in the infrared spectrum. So two IR Photodiodes is all you need for a human motion sensor.

A motion sensor detects motion by looking at two pixels of IR light. Motion is 'defined' as one pixel lighting up.... Followed by the second pixel lighting up. If only one pixel lights up, then no motion (it's just a glitch). Real motion should show up across both pixels in a slight time delay fashion.

IR Photodiodes (or phototransistors) are used because it's the spectrum of light humans shine the most brightly in, despite being invisible to us humans it's the best sensor at the electronics level for these purposes.

---------

There really isn't much electrical complexity to the motion sensor that this blogpost was trying to debug.

I would suggest building your own motion sensor as the next step in your EE journey.

By @EncomLab - 5 months
The way those resistors are twisted together makes my heart hurt....

Another fun fact about light and semiconductors is that the first commercial digital camera - the Cromemco Cyclops - used a 32x32 memory ic that used a windowed dip package so the chip was exposed to light. The memory was refreshed to all 1's, and the light would flip them back to 0's with the strongest light flipping the bits the fastest. The times were then parsed and converted to pixel values.

By @rpaddock - 5 months
Similar things have been around for a while and there are several variations to be found for the AVR, using the I/O pins rather than the ADC.

See US Patent 6,870,148 B2 March 22 2005 "LED WITH CONTROLED CAPACITIVE DISCHARGE FOR PHOTO SENSING" by Mitsubishi Electric Research Lab (MERL). https://image-ppubs.uspto.gov/dirsearch-public/print/downloa...

Also:

Dietz, P.H., Yerazunis, W.S., Leigh, D.L., "Very Low-Cost Sensing and Communication Using Bidirectional LEDs", ACM International Conference on Ubiquitous Computing (UbiComp), October 2003. BibTeX TR2003-35 - https://www.merl.com/publications/TR2003-35

Which is good for ~30 Baud and distances of inches at best.

https://people.ece.cornell.edu/land/courses/ece4760/FinalPro...

https://forum.chibios.org/viewtopic.php?t=2179

By @jIyajbe - 5 months
The reversed-biased method is actually the basis of single-photon detectors used in quantum optics experiments, especially experiments with entangled photons. Here's a paper that uses exactly this--reversed-biased red LEDs-- to introduce physics undergrads to the essential concepts:

https://advlabs.aapt.org/items/detail.cfm?ID=13808

It includes a simple circuit that probably anyone here on HN could build. (Even me!)

By @yonatan8070 - 5 months
I know someone who made digital candles using the technique for Hannukkah (Jewish holiday), there was a set of LEDs connected like shown in the post, and you could use your a flashlight (or a purpose built "shammash" LED) to turn each candle/LED on by touchibg it with a light, it was quite cool
By @frankus - 5 months
I remember a coworker telling me about a project that had mysterious battery drain after shipping.

It turns out the microcontroller was doing read-modify-write on a port that had an LED connected, and in bright sunlight the LED input would be high, and then stay high after the write. It was fixed with a firmware update that set the LED bit based on whatever logic determined if it should be on on every write.

By @lynndotpy - 5 months
Oh this is fun :) I knew LEDs and microphones could be used as light sensors (and vice-versa), but I would never know the specifics of how to do it. I appreciate this.

What other sensors/emitters can be reversed this way?

By @antirez - 5 months
Other two reversible effects that are not cited in the thread:

1. Peltier effect. 2. Piezoelectric effect.

The second one is especially useful in DYI stuff since you can use a piezo speaker in order to generate a current when mechanically stressed.

By @dmitrygr - 5 months
Basically a slightly less polished version of this: [1] [2]

The above paper goes further implementing two way comms using only a LEDs

[1] https://www.researchgate.net/publication/221568729_Very_Low-...

[2] https://courses.cs.washington.edu/courses/cse590u/03su/paper...

By @dfox - 5 months
Twenty years ago (when building community wireless networks was all the craze, at least here), we played with free-space optics and in course of that found out that random soviet (4mm!) green LED from parts bin makes exceptionally good receiver for the red laser LED from cheap laser pointer (driven by bunch of paralleled up HC04 gates). The contraption was not really practical, but across few meters of lab bench had higher bandwidth than we could measure with the surplus 50MHz CRO we had there.
By @floitsch - 5 months
I used this technique to send data from the cell-phone to an ESP32 using the phone's flashlight: https://www.reddit.com/r/esp32/comments/wfea4r/send_data_wit...
By @2sk21 - 5 months
Article like this are why I keep coming back to HN: I was using an LDR (Light Dependent Resistor) for a similar task but this LED idea is fascinating. It would have never occurred to me!
By @mikewarot - 5 months
Decades ago, I built a sensor that detected a break in a path to a retro-reflector up to 6 feet away using a single bi-color LED and some op amps, etc. I don't remember all the details... but I did learn how the photoelectric effect works, in my gut.

A red LED emits a lower energy photon than a green LED. Each will pick up their own reflections, but the green LED will not pick up the light from the red one.

By @DoingIsLearning - 5 months
> Certainly looks like that motion sensor is turning the light on in the night uselessly.

Interesting read John but since no one brought it up you should be careful to assume the sensor is at fault.

Hypothetically the timestamps in the example (roughly 00:00 to 04:00) sound like a fine time for rodents to roam and many motion sensors will also trigger on our small mass friends. :)

By @icpmoles - 5 months
I remember the OG pebble using the backlight LEDs as ambient light sensors but I can't find where I read it.
By @anfractuosity - 5 months
By @lolc - 5 months
Oh that was an interesting dive into LED peculiarities. And I did have an oddly behaving movement sensor too! It turned out to be mice. The sensor was just very sensitive.
By @kragen - 5 months
this was one of the first things i did with an avr. it's a little more challenging on an attiny2313, because it doesn't have an adc: http://canonical.org/~kragen/light_sensing/
By @mmoustafa - 5 months
> Certainly looks like that motion sensor is turning the light on in the night uselessly.

Have you seen the movie Parasite?

By @NietTim - 5 months
Bought a "proper" light sensor last week but would've fit my needs. D'oh!
By @zoky - 5 months
Well I guess if a generator is just a motor run backwards then this stands to reason…
By @brokenmachine - 5 months
Tricky. More than one way to skin a cat.
By @persnickety - 5 months
"the LED [...] will act as a capacitor that charges when light hits it."

Wait, what? Photodiodes are supposed to be letting (dark) current through continuously when light hits them in reverse bias. Heck, I even tested it empirically myself, and the light was continuous.

On this topic, what kind of light detector is the most linear? Photodiode? Phototransistor? Photoresistor? Something else?

I'm thinking about connecting it to a white LED to have an ambient-aware light.