Controlling a digital output using a LDR.

Introduction: The LDR (Light-Dependant Resistor) is a resistor type that varies your resistance value according to the light that converges over your surface. The Photoelectric Phenomenon, that makes the photoconduction in the LDR possible, was explained for the first time in 1905 by Albert Einstein. He shown that the light, converging over a metal-like surface, can be accepted as a stream of energetic particles called Photons and these particles do have electric charge.

What happens in a LDR is that the light that converges over his surface increases the conductibility of the material (or reduces the resistance). Thus, the electrons on the electric current that flows over the LDR terminals can move more easily.

LDR

Components needed:

→ One LDR (Light-Dependant Resistor);

→ One LED (Light Emissor Diode);

→ One 330 Ohm resistor;;

→ Protoboard;

→ Jumpers.

1st Step:

On a Protoboard, assemble the circuit below:

Proto2

Where:

→ Red wires and black wires referes to VCC 5V and GND, respectively;

→ The Yellow wire connects the output voltage from the LDR, to be read by an ADC (in this case, A0);

→ The orange wire connects the LED resistor to the Digital Output (D7).

Schematic

Schem2

2nd Step:

Upload the following code to Galileo, using the Arduino IDE or compiling directly from Linux terminal:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
    const int pinLDR = A0;
    const int pinLed = 7;
    int valorref=400; //The reference value to switch between HIGH/LOW
       
    void setup() {
        pinMode(pinLed, OUTPUT); //Set D7 (LED) as OUTPUT.               }
    void loop () {
      int valorSensor = analogRead(pinLDR); /*LDR is connected to A0 */
      if(valorSensor         digitalWrite(pinLed, HIGH);
       } else
        {
        digitalWrite(pinLed, LOW);
         }
    }

3rd step:

To modify the led switching to ON when light focus over LDR surface (and keep it for 10s) you just have to change the void loop () to:

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
    void loop () {
      int valorSensor = analogRead(pinLDR); /*LED is connected to A0 */       if(valorSensor        digitalWrite(pinLed, LOW);
       } else
        {
        digitalWrite(pinLed, HIGH);
        delay (10000);          }
    }

This method is widely used in parking lots and stairways.