The Arduino Inventor's Guide (35 page)

BOOK: The Arduino Inventor's Guide
11.58Mb size Format: txt, pdf, ePub

Without an additional
delay()
command in the
loop()
, the Arduino takes a sensor reading about 80–90 times per second and sends the information back to the computer. This is why you get a stream of readings rather than a single reading. If you want to slow down the rate at which the sketch prints the readings to the screen,
simply add a
delay(1000);
at the end of the
loop()
, just after the last
Serial.println();
. This will slow the loop to a single reading per second so it’s not streaming as fast. You’ll do that later in the code.

There’s one more step to get the sketch to show the temperature rather than volts.

Convert Voltage to Temperature

Now you need a formula to convert the volts to temperature. You know that the voltage the TMP36 outputs varies linearly depending on the temperature it senses, meaning that as the temperature changes, the voltage changes proportionately. Therefore, to create a formula, you use the
slope-intercept
equation:

y
=
mx
+
b

This equation describes a line or, more generally, a relationship between two variables
x
and
y
, where
m
is the slope of the line describing the rate of change, and
b
is the y-intercept, showing where the line intercepts the y-axis. In this case, the two variables
x
and
y
are the voltage and temperature, respectively, and you’ll use the known variable
x
(volts) to calculate the unknown variable
y
(temperature).

As mentioned, the datasheet for the temperature sensor states that the voltage changes at a rate of 0.010 V per degree Celsius, or 1 V per 100 degrees Celsius. This rate is the slope of the line and so becomes
m
in the equation. If you plug in the variables, the equation looks like this:

The datasheet for the TMP36 also provides a reference point for mapping voltage to degrees Celsius: at 25 degrees Celsius, the sensor has a voltage of 0.750 V. Substitute these numbers into the equation to solve for the y-intercept
b
:

So now you have values for
m
and
b
. Here’s the final equation:

This is a technique that you can apply to any number of sensors that have a linear relationship with voltage. It’s also a nice reminder that math is important and useful. But, if none of that made any sense, don’t worry—all you need to know is that to find the temperature of the sensor, you simply plug the voltage reading into this equation.

Now, use this equation in the code to convert the
rawVolts
reading to a temperature. You’ll work from the example code in Listing 7-3 and add the new code in
Listing 7-4
to show you the temperature reading from the sensor.

LISTING 7-4:
Converting from volts to temperature

  
//Example sketch – calculates the temperature from the TMP36
  
//sensor and prints it to the Serial Monitor
  
int rawSensorValue;
  
float rawVolts;

float
tempC;
  
float
tempF;
  
void setup()
  
{
    
Serial.begin(9600);  //initializes the serial communication
    
Serial.print("raw");
    
Serial.print("\t");  //tab character
    
Serial.print("volts");

   
Serial
.
print
(
"\t"
);
    
Serial
.
print
(
"degC"
);
    
Serial
.
print
(
"\t"
);
    
Serial
.
print
(
"degF"
);
    
Serial.println();
  
}
  
void loop()
  
{
    
rawSensorValue = analogRead(A0);   //read in sensor value
    
rawVolts = volts(rawSensorValue);  //convert sensor value
                                       
//to volts

   tempC = 100 * rawVolts – 50;  
//convert volts to deg. C

   tempF = 1.8 * tempC + 32;     
//convert deg. C to deg. F
  
--
snip
--
    
Serial.print(rawVolts);  //print raw voltage reading

   
Serial
.
print
(
"\t"
);
    
Serial
.
print
(tempC);
    
Serial
.
print
(
"\t"
);
    
Serial
.
print
(tempF);
    
Serial.println();        //new line character

   
delay
(1000);
  }
  
/***********************************************************/
  
float volts(int rawCount)
  
--
snip
--

First, add two lines in the global namespace at the top to declare variables to store the temperature in degrees Celsius (
tempC
) and in degrees Fahrenheit (
tempF
)

. Next, add a few lines in the
setup()
to use appropriate column headings for the readings printed to the Serial Monitor, separated by a tab character, which is represented by the control character
\t

. You can now use the slope-intercept equation to calculate the temperature in degrees Celsius

. You’ll also add one last line that converts the temperature from Celsius to Fahrenheit

.

And, for some additional feedback, add a few extra lines to
Serial.print()
to make sure you print the new variables

to the Serial Monitor. Notice that the very last
Serial.print()
line is actually a
Serial.println()
command that inserts a newline character and moves the cursor to the next line. This will make sure each new reading starts on a new line. Finally, add the 1-second delay

to slow down the loop so that the sensor is sampled only once per second and you have time to read the text.

Upload the updated code to your Arduino, and open the Serial Monitor. You should see four columns of data scroll up the screen, as shown in
Figure 7-11
. As the new column headings denote, these figures represent raw data, voltage, temperature in degrees Celsius, and temperature in degrees Fahrenheit. If you squeeze the temperature sensor with your fingers, you should notice that the temperature increases. Congratulations, you have a working temperature monitor, the first big part of this project!

FIGURE 7-11:
Serial Monitor displaying temperatures from the TMP36

BUILD THE SERVO MOTOR AUTOVENT

You’ll use a servo motor like the one you used in
Project 6
to open and close a window on the greenhouse. A servo is a simple motor that uses three wires for the control signal (yellow or white), power (red), and ground (black) connections, as shown in
Figure 7-12
.

FIGURE 7-12:
Adding the servo motor to the circuit

Most standard servos have a three-pin female connector. Connect three male-to-male jumper wires to these three pins to extend these connections, as shown in
Figure 7-13
. If possible, match the colors of your jumper wires to the leads on the servo connector. Then, connect the yellow (or white) signal wire to pin 9 on the Arduino board. Connect the red wire to 5 V and the black wire to GND on the right side of the breadboard.

FIGURE 7-13:
Inserting male-to-male jumper wires to connect the servo to the breadboard

Other books

El cuadro by Agatha Christie
Visions Of Paradise by Tianna Xander
Shadows of New York by Heather Fraser Brainerd
The Seahorse by Michael Aye
The Devil Behind Me by Evangelene
Hidden Fire by Alexis Fleming
The Moon Spun Round by Gill, Elenor
Small Steps by Louis Sachar