A little known fact about LEDs (Light Emitting Diodes) is that when hooked up backwards, they act as photodiodes. I have manipulated this property to make a few fun and cheap Arduino projects, including a musical instrument I call a photo-theremin.
I am going to begin with my understanding of physically how this works. Bear with me -- my 4th grade teacher once told my mother that she was certain I’d never be an artist, and it shows in my drawings. If you’re not interested in exactly how it works, feel free to scroll past my crude diagrams. If you’re just interested in the photo-theremin, you will find it at the bottom of this post.
Hooked up 'correctly'
This my very simplified way of thinking of diodes, if you want a better and accurate explanation, I suggest looking at this article on HowStuffWorks.
My understanding is that particles there are positively charged (P-type) and negatively charged (N-type) sides on the diode. In the middle, there is what is called a “depletion zone”. This can be simplified to a positive side, a negative side, and a “hole”. When the voltage drop is in the correct direction of the diode, the negatively charged particles are repelled by the negatively charged wire, and fills the hole. In an LED, as they cross the gap, they release electromagnetic energy, a photon, or light.
Hooked up backwards
When a diode is hooked up backwards, the positive side is now connected to the negatively charged wire, so instead of going across the wire, the particles are attracted to the edges and no current flows.
LEDs exhibit an interesting property: photons can excite the LED and allow some current to flow through. Notice in the diagram above, the LED is hooked up backwards, but the photon has excited the electron to fill the gap between the P-type and N-type, allowing a bit of current to flow. In essence, the LED becomes a photodiode.
So how can we use this with the Arduino?
Light sensitive dimmed blinking LED
My first project with the reverse LEDs was a photosensitive blinking light. This is a very simple arduino circuit, requiring only a single LED and a current limiting resistor (100Ω). The LED is lighter when the room is lighter.
I found the basis of the project on Arduino Playground – LEDSensor. If I were to do this project over again, I’d look into using analogRead and analogWrite to achieve the same effect, probably more efficiently.
Here is my code; the negative side of the LED is hooked up to pin 2, the positive to pin 3.
In this example, we are reading the LED by treating it somewhat like a capacitor. We “charge” it up by letting turning the LED on, and then quickly turn it off and perform a digital read on it. As light hits it, the voltage will change slowly as the current is allowed through it, and eventually we reach the threshold to a digital LOW. By counting the clock cycles it took to switch to a digital LOW, we can determine how much light is shining on the LED.
The second half of the loop function is dedicated to dimming the LED. When we turn the LED off and on very fast, it looks to our human eye like the LED is on continuously but dim.
To create a photo-theremin, you could do it with only an LED and a piezo buzzer. These are cheap components you could get at even radioshack for less than $5. In my example, I am additionally using a push button to play the notes, as well as a potentiometer (variable resistor) to control the volume of the piezo buzzer. These are also very cheap components you could easily get.
For this example, to read the LED we are going to use the Arduino’s analogRead function which is much easier than digitally reading the voltage like in the example before. The analogRead function directly gives us a relative voltage (if the voltage were 5V, analogRead would return 1023; 2.5V would read 512; 0V would read 0).
To play music through the piezo buzzer, we are going to use the Arduino’s tone function which will output a square wave frequency to the piezo buzzer. To generate a frequency corresponding to a note, I found that the equation 440 * 2^(n/12) will generate a frequency n notes above A440 Pitch Standard. If n is a double, you can generate any sound, but if n is an integer, this equation will generate only on-pitch notes. I went a step further with the help of my friend Sam Jacobs, and my photo-theremin will only produce notes on a defined musical scale.
Here is my code; the LED is hooked from A0 to GND, my push button is connected to pin 7, and my piezo element to pin 8.
Feel free to comment or shoot me an e-mail if you have any questions or ideas.