Example 09: Temperature (Thermistor and Temp Sensor)

Learn how to measure temperature using different sensors.

Hardware Required

  • TI LaunchPad

  • Breadboard BoosterPack

  • Breadboard

  • Thermistor

  • LM19 Temperature Sensor

  • 6x Jumper Wires

  • 10K resistor

Relevant Groundwork

In this example we will learn more about sensors. In some of the previous examples we have used components as basic sensors. Sensors are components that can sense the physical world. One common attribute of the physical world that we would like to keep track of is temperature. If things get too hot or too cold it can be bad. We can use formulas to calculate the temperature of an object or environment based on changes in resistance of the sensor.

We are also going to learn how to use serial communication. The LaunchPad can talk to your computer with a protocol called UART. We can pass lines of text (called strings) between the computer and the LaunchPad via the USB cable. In the example, we are going to read messages from the LaunchPad about the data of the temp sensors. In Energia there is a tool called the Serial Monitor, which is the magnifying glass in the upper right corner. When you open the tool, you will get a window called a terminal. Here, if your Serial Port is correctly selected and your bit rate is properly set, you can see messages from the LaunchPad. The messages are ones that you program using the Serial library. Using the Serial library APIs, we can print text that can tell us values of variables, clues to where we are in the program, and many other useful things.

In this circuit we will use a thermistor and an analog temperature together and compare the results. A thermistor is a variable resistor that changes with temperature. This can be good enough to measure temperature in a lot of products but often we need to be more precise. For better results, there are all sort of analog temperature sensors available that are purpose built to measure temperature with accuracy. The temperature sensor we will use is the basic, but effective LM19 from Texas Instruments. You can find the datasheet here: http://www.ti.com/lit/ds/symlink/lm19.pdf. Circuit & Schematic

With Breadboard BoosterPack

800

With LaunchPad

800

Code

/*
  Example 09: Temperature (Thermistor and Temp Sensor)
  Sidekick Basic Kit for TI LaunchPad

  In this example we are going to compare the results of a thermistor
  and an Analog Temperature Sensor. A thermistor is a very basic way
  to get temperature data. It is a simple device that changes its
  resistance based on temperature. For more accuracy, you can use an analog
  temperature sensor like the LM19 but there are many different types
  of temperature sensors available.

  Here we will build a simple circuit with both sensors and then read
  the values from the Energia Serial Monitor.

  Hardware Required:
  * TI LaunchPad
  * Breadboard BoosterPack
  * Breadboard
  * Thermistor
  * LM19 Temperature Sensor
  * 10K resistor (optional)
  * 6x Jumper Wires

  The LM19 Example is by Kerry Wong
  http://www.kerrywong.com/2010/05/09/working-with-lm19-temperature-sensor/
  Modified by Mark Easley

  This example code is in the public domain.
*/

// For the Thermistor: Attach VCC to left leg, an AnalogRead capable pin (2) to right leg
// with a 10K resistor to GND in series.
// For the Temp Sensor: Attach VCC to the left leg, an AnalogRead capable pin (6) to the
// middle leg, and GND to the right leg. The letters and flat face should be facing you.
// Check the datasheet if you want to verify the correct pin connections.
// This example uses pin 2 and 6 on your LaunchPad, but can be changed to any analog pin.
// WARNING: the LM19 will get very hot to the touch if you have it plugged in backwards!

/* In the setup function we will initialize the Serial library
 * which let's us send and receive data over the UART channel
 */
void setup()
{
  // initialize serial communication over UART at 9600 baud
  Serial.begin(9600);
}

/* In the loop section we will take measurements from our two sensors
 * perform any mathematical formulas to convert to the temperature,
 * and then print out the information to the console (serial monitor).
 */
void loop()
{
  // >>> Thermistor Section <<<
  // take in analog data from thermistor and run it through formulas
  // and conversions

  float ThermistorVal = analogRead(2);
  float ThermistorTempC;
  float ThermistorTempF;

  ThermistorTempC = logf(10000.0 * ((1024.0 / ThermistorVal - 1)));
  //         = logf(10000.0 / (1024.0 / ThermistorVal - 1)); // for pull-up configuration
  ThermistorTempC = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * ThermistorTempC
                    * ThermistorTempC ))* ThermistorTempC );
  ThermistorTempC = ThermistorTempC - 273.15;            // Convert Kelvin to Celcius
  ThermistorTempF = (ThermistorTempC * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
  Serial.print("Thermistor: ");
  Serial.print("vin=");
  Serial.print(ThermistorVal);
  Serial.print(" ");
  Serial.print("TempC=");
  Serial.print(ThermistorTempC);
  Serial.print("  ");
  Serial.print("TempF=");
  Serial.print(ThermistorTempF);
  Serial.println();


  // >>> End of Thermistor Section <<<

  // >>> LM19 Section <<<
  // take in analog data from LM19 temp sensor and run it through
  // formulas and conversions

  // multiply analog read by VCC which is 3.3V on LaunchPad
  // divide that result by ADC range (256 for 8 bit, 1024 for 10 bit,
  // 4096 for 12 bit) which is the max resolution of the analog pin

  // Approximated linear transfer function from datasheet
  // slightly less accurate but good at typical temps
  float vin = 3.3 * analogRead(6) / 4096.0;
  Serial.print("LM19: ");
  Serial.print("vin=");
  Serial.print(vin);
  Serial.print("  ");
  // plug in value to celcius temp equation
  float tempC = (1.8663 - vin) / 0.01169;
  // run celcius to fahrenheit conversion
  float tempF = 1.8 * tempC + 32.0;
  // print temperature to serial
  Serial.print("tempC=");
  Serial.print(tempC);
  Serial.print("  ");
  Serial.print("tempF=");
  Serial.println(tempF);
  Serial.println();
  delay(1000);

  // More accurate parabolic formula from the datasheet
  // Covers full temperature range of LM19
  /*
  float vin = 3.3 * analogRead(6) / 4096.0;
  Serial.print("LM19: ");
  Serial.print("vin=");
  Serial.print(vin);
  Serial.print("  ");
  // Datasheet Formula: tempC = sqrt((2.1962 * 10^6) + ((1.8639 - vin)/(3.88 * 10^(-6)))) - 1481.96
  float tempC = sqrt(2196200 + ((1.8639 - vin) / 0.00000388)) - 1481.96;
  float tempF = 1.8 * tempC + 32.0;

  // print temperature to serial
  Serial.print("tempC=");
  Serial.print(tempC);
  Serial.print("  ");
  Serial.print("tempF=");
  Serial.println(tempF);
  Serial.println();
  delay(100);
  */

  // >>> End of LM19 Section <<<
}

Programming Challenge

Can you change how the temperature text is displayed in the Serial monitor? Use the Serial.print() and Serial.println() functions to clean up the output.

Troubleshooting

Code not uploading?

  • Check for errors in Energia debug window. The compiler will tell you what is happening. Errors are in red text.

  • Sometimes your LaunchPad gets stuck or hung up on the previous code. Unplug your LaunchPad and plug it back in to perform a full reset. This is called a power on reset. Sometimes using the RESET button can work but taking away the power and letting the microcontroller fully reset is often best.

  • If you have a failure to upload it could be your drivers are not properly installed. Energia will sometimes give the error "No unused FET Found" which means it can’t find a LaunchPad connected to your computer. Make sure you download the drivers for your operating system found on the Getting Started Guide.

  • If you had no problem with the previous Blink example, your Energia should be correctly set up. Restart your LaunchPad and restart Energia IDE if you encounter any problems. Make sure to select the right serial port and board type under the Tools menu.

Nothing is happening?

  • You need to open the Serial Monitor in the Energia IDE to see the program running. Click the magnifying glass icon in the upper right corner or go to Tools > Serial Monitor.

Nothing showing on the Serial Monitor? Weird character are showing on the Serial Monitor?

  • Make sure you have the correct Serial Port for UART selected.

  • Make sure you have the right baud rate selected in the Serial Monitor. Should be 9600 for this example.

Temperature value is wrong? Temperature sensor getting very hot?

  • Check that your temperature sensor pins are correct. You may have put it in backwards.

For additional support, try searching the Energia forums on 43oh.com. We believe in you to figure out any problems, now believe in yourself and find the solution!

Guide Home