Выбрать главу

Figure 7-13: The TMP36 pinout

The positive supply voltage to pin V+ on the TMP36 can be anything between 2.7V and 5.5V. On its middle pin, the chip produces an analog output voltage proportional to the temperature. The temperature of the chip (in degrees Celsius) can be calculated from the voltage at the Out pin by this formula:

Temperature = 100 × volts - 50

So, if the voltage were 0.6V, the temperature would be 100 × 0.6 - 50 = 10 degrees Celsius. If you prefer your temperatures in degrees Fahrenheit, then just make one further calculation:

°F = °C × 9/5 + 32

The TMP36 can measure temperatures in the range -40 to +125 degrees Celsius, but the measured temperature is accurate only to within 2 degrees Celsius of the actual temperature.

STEP 1: MAKE A LONGER LEAD FOR THE TMP36

To extend the lead of the TMP36, you could just solder a three-core wire to it. However, to make it a bit more durable, you can use heatshrink tubing on top of the soldered connections. Figure 7-14 shows the process.

Figure 7-14: Using heatshrink on the TMP36 lead

First, strip the wires of each lead and slip the cut lengths of heatshrink over the individual wires (Figure 7-14a). Next solder the wires to the leads of the TMP36 (Figure 7-14b). Slide the heatshrink up over the solder joint (Figure 7-14c) and finally apply a hair dryer or hot air gun to the heatshrink until it, well, shrinks (Figure 7-14d). If you have wide diameter heatshrink, then you could place this around the whole sensor and individual leads to make this build more durable.

NOTE

For more information on using heatshrink, see the “Using Heatshrink” on page 235.

STEP 2: ATTACH THE TEMPERATURE SENSOR LEAD TO THE SCREWSHIELD

Attach the wires from the temperature sensor to the screwshield (Figure 7-11). You don’t have to use the GND connection shown; any of the GND terminals on the screwshield will do.

SOFTWARE

If you want to make this project on its own, without any of the earlier Arduino-based projects, then open the sketch Project_12_Temperature from this book’s source files and load it on to your Arduino now. If, on the other hand, you built one or more of the earlier Arduino projects, then use the sketch All_Sensors and change the constants at the top to select the projects that you have made. See the comments section in this sketch for instructions on this.

NOTE

All the source code for this book is available from http://www.nostarch.com/zombies/. See “Installing the Antizombie Sketches” on page 248 for instructions on installing the programs.

This code follows the same pattern as Project 4, so for more information on how the program as a whole works, please refer to “Software” on page 57. Here, I’ll just describe the code specific to this project.

First, a new constant is defined for the Arduino pin that will act as an analog input for the TMP36:

const int tempPin = A2;

Two more constants are defined to set the maximum and minimum temperatures allowed before an alarm is triggered. These are floats rather than ints because they represent decimal numbers rather than whole numbers.

// Project 12 constants

// these can be in C or F

const float maxTemp = 45.0;

const float minTemp = -10.0;

As the comments above the constants state, these temperature values can be in either Celsius or Fahrenheit. The units that the temperature is measured in are decided by a new function you’ll define.

The main loop function now includes a call to checkTemp, too. This function is defined as follows:

void checkTemp()

{

  float t = readTemp();

  if (t > maxTemp)

  {

    alarm("HOT", t);

  }

  else if (t < minTemp)

  {

    alarm("COLD", t);

  }

}

The checkTemp function first calls readTemp to measure the temperature and then compares that with the maximum and minimum temperatures. If the temperature is too high or too low, then the alarm function is called. Note that this version of the alarm function has an additional parameter that is used to display the temperature on the LCD screen.

The readTemp function is where the raw analog input reading from the TMP36 is converted into a temperature.

float readTemp()

{

  int raw = analogRead(tempPin);

  float volts = raw / 205.0;

  float tempC = 100.0 * volts - 50;

  float tempF = tempC * 9.0 / 5.0 + 32.0;

  // One of the following two lines must be uncommented

  // Either return the temperature in C or F

  return tempC;

  // return tempF;

}

The raw value returned by analogRead is a number between 0 and 1023, where 0 indicates 0V at the analog input pin and 1023 indicates 5V. This voltage is calculated by dividing the raw value by 205 (205 is roughly 1023/5).

The temperature in degrees Celsius is then calculated using the formula described in “TMP36 Temperature Sensor” on page 133, as the voltage multiplied by 100 with 50 subtracted from the result. The temperature in degrees Fahrenheit is also calculated.

Finally, one of these two values has to be returned. In this version of readTemp, the line to return tempF is commented out, so the temperature in Celsius will be returned. If you want to flip this, then comment out the line return tempC and un-comment return tempF so that the last three lines of the function look like this:

  // return tempC;

  return tempF;

}

To test the sensor, try changing the value of the maxTemp constant to just above the room’s temperature, load the updated sketch onto the Arduino, and then squeeze the temperature sensor between your fingers to warm it up. Watch the LCD, and the readout should change.

USING THE TEMPERATURE ALARM

There’s a limit to how much distance you can put between your temperature sensor and your Arduino. You could make the lead you attach to the TMP36 as long as 20 feet (7 m), but the sensor will become less and less accurate as the lead gets longer due to electrical noise on the line and the resistance of the wire.

Leave the sensor near the item you want to stay at a certain temperature and watch the LCD. If that wine cellar just won’t stay cool enough, try setting up the sensor in different rooms in your base until you find one with the right climate. If there isn’t a good room for the wine, just put the sensor back on your generator, invite the other survivors in your area over for a drink, and have an antizombie strategy meeting.

Now that you have a bunch of sensors to warn you of dangers in your base, in the next chapter, you’ll combine the Arduino projects with a Raspberry Pi to make a control center.