Advertisement
  1. Computer Skills
  2. Electronics
Computers

How to Read Temperatures With Arduino

by
Difficulty:BeginnerLength:MediumLanguages:

Reading the temperature with an Arduino is an extremely useful function. It's the sort of function that is essential in many projects ranging from building your own home thermostat to creating a weather station. In addition it's simple enough to be implemented in a few minutes with any Arduino and just two simple components.

In this tutorial, I will show you how to use an Arduino to read temperature from a thermistor and print it on the serial port.  A thermistor is a simple electronic component that changes resistance based on the temperature. This tutorial focuses on the simplest and least expensive means for reading temperature. Along the way you will learn a simple, core building block of electronics that enables you to explore a whole world of sensors with your Arduino.


How Arduino Reads Temperature

There are several ways to read temperature with an Arduino. A few of these include:

  • I2C or Serial Sensors – There are advanced sensor modules that often can measure barometric pressure, temperature, humidity, and other conditions all in one package.  However these modules are typically much more expensive and require the use of the I2C or serial protocol to read. These might be great for a more advanced weather sensor project.
  • Thermal analog sensor – A three pin component that takes power, ground, and outputs a variable voltage based on the temperature by implementing a band gap core inside a single component.  This class of component is useful and I'll examine this in a future tutorial.
  • Thermistor – A resistor that changes resistance based on the ambient temperature.

This tutorial focuses on using the thermistor method for several reasons. First, it responds quickly to temperature changes, second, it's cheap and finally, it's easy to use.

There are also two very important concepts to be learned from this tutorial:

  1. Reading a value from an Arduino analog pin.
  2. Using a voltage divider circuit to read variable resistor sensors.

Variable resistor sensors are manufactured to measure all sorts of things, in the physical world, and the ability to read these sensors with your Arduino will be an excellent basic skill to master. Rotation, pressure, strain, flex, light, and heat are all examples of things you can measure using an analog pin and a voltage divider circuit.


Gather the Parts

For this project you will need

  • Breadboard
  • Arduino (An Uno is used in my examples, but any model should do)
  • 10K Ohm Thermistor
  • 10K Ohm Resistor (Brown, Black, Orange)
  • Hookup Wires
  • Computer with Arduino IDE installed (installing and using the IDE are not covered in this tutorial)
  • USB Cable (to connect Arduino and PC)

How It Works

Arduino analog pins read a voltage that is expected to range from 0V to 5V.  A standard way to turn a resistance change in a thermistor into a voltage change that the Arduino analog pin can read is to create a voltage divider circuit.  The circuit uses two resistor in a circuit of a known voltage to create a mathematically predictable voltage value: Vout.

It's a very simple circuit as shown below. As the R1 (resistor 1) value changes, Vout changes. In our tutorial R1 will be the thermistor and its value will change relative to the temperature. Vout is connected to our analog port on the Arduino so we can monitor it.

schematic-horizonal
Voltage divider circuit schematic

Enough theory, let's move on to building up the breadboard and Arduino.


Setting Up

Set up your breadboard and Arduino board like this diagram below.  The diagram was made with Fritzing a great tool for wiring up projects logically before grabbing wires and components.  The top, grey component is the thermistor, or R1, in the diagram above. This is one of many ways to wire up the circuit, I chose it because it complies with some good, basic breadboarding practices.

thermistor-setup-small
Breadboard layout with thermistor and voltage divider circuit

Programming The Arduino

Reading the analog pin on an Arduino is quite simple.  The pins labeled A0 - A5 on the Arduino are special pins that when read with the analogRead() function will return the value from 0 to 1023 where the input voltage is from 0V to 5V.  As the value of R1, the thermistor, changes based on the temperature, the voltage into the A0 pin will change predictably between 0V and 5V.

Let's write up some code and push it over to the Arduino.

  1. Plug the Ardunio into your computer with the USB cable
  2. Open the Arduino IDE
  3. Copy and paste the code below
  4. Press the Upload button to load the code into your Arduino
  5. Open up the Serial Monitor of the Arduino IDE by pressing CTRL SHIFT M or Selecting the menu Tools > Serial Monitor.

Tip: Make sure the baud rate of the Serial Monitor matches what we set in the setup() function.  In this example: 115200.

The output should look something like this:

463
463
463
463
463
463

Unless your breadboard is in a very hot oven, these values don't make sense.  That's because these are simply voltage samples translated into a scale from 0 to 1023.  Next, we need to turn these into a usable temperature value.


Converting Analog Values to Temperature

Above I mentioned that using the thermistor would be simple, and that's because we can stand on the shoulders of giants.  There is an equation to do the translation from sampled value to temperature called the Steinhart–Hart equation. (http://en.wikipedia.org/wiki/Thermistor)  The Steinhart-Hart equation has already been translated for the Arduino.  One examples of this can be found at playground.arduino.cc in an article by Milan Malesevic and Zoran Stupic.  I've illustrated their function Thermistor() below and added comments on how to use it.

  1. Copy and paste the code below into the Arduino IDE replacing the original example
  2. Click on the Upload button to push this code up to your Arduino.
  3. Open up the Arduino Serial Monitor window once again as it has vanished when you uploaded the code.

Now the output should look much more like this:

69.22
69.07
69.22
69.22
70.33
72.07
72.86
73.34
74.13

Now this makes sense.  My workshop is indeed 69 degrees Fahrenheit.  During this example, I touched the top of the thermistor with my finger and it sensed the temperature increase as you can see.

Try experimenting with your setup to get more comfortable with these new skills.  Here are some suggestions.

  • Reduce the delay value in the loop to see how fast the thermistor can react to temperature changes. (I don't suggest changing this below 50 or you might overflow your serial buffer.)
  • Try altering the program to get Celsius values (hint: read the comments in the code)
  • Modify the breadboard and code to use pin A1 instead of A0
  • Extra Credit: Reconfigure the circuit to use a 10K Ohm mini photo resistor and document analogRead() values based on lighting changes (hint: use the first code segment)

Summary

That's all there is to it.  Now you can go and create any manner of invention using a very inexpensive thermistor.

  • You have employed the skills of building a breadboard circuit
  • Compiling and uploading a sketch to your arduino

Additionally from this tutorial you have learnt how to:

  • read analog values from the Arduino using analogRead()
  • understand and manipulate the value returned from the analogRead() function
  • use a a voltage divider circuit to read changes to a resistor based sensor such as the thermistor
  • convert analog thermistor values into temperature values

While at first programming your Arduino to read and understand the world around it may sound complicated, in reality, there is a whole array of simple and inexpensive sensors available that will allow you to interface with the real world quickly and easily.  The voltage divider circuit and some simple code can give your next creation new and powerful senses.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.