Activity - Interfacing Analog Light Sensor With Arduino
Activity - Interfacing Analog Light Sensor With Arduino
Objective:
x
Let’s try it out by choosing the most
basic analog sensor of all, the LDR.
The Light Dependent Resistor (LDR)
lo
or a photoresistor is exactly what its
name suggests. It is a resistor whose
resistance changes according to the
amount of Light falling on it. So,
working principle of an LDR is
itB
simple:
So, in today’s activity we will learn how to take Light Intensity as an analog Input
using Arduino. These analog values for the changing light intensities, once read, will
be sent over to the Computer via the USB using the Arduino’s Serial Monitor Utility.
And finally two different outputs in the form of Red and Green LEDs are controlled.
Materials Required:
x
2 Breadboard 1
lo
itB
3 M-M Connection Wires 10
W
x
lo
itB
8 LDR 1
W
x
lo
itB
W
Explanation:
The connection diagram shown in the image above has the Light sensing
component (the LDR) as the Analog Input and two LEDs (Red and Green) with their
individual resistors as the Outputs.
The LDR is connected such that its one terminal is connected to the +ve of the
breadboard with the 1k series resistor in the middle. The second terminal of the LDR
is connected to one of the terminals of the 10k resistor while the other side of the
10k resistor is connected to the -ve of the Breadboard.
The junction, where the LDR meets the 10k resistor, is connected to the Arduino
x
Analog pin 2 (A2) and is shown by the Orange line.
The Red LED is connected such that its +ve terminal is connected to the Arduino’s
lo
digital Pin 2 (D2) and the -ve terminal is connected to the 1k resistor. The other side
of the 1k resistor is connected to the -ve of the Breadboard.
The Green LED is connected such that its +ve terminal is connected to the Arduino’s
digital Pin 3 (D3) and the -ve terminal is connected to the 1k resistor. The other side
itB
of the 1k resistor is connected to the -ve of the Breadboard.
Lastly, the Arduino’s 5v pin is connected to the +ve of the breadboard and the Gnd
pin is connected to the -ve of the breadboard.
Arduino Code:
W
The Arduino code is simple but needs to be done in 2 parts. Let's see why. We have
1 analog input whose value can range from some minimum value to some maximum
value. And we have 2 outputs, one of which we want to turn On (and the other
remains Off) when the present value of the sensor is below a given threshold (just a
fancy word for limit). We want the second Output to turn On (and the first remains
Off) when the present value of the sensor is above the given threshold.
If we don't know where to place this threshold, we won’t be able to set when the
Red LED goes On or Off and when the Green LED goes On or Off. So first, we need
to find out the value of this threshold. For this, we simply send the value of the
sensor continuously to the Computer using ”Serial.println();”.
Here is that initial code:
void setup()
{
pinMode(LDR,INPUT); // LDR pin as Input
void loop()
x
{
light_value = analogRead(LDR); //read LDR pin and store in variable
lo
delay(200); // wait for 200ms
itB
Explanation:
Here, we are telling the Arduino that from now on, we will call pin A2 as LDR and
then take an Integer type variable “light_value” for storing the analog value.
void setup()
{
pinMode(LDR,INPUT); // LDR pin as Input
Here, inside the setup function, we are declaring that we are going to use LDR (pin
2) as Input and that we are beginning a serial communication with the Computer at a
speed of 9600 bits per second (bps).
void loop()
{
light_value = analogRead(LDR); //read LDR pin and store in variable
1. Reading the LDR pin and storing its value in the “light_value” variable.
2. Printing the value of this variable on the serial monitor (computer’s USB).
3. Waiting for a short time to make sure the sent value is printed for sure.
x
Once this code is compiled and uploaded, we will see a continuous stream of live
values printed on the serial monitor terminal. These values will change suddenly
lo
when we place our hand above the LDR, casting a shadow on it. These values will
again change suddenly when we move our hand away from the LDR.
We are looking for this approximate value on one side of which we will call it dark
and on the other side of it we will call it bright. This value need not necessarily be
itB
the same for everyone. It will vary from sensor to sensor and place to place
depending on how much light or shadow is at a given place.
Let’s say we found this threshold value to be 200. So now, we will turn On Red on
one side (greater than) of this value and turn On Green on the other side (less than)
of this value.
So, we will use two sets of “if” conditions to compare the present value to the
W
threshold. Then, depending on whether the value is less than or greater than 200 we
will “digitalWrite();” the Red and the Green LEDs accordingly. Given below is the
complete Arduino code for doing the process that was just described.
void loop()
{
light_value = analogRead(LDR); //read LDR pin and store in variable
x
Serial.println(light_value); // send stored value to computer
delay(200); // wait for 200ms
lo
if(light_value > 200)
{
}
digitalWrite(Red,HIGH);
digitalWrite(Green,LOW);
// if value more than threshold
Here, we added the comparison part using the “if”. One “if” compares if the value is
Greater than 200 while the other “if” compares if the value is Less than 200.
1. If the present measure analog value is greater than 200, the Red LED is
turned On and the Green LED is turned Off.
2. If the present measure analog value is less than 200, the Red LED is
turned Off and the Green LED is turned On.
Outcome and Observations:
1. Once the Arduino code is compiled and uploaded, start the serial monitor by
selecting the proper baud rate number. You will see values being displayed
continuously one below another.
2. If we move our hand such that it casts a shadow on the LDR light sensor, the
value of the analog readings decreases and as we keep moving our hand
closer and closer to the LDR light sensor, the value keeps decreasing and
finally when the LDR light sensor is made completely dark, the value drops to
minimum.
x
3. Now, if we keep moving our hand farther and farther away from the LDR light
sensor, the value of the analog readings keeps increasing and finally there
comes a point where the value no longer changes even if we move our hand
lo
farther.
4. If, however, we take the setup to a brighter room or outdoors, the total range of
analog values will vary more as the amount of light available is more.
itB
5. If we change the 100k resistor with a 10k resistor, the entire analog value
range shifts down.
6. If we want the setup to behave in the opposite way i.e. moving the hand closer
should reduce the analog readings and moving the hand farther should
increase the readings, then we need to take the inverse of the “light_value”
variable.
W
light_value = analogRead(ldr);
Adding this line will reverse the behavior of the setup. Now, more light will
make lower readings and less light will make higher readings.