Thursday, November 17, 2011

Sparkfun Inventor's Kit Circuit-10: Measuring Temperature

The MIT Sea Perch Sensor Suite that we will be using with the students of Venice High School and the Washington Math Science Technology Public Charter School as we "pimp our Perches" to give them autonomy, will use an Arduino board to measure water temperature, depth, light and conductivity.  In the previous Sparkfun Inventor's Kit Circuit Tutorial they showed us how to measure light and use its intensity to control an LED and a servo using a Photo Resistor.  In Circuit-10 we get to play around with a Precision Temperature Sensor.

The temperature sensor looks identical to a transistor.  It fact it looks so much like the two transistors supplied in the Inventor's Kit that you have to be really careful not to confuse them.  Use a magnifying glass and you can see that the temperature sensor has the tiny letters TMP inscribed on the flat side. The other two say P2N2 222A.  So use the one that doesn't say P2N2 222A. Use the one that says TMP (if you can see it, it is so tiny).

This is "a rather complicated IC (integrated circuit) hidden in a package identical to our P2N2222AG transistors. It has three pins, ground signal and +5 volts and it is easy to use. It outputs 10 millvolts per degree centigrade on the signal pin (to allow measuring temps below freezing there is a 500 mV offet, e.g. 25 C = 750 mV so 0 C = 500 mV)." So sayeth the manual.

This tutorial uses the Arduino IDE's serial monitor so that we can see how it converts the millivolts to degrees.
The TMP36 Datasheet can be found at http://ardx.org/TMP36. The code can be downloaded from http://ardx.org/CODE10

The build on this one is really simple; you just plug in the temp IC and connect the right lead to ground, the middle lead to Arduino's Analog 0 pin and the left lead to 5 Volts. Then you paste the code in the sketch and upload it and open the serial monitor (the icon is a square with an antenna).

Mine starts spitting out data saying it is between 21.78  and 22.27 degrees C in the room.
When I hold the black casing of the IC between my fingers it quickly starts to rise in temperature; after a couple of seconds it is over 26 degrees.

The manual says if you want to see the voltage instead of the temperature, delete the line (or comment it out) that says temperature = (temperature - .5) * 100;

Now when I run it the serial monitor shows the room temperature as .73 which must be 730 mV.  When I touch the sensor with my fingers it goes up to .8 within a few seconds.
We can make the serial monitor display the temperature in Fahrenheit by recalling the formula
F = (C * 1.8) +32).  We add a line that says "temperature = (((temperature - .5) * 100) * 1.8) + 32;" and stick it before Serial.println(temperature);

Instead of doing that, because I'm getting more and more confident hacking the code myself, I wrote
void loop()                     // run over and over again
{
 float temperature = getVoltage(temperaturePin);
 //getting the voltage reading from the temperature sensor

  float temperatureC = (temperature - .5) * 100;          //converting from 10 mv per degree wit 500 mV offset
  float temperatureF = (((temperature - .5) * 100) * 1.8) + 32;                                                //to degrees ((voltage - 500mV) times 100)
 Serial.println(temperatureC);
  Serial.println(temperatureF);
 //printing the result
 delay(1000);                                     //waiting a second
}


That way I got a display that showed me both Celsius and Fahrenheit (and note, I had to declare new floats for my new variables temperatureC and temperatureF which I defined with the math.)

The manual says we can create more informative output by changing Serial.println(temperature); to read  Serial.print(temperature);Serial.println(" degrees centigrade"); and it says "the change to the first line means when we next output it will appear on the same line, then we add the informative text and a new line."

I made my code read the following:

void loop()                     // run over and over again
{
 float temperature = getVoltage(temperaturePin);
 //getting the voltage reading from the temperature sensor

  float temperatureC = (temperature - .5) * 100;          //converting from 10 mv per degree wit 500 mV offset
  float temperatureF = (((temperature - .5) * 100) * 1.8) + 32;                                                //to degrees ((volatge - 500mV) times 100)
 Serial.print(temperatureC);
 Serial.println(" degrees Centigrade");
  Serial.print(temperatureF); Serial.println(" degrees Fahrenheit");
 //printing the result
 delay(1000);                                     //waiting a second
}


Now my serial monitor tells me the room is at "22.7 degrees Centigrade" and "72.08 degrees Fahrenheit." Cool... er...warm!

Finally the manual shows us how to make the data transmission to the serial monitor 12 times faster by changing the baud rate from 9600 to 115200.  Change Serial.begin(9600) to Serial.begin(115200) or any value you want and watch the serial monitor fly!
Oops, when I run it I get garbage output, but that is because I haven't changed the speed in the pull down menu at the bottom of the serial montor to 115200.  So I do that and... voila!






No comments:

Post a Comment