This page will provide a more detailed technical explanation about the circuit we built and the code we used. It will also offer a couple of ways to troubleshoot your DIY thermometer.
We mentioned that thermistors change resistance as operating temperatures change. Therefore, to measure changes in temperature, we’ll need a way to measure changes in the thermistor resistance.
Before we go further, notice that we are using an Arduino microcontroller. The problem with Arduinos is that they do not have the ability to measure resistance directly. Luckily, they do have a voltage reader.
In order for the Arduino to output the temperatures we’re interested in, the first thing we have to do is measure the change in voltage due to the changes in the thermistor’s resistance.
To do this, we will build a voltage divider:
The variable we are interested in is RT, the resistance of the thermistor, which is directly correlated to the temperatures we have to measure
Here is the relevant equation for a voltage divider:
Explanation of variables
The Vin is 5V because we are supplying voltage from the 5V pin on the Arduino
The RS is our “known” resistor. Its resistance value will be fixed/constant.
The Vout is the voltage that passes through this circuit that gets read by the Arduino
RS, the resistance of the known resistor, should be roughly equal to the base resistance of the thermistor. RS is R1 in our code. Make sure to adjust the value in the code (line 3) provided based on the base resistance of your resistor.
Notice how we used five 10k Ohms resistors arranged in series. This is because the Sidekick kit does not have a 50k Ohm resistor. By arranging the five resistors in series, we are able to get the 50k Ohms that we need. To calculate the total resistance of resistors in series -- just add their values:
Remember when we talked about connecting the thermistor to Arduino pin A0?
This means that the value of the resistor is read using one of the analog input pins on the Arduino. This is done in line 1 in the code.
The analog value is read as a voltage and the voltage range is then converted to a digital value (known as the ADC value, which ranges between 0 and 1023) using the analogRead() command in the code.
We then take the ADC values, which are essentially the voltage values except in a different form, and convert that to resistance using the voltage divider equation above while subbing the below equation in for Vout in the voltage divider equation.
Analog Voltage Measured = Vout
This is done in the code in line 14: R2 = R1 * (1023.0 / (float)Vo - 1.0);
R2 is the same as RT and R1 is the same as RS
Now that we have the resistance values, how do we convert that to temperature?
We have to use the Steinhart-Hart equation (line 16 in code):
Notice the temperature output is in kelvin; this is easily converted to Fahrenheit (done in lines 17-18 in the code).
Also note that R, the resistance, should be in ohms. This should not be an issue if you don't have to modify our code, but if you do, please keep these units in mind.
a, b, and c are the Steinhart-Hart coefficients (line 5)
Please see the section titled "Calibrating Your Thermistor" on this page to find out how to find the Steinhart-Hart coefficients. These are specific to your thermistor!
The last few lines (lines 20-24) in the code just prints the results out so that you can view them in the serial monitor.
In order to convert resistance to temperature, we have to use the Steinhart-Hart equation. The coefficients used in this equation are specific to the type and model of the thermistor you're using for this DIY. Although these coefficients are usually published by the manufacturer, they can also be derived if needed.
If you can find a datasheet published for your thermistor that includes the resistance of the thermistor at different temperatures, use a Steinhart-Hart coefficient calculator online (like this one for example) to get the a, b, and c values to use in the code.
If you absolutely cannot find any information about your thermistor, you will have to take three measurements of the thermistor resistance at three precise temperatures. Using these three data points and a Steinhart-Hart coefficient calculator, fit the three coefficient values.
Here is a code using Arduino that will output the resistance of the thermistor. The circuit will be the same as the one detailed in the page titled "Building an Arduino Thermometer."
What if you don't have a thermometer or if you want to make sure the temperatures you're using are precise? We'll have to start by discussing some very basic thermodynamic principles if this is the case.
Consider phase changes, like ice melting to liquid water or liquid water transforming into steam. These phase changes occur at a constant temperature.
We know some common temperatures; for example, ice melts at 0 °C and water boils at 100 °C.
This means that in order for ice and water to coexist, or for water and steam to coexist, the temperature must be 0 °C and 100 °C respectively.
So what we'll do is we'll leverage this idea of thermodynamic equilibrium so that we can get precise temperatures to calculate the coefficients we need for the Steinhart-Hart equation.
Here are the steps to follow if you want to use the thermodynamics method.
Run the code above in Arduino IDE and make sure your circuit is ready.
Prepare your equilibrium systems:
Grab some ice in a container and allow it to melt until you have ice and liquid water. Take the thermistor (covered with saran wrap), and put it in the ice water. Wait until the values displayed on the serial monitor is relatively constant. Record resistance value.
Do the same for boiling water. Be careful not to burn yourself.
Plug data into calculator to find a, b, and c coefficients.
Unfortunately, this method still requires you to have one more temperature reading since phase changes only count for two. Perhaps use the ambient temperature outside or the thermostat in your house as a another data point. Just make sure to allow the thermistor to equilibrate before you start recording resistance values!
Here are some possible issues you might've encountered:
The temperature values outputted by the code make absolutely no sense and are not remotely close to appropriate (ie. it's reading 1 degree Fahrenheit at room temperature).
This is most likely due to an error in your code. Make sure that the R1 value (line 3 in the code) matches the thermistor you're using and that your Steinhart-Hart coefficient values (line 5) are calibrated for your specific resistor.
The values outputted by the code make sense, but they are inconsistent or fluctuates often.
This could be due to a bad connection between your wires and the breadboard or Arduino. Make sure wires are secured. You can also try the second code detailed in the instructions page that takes the average of the readings so that the values fluctuate less. If you used tape or hot glue to extend your thermistor wires instead of soldering, make sure that you twisted the thermistor wires around the connecting wires at least two times so that it's secured and well-connected.