Hardware Reference
In-Depth Information
Using this circuit, you'll make a simple temperature alert system. The light
will glow green when the temperature is within an acceptable range, will turn
red when it gets too hot, and will turn blue when it gets too cold.
First things first, you need to ascertain what values you want to use as your
cutoffs. Using the exact same sketch from Listing 3-1, use the serial monitor
to figure out what analog values correspond to the temperature cutoffs you
care about. My room is about 20 $ C, which corresponds to an analog reading
of about 143 . These numbers might differ for you, so launch the sketch from
before, open the serial terminal, and take a look at the readings you are getting.
You can confirm the values mathematically using the graph from Figure 3-7.
In my case, a value of 143/1023 corresponds to a voltage input of about 700mV.
Deriving from the datasheet, the following equation can be used to convert
between the temperature ($C) and the voltage (mV):
Temperature($C) n 10 = voltage (mV) - 500
Plugging in the value of 700mV, you can confirm that it equates to a tempera-
ture of 20 $ C. Using this same logic (or by simply observing the serial window
and picking a value), you can determine that 22$C is a digital value of 147 and
18 $C is a digital value of 139 . Those values will serve as the cutoffs that will
change the color of the LED to indicate that it is too hot or too cold. Using the
if statements, the digitalWrite function, and the analogRead function that
you have now learned about, you can easily read the temperature, determine
what range it falls in, and set the LED accordingly.
NOTE BeforeyoucopythecodeinListing3-2,trytowritethisyourselfandsee
whetheryoucanmakeitwork.Aftergivingitashot,compareitwiththecodehere.
Howdidyoudo?
Listing 3-2: Temperature Alert Sketch—tempalert.ino
//Temperature Alert!
const int BLED=9; //Blue LED on pin 9
const int GLED=10; //Green LED on pin 10
const int RLED=11; //Red LED on pin 11
const int TEMP=0; //Temp Sensor is on pin A0
const int LOWER_BOUND=139; //Lower Threshold
const int UPPER_BOUND=147; //Upper Threshold
int val = 0; //Variable to hold analog reading
void setup()
{
pinMode (BLED, OUTPUT); //Set Blue LED as Output
pinMode (GLED, OUTPUT); //Set Green LED as Output
Search WWH ::




Custom Search