My first attempt at using the Arduino (Duemilnove). This sketch is based on this and that but mine uses 2 LEDs, one for the sensor and one for the feedback. And instead of getting bright when more ambient light is present, it turns OFF if ambient light is bright, stays ON in the dark and blinks if it’s in between.
//
// This example shows one way of using an LED as a light sensor.
// You will need to wire up your components as such:
//LED #1:
// + digital2 (N side)
// |
// <
// > 100 ohm resistor
// <
// |
// |
// -----
// / \ LED #1, 5mm, clear plastic is good (or translucent green lense)
// -----
// |
// |
// + digital3 (P side)
//
//////////////////////////////////
//LED #2 (only required if you don't have the on-board LED on pin 13):
// + digital13 (P side)
// |
// |
// -----
// / \ LED #2
// -----
// |
// |
// + Ground
//
// What we are going to do is apply a positive voltage at digital2 and
// a low voltage at digital3. This is backwards for the LED, current will
// not flow and light will not come out, but we will charge up the
// capacitance of the LED junction and the Arduino pin.
//
// Then we are going to disconnect the output drivers from digital2 and
// count how long it takes the stored charge to bleed off through the
// the LED. The brighter the light, the faster it will bleed away to
// digital3.
//
// Then just to be perverse we will display the brightness back on the
// same LED by turning it on for a millisecond. This happens more often
// with brighter lighting, so the LED is dim in a dim room and brighter
// in a bright room. Quite nice.
//
//
#define LED1_N_SIDE 2 // original code uses pin 2
#define LED1_P_SIDE 3 // original code uses pin 3
#define LED2 13 // LED 2 is on-board or external on pin 13 and ground
//
// -1 = very dark ; 1 = very bright
int extreme = 0;
//
void setup() {
pinMode(LED1_N_SIDE, OUTPUT); // N side is output or input but starts w/ output
pinMode(LED1_P_SIDE, OUTPUT); // P side is alway output pin (drive low or high)
pinMode(LED2, OUTPUT);
}
//
void loop() {
unsigned int waitDischarge, extenLimit;
//
// Apply reverse voltage, charge up the pin and led capacitance
digitalWrite(LED1_N_SIDE,HIGH);
digitalWrite(LED1_P_SIDE,LOW);
delay(30);
//
// Isolate the pin 2 end of the diode
pinMode(LED1_N_SIDE,INPUT);
digitalWrite(LED1_N_SIDE,LOW); // turn off internal pull-up resistor
//
// Turn LED on if ambient light is very dark or medium
if (extreme <= 0) {
digitalWrite(LED2,HIGH);
}
//
// Count how long it takes the diode to bleed back down to a logic zero
for ( waitDischarge = 0; waitDischarge < 65534; waitDischarge++) {
if ( digitalRead(LED1_N_SIDE)==0) {
// very dark (you might need to sligthly change this number depending
// on the color of the lense of LED #1 for example)
if ( waitDischarge > 6 ) extreme = -1;
// very bright (you might need to sligthly change this number depending
// on the color of the lense of LED #1 for example)
else if ( waitDischarge < 3 ) extreme = 1;
else extreme = 0;
break;
}
delay(30); // to avoid going above 65534
}
//
// Turn LED off if ambient light is very bright or medium
if (extreme >= 0) digitalWrite(LED2,LOW);
delay(200);
}