It turns out that many electronic components can run backwards and forwards. For example, it’s possible to use headphones as a microphone. Here’s a simple Arduino-based circuit that shows how to use an LED as a light sensor. The diagram below shows how to hook everything up.
The basic idea is then to use
analogRead() to detect the voltage across the LED. If the value is low, it means the LED is not detecting much light, while if the value is higher then there is more light present.
Three different versions of the code that goes along with this circuit is available at https://github.com/eholk/ArduinoExamples/tree/master/LEDLightSensor. The SerialOutput folder has a sketch that just dumps the values it reads to the serial port. You can use this to find a good threshold value, depending on the exact light conditions you are seeing. The Threshold sketch simply turns the LED on pin 13 off when there is light, and on otherwise. You could use this as a simple night light. Finally, the Fading sketch adjusts the intensity of the LED based on how much light is detected. It requires the output LED to be connected to a PWM pin, so if you are using an Arduino Uno instead of a Mega, you’ll want to adjust the code and the circuit to use, for example, pin instead of pin 13 for the output.
Here’s a video of the fading example.