We previous discussed
Digital Inputs (here) and While switches and buttons are great, there’s a lot
more to the physical world than on and off. Even though the Arduino is a
digital tool, it’s possible for it to get information from analog sensors to
measure things like temperature or light. To do this,
you’ll take advantage of
the Arduino’s built-in Analog-to-Digital Converter (ADC). Analog in pins A0-A5
can report back a value between 0-1023, which maps to a range from 0 volts to 5
volts.
One of the components we may
find in our kit is the Photoresistor otherwise called the Light Dependent
Resistor. This component outputs a changing resistance depending on the
light it senses. It’s resistance decreases
as the intensity of light increases and the current increases as the light
intensity increases. In the sketch for this Project, you’ll read the sensor’s
output and use it to turn LEDs on and off, indicating presence of light.
The Arduino IDE comes with a tool called the
serial monitor that enables you
to report back results from the microcontroller. Using the serial monitor, you
can get information about the status of sensors, and get an idea about what is
happening in your circuit and code as it runs. The serial monitor can be opened
by pressing the icon on the top right corner of the taskbar in the Arduino IDE.
We should have something like this if we open our serial monitor right now.
Now let us build the circuit
The Photoresistor is not polarized so there are
no positive or negative terminals/ends; one terminal/end is connected to analog
pin 0 (you can decide to use any analog pin) and it runs through a grounded 1K
ohm resistor. The other end is connected to 5V on the right side of the breadboard.
Then we have two LEDs connected to pins 13 and 12 respectively. We also have
220 ohm resistors to limit the flow of current to the LEDs.
Code
You can copy the code here and paste in your Arduino
IDE, however you should note that certain modifications may be made depending
on the intensity of light in the environment you are building this project.
int light = 0; // store the current light value
void setup() {
// put your
setup code here, to run once:
Serial.begin(9600); //configure serial to talk to computer
pinMode(13,
OUTPUT); // configure digital pin 13 as an output
pinMode(12,
OUTPUT); // configure digital pin 12 as an output
}
void loop() {
// put your
main code here, to run repeatedly:
light =
analogRead(A0); // read and save value from PR
Serial.println(light); // print current light value
if(light >
450) { // If it is bright...
Serial.println("It is quite light!");
digitalWrite(13,LOW); //turn left LED off
digitalWrite(12,LOW); // turn right LED off
}
else if(light
> 229 && light < 451) { // If it is average light...
Serial.println("It is average light!");
digitalWrite(13, HIGH); // turn left LED on
digitalWrite(12,LOW); // turn
right LED off
}
else { // If
it's dark...
Serial.println("It is pretty dark!");
digitalWrite(13,HIGH); // Turn left LED on
digitalWrite(12,HIGH); // Turn right LED on
}
delay(1000);
}
Now I will use this opportunity to introduce you to the
concept of serial monitors. Looking at the void
setup() part of the code, you can see the first code written is Serial.begin(9600);. This opens up a
connection between the Arduino and the computer, so you can see the values from
the analog input
on your computer screen. The argument 9600 is the speed at which the
Arduino will communicate, 9600 bits per second. You will use the Arduino IDE’s
serial monitor to view the information you choose to send from your
microcontroller. When you open the IDE’s serial monitor verify that the baud
rate is 9600.
Then light =
analogRead(A0);, here we use the analogRead
function to read values from the Photoresistor connected to our analog pin A0, the values we read are now stored
in a variable called light which we declared
at the start of the program.
If the Photoresistor value is greater than 450, i.e if(light > 450), it prints “It is quite light” on the serial monitor and all the LEDs turn OFF
Else If the value lies between 230 and 450 i.e else if(light > 229 && light
<451). It prints “It is average
light” on the serial monitor and
one LED (pin 13) turns ON. This sign
(&&) depicts the “AND” operation and we use it to combine
two or more conditions which must be true before the code in the if() statement runs. There are other
logical operations like OR which we
will get to later on.
The last part of the code caters for light values lower
than 230 i.e (light< 230), so we
just write this in our else()
statement. We do not need to add the condition for this as this is the only
other outcome left and we do not add conditions to the else() statement. The else()
statement caters for anything the if()
or else if() statement do not answer
true to. The serial monitor prints “it
is quite dark” and both LEDs turn ON.
It is important
to know that you may have varying values from mine depending on the light
intensity in the environment, if you do; I suggest you read the values from the
photo resistor first and test for varying conditions such as high presence of
light and absence of light. Then you should open the serial monitor and watch
how the values increase or decrease depending on the presence or absence of
light, this will give you a fair judgment of values you need to use as your
thresholds in the if() and else if() statements. You can run this code for that
purpose;
int light = 0; // store the current light value
void setup() {
// put your
setup code here, to run once:
Serial.begin(9600);
}
void loop() {
light = analogRead(A0);
Serial.println(light);
Delay(500);
}
I am certain you will be able to pull this off no matter
the intensity of light in your environment. We have used this project to
understand the concept of analog inputs and how we can take certain action
depending on the input, a lot of real life situations involve analog input so
it is very necessary we understand it so we can take appropriate action. Ciao.
No comments:
Post a Comment