martes, 18 de noviembre de 2014

LED as sensor

Led as sensor



Controlled by Arduino, we are using a single yellow led as both light and sensor of light.
Five times per second we reverse the polarity of the led, charging it.
If there is light, photons are going to interact with the electrons discharging it faster.

We then measure the time it takes for the digit pin to read a logical 0.
If this zero is reached before some threshold time, we know there is light.
If it takes a lot of time, there are no photons helping our electrons cross the band gap of the diode.


 Arduino Code

//
// This example shows one way of using an LED as a light sensor.
// You will need to wire up your components as such:
//
//           + digital11
//           |
//           <
//           > 180 ohm resistor
//           <
//           |
//           |
//         -----
//          / \  LED, maybe a 5mm, clear plastic is good
//         -----
//           |
//           |
//           + digital12
//
// What we are going to do is apply a positive voltage at LED N Side and
// a low voltage at LED P Side. 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 LED P Side 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 
// LED N Side.
//

#define LED_N_SIDE 11
#define LED_P_SIDE 12
#define MAX_COUNT 180

void setup()
{
}

void lightMode()
{
  // Turn the light on
  pinMode(LED_N_SIDE,OUTPUT);
  pinMode(LED_P_SIDE,OUTPUT);
  digitalWrite(LED_P_SIDE,HIGH);
  digitalWrite(LED_N_SIDE,LOW);
}

void sensorMode()
{
  // Apply reverse voltage, charge up the pin and led capacitance
  pinMode(LED_N_SIDE,OUTPUT);
  pinMode(LED_P_SIDE,OUTPUT);
  digitalWrite(LED_N_SIDE,HIGH);
  digitalWrite(LED_P_SIDE,LOW);

  // Isolate the LED N Side of the diode
  // Turn off internal pull-up resistor
  pinMode(LED_N_SIDE,INPUT);
  digitalWrite(LED_N_SIDE,LOW);
}

boolean isDark = true;

void loop()
{
  if (isDark)
  {
    lightMode();
    delay(200);
    sensorMode();
  }

  isDark = true;
  for( byte j = 0; j < MAX_COUNT; j++ )
  {
    if ( digitalRead(LED_N_SIDE) == 0 ) 
    {
      isDark = false;
      break;
    }
  }
}


I became first aware of this technique browsing through the Arduino Playground
http://playground.arduino.cc/Learning/LEDSensor. I tried implemented it with different leds and resistences but I could not make it work.

Then I found LED as a Light AND a Light Sensor - Arduino controlled which made this possible.
Special thanks to Ido Gendel for his code and patience.