Here, we are going to start Physical Computing by controlling an LED using a light sensor. There will be two circuits which are very similar; First – LED lights up when there is light, and Second – LED lights up in the absence of light. Before we go on to the Arduino, i want to show and make this using the primitive techniques – Voltage Regulation, Transistors, etc.




You will need the following:

Qty. Product Value Specifications
1 LDR (Light Dependent Resistor)
1 LED (Light Emitting Diode) Forward Current, If = 20mA
1 Transistor BC547 – NPN
1 Resistor 220Ω or 330Ω both will work, but with 220Ω, LED will be more brighter
1 Resistor 1kΩ or 1000Ω or 1K
1 Resistor 10kΩ

Plus all the components from the Voltage Regulator circuit on a BreadBoard for converting your unregulated power supply to a circuit friendly voltage (Regulated 5V).



Case 1: LED lights up in the absence of light, i.e., in a dark room.

Case 2: LED lights up when there is light in the room and turns off if there is no light.




For both the cases, the circuit will be same. The LED is optional. If you really want some lights to flicker in your room, you can put it in pin 13 of your Arduino board with a 220Ω or 330Ω resistor as shown in the figure.


Case 1:

/*

  From Darkness to Light (Analog Input using an LDR)
 
 Demonstrates analog input by reading an analog sensor
 (here, an LDR) on analog pin 0 and turning on and off
 a light emitting diode (LED) connected to digital pin 13.
 The amount of time the LED will be on and off depends on
 the value obtained by the LDR using the analogRead() function.
 
 The circuit:
 * A Light Dependent Resistor (LDR) attached to analog input 0
 * one pin of the LDR is connected to the analog pin 
   and also through a 1K resistor to the ground
 * other pin of the LDR to +5V
 * Built-in LED at Pin 13 or an external LED connected as follows:
     LED anode (long leg) attached to digital output 13
     LED cathode (short leg) attached to ground
 
 Created by David Cuartielles
 Modified 4 June 2011
 By Explore Labs
 
*/

int ledPin = 13;            // the default pin where Uno's LED is connected
int ldrPin = A0;            // input pin for the LDR
int ldrVal = 0;             // variable to store value coming in from the analog pin

void setup()
{
  //Serial.begin(9600); // Debug: for serial communication to view output in your computer
  pinMode(ldrPin, INPUT);   // not necessary
  pinMode(ledPin, OUTPUT);  // declare pin 13 as output
}

void loop()
{
  ldrVal = analogRead(ldrPin);    // read value coming from the LDR

  //Serial.println(ldrVal, DEC); // Debug: un-comment if you want to see the output
                                  // in Serial Monitor

  digitalWrite(ledPin, HIGH);     // turn the ledPin ON
  delay(ldrVal);                  // wait for milliseconds
  digitalWrite(ledPin, LOW);      // turn the ledPin OFF
  delay(ldrVal);                  // wait for milliseconds
}

Case 2:

For the second case, i.e., to light up the LED in the presence of light, no change is done in the circuit. Only the code needs a little rework.

/*

  From Darkness to Light (Analog Input using an LDR)
 
 Demonstrates analog input by reading an analog sensor
 (here, an LDR) on analog pin 0 and turning on and off
 a light emitting diode (LED) connected to digital pin 13.
 The amount of time the LED will be on and off depends on
 the value obtained by the LDR using the analogRead() function.
 
 The circuit:
 * A Light Dependent Resistor (LDR) attached to analog input 0
 * one pin of the LDR is connected to the analog pin 
   and also through a 1K resistor to the ground
 * other pin of the LDR to +5V
 * Built-in LED at Pin 13 or an external LED connected as follows:
     LED anode (long leg) attached to digital output 13
     LED cathode (short leg) attached to ground
 
 Created by David Cuartielles
 Modified 4 June 2011
 By Explore Labs
 
*/

int ledPin = 13;            // the default pin where Uno's LED is connected
int ldrPin = A0;            // input pin for the LDR
int sensorVal = 0;          // variable to store value coming in from the analog pin
int ldrVal = 0;             // variable in which the rework, i.e., mapping is done
void setup()
{
  //Serial.begin(9600); // Debug: for serial communication to view output in your computer
  pinMode(ldrPin, INPUT);   // not necessary
  pinMode(ledPin, OUTPUT);  // declare pin 13 as output
}

void loop()
{
  sensorVal = analogRead(ldrPin);    // read value coming from the LDR
  ldrVal = map(sensorVal, 0, 1023, 1023, 0);  // to change the range of values from
                                              // 1023 to 0 (default was 0 to 1023) 

  //Serial.println(ldrVal, DEC); // Debug: un-comment if you want to see the output
                                  // in Serial Monitor

  digitalWrite(ledPin, HIGH);     // turn the ledPin ON
  delay(ldrVal);                  // wait for milliseconds
  digitalWrite(ledPin, LOW);      // turn the ledPin OFF
  delay(ldrVal);                  // wait for milliseconds
}

After uploading the codes to the board, if you want to see the output in the Serial Monitor, just uncomment the two lines of code starting with the keyword Serial. Then, while Arduino is running with any of the above codes, click on Serial Monitor.

The Serial Monitor showing the result of Case 1 with real time changes in values of the light falling on the LDR. (shown on Arduino 0022)





The Arduino has taken the world of electronics and microcontrollers by a storm. Due to Open Source, many makers and DIYers are following it and the Arduino community has become one of the largest community of DIYers in the world. The Arduino hardware reference designs are distributed under a Creative Commons Attribution Share-Alike 2.5 license and are available on the Arduino Web site.

There are many different variants of the Arduino board available in the market with the ever increasing number of shields growing day by day.

Arduino is the best example of “Physical Computing” meaning interacting with the real world objects using software and hardware. Physical Computing is a subject taught in many universities across the world.
References

Circuits were Made with Fritzing. For more circuit examples, see the Fritzing project page
Code – Analog Input from Arduino

Creative Commons License

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.