I welcome you to my next blog post as part of Experimenting with Thermistors Design Challenge. In previous 1st blog post I described my plans as part of this competition and in second blog post I started my series of 3 mini tutorials showing usage of thermistors with Arduino. In this blog post I will continue and today I will automate measurement process which I recently did manually using multimeter. In this blog post I will measure resistance of thermistor using Arduino and evaluate measured resistance using table of corresponding temperatures from Molex Thermistor Datasheet.
Connection
The first step is measuring resistance of thermistor. The easiest way is using voltage divider and analog pin. Voltage divider works in way that voltage on wire between two resistors depends on ratio of resistor values. In our voltage divider one of the resistors is thermistor with variable resistance and second resistor is static resistor with known value. Resistor with static known value should be accurate as much as possible, but in case of inaccurate resistor you can manually adjust value in code later. One end of the voltage divider connects to 5V of Arduino and second to the GND. Middle terminal of voltage divider will face variable voltage depending on ratio of thermistor actual value and known resistor value. This voltage is sensible by ADC of Arduino, so connect it to the A0 or other analog pin of Arduino. Value of resistor depends on thermistor value and required range of operation. For Molex thermistor 215272-3707 which I use in this project I chosen 2k2 but many other values will also work well. Schematically connection looks as follows:
In real world I interconnected components using terminal block. I did not use breadboard this time. It is good to reduce resistance of wires and joins as much as possible because they affect accuracy of measurements. Connection look as follows:
Converting voltage to resistance
The next step is finding formula which we use for converting measured voltage to resistance. Generic formula for computing voltage drop of resistor as part of voltage divider is following:
In our case R2 is thermistor:
Now we need to express Rth from formula. R1 is constant (2k2) and U is constant (5V on Arduino).
After placing known constants (R1=2k2, U=5V):
And we have simple formula expecting voltage equivalent to voltage on A0 pin and outputting resistance of thermistor which we can use for measuring temperature.
Arduino Code
Now we can use all knowledge from previous paragraphs to write Arduino code.
In setup function you only need to initialize Serial port:
Serial.begin(9600);
Before we start coding our application logic, let’s declare constants used in circuit. VCC is voltage used for powering Arduino and R1 is value of static resistor in voltage divider:
float R1 = 2200.0; float VCC = 5.0;
In loop function we will read analog pin. This we can do using analogRead(A0) call. This function provide value in range from 0 to 1023 corresponding to measured voltage. We will divide it by 1023.0 (float) which will convert measured value to decimal number in range between 0 and 1. After this we will scale this value to real voltage range by multiplication value with ADC reference which is the same voltage used for powering Arduino. It is 5V under normal conditions. In code this looks as follows:
float voltage = analogRead(A0) * VCC / 1023.0;
Then we use formula from previous part:
float thermistorResistanceOhm = R1 * (VCC - voltage) / voltage;
And for getting more friendly numbers we can convert resistance from ohms to kiloohms:
float thermistorResistanceKOhm = thermistorResistanceOhm / 1000.0;
For now, the most important part is completed. We can print measured resistence:
Serial.print("Thermistor resistance: " + String(thermistorResistanceKOhm, 3) + ". ");
In this tutorial I will use table from datasheet for finding corresponding temperature. I gathered all 36 values from table and wrote them to the arrays. Array temperature contains temperature points and resistance array contains corresponding resistances for these points.
// values matches typcial values of Molex 215272-3707 (12kOhm @25 deg C) #define REFERENCE_POINTS_COUNT 36 float temperature[REFERENCE_POINTS_COUNT] = {-40, -35, -30, -25, -20, -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135}; float resistance[REFERENCE_POINTS_COUNT] = {365.3, 267.1, 197.3, 147.2, 110.8, 84.26, 64.57, 49.84, 38.73, 30.24, 23.78, 18.82, 14.98, 12, 9.664, 7.823, 6.365, 5.204, 4.274, 3.533, 2.935, 2.449, 2.053, 1.728, 1.461, 1.241, 1.059, 0.907, 0.779, 0.672, 0.581, 0.505, 0.439, 0.384, 0.329, 0.274};
At first, we should check that measured value is not outside range of valid values. In this case we can report temperature above or below allowed limits:
if (thermistorResistanceKOhm > resistance[0]) { Serial.println("Temperature lower than " + String(temperature[0], 0) + " deg C!"); } else if (thermistorResistanceKOhm < resistance[REFERENCE_POINTS_COUNT - 1]) { Serial.println("Temperature higher than " + String(temperature[REFERENCE_POINTS_COUNT - 1], 0) + " deg C!"); }
Then add else part to the condition and in for loop we can check to which temperature range measured temperature corresponds to:
for (int i = 0; i < REFERENCE_POINTS_COUNT - 2; i++) { if (thermistorResistanceKOhm < resistance[i] && thermistorResistanceKOhm > resistance[i + 1]) { Serial.println("Temperature is between " + String(temperature[i], 0) + " and " + String(temperature[i + 1], 0) + " deg C."); } }
Finally, we can add some delay before making next measurement:
delay(100);
And this is all. We can compile and deploy program to Arduino and see output on Serial monitor.
Testing
After starting you should see similar output on the serial monitor:
For an experiment I tried to place thermistor to the cup of hot water like I did in previous part and output changed as expected:
Summary
This is all from this blog post. Thank you for reading it. In this blog I shown and described very simple method of measuring resistance of Molex thermistor using Arduino and implemented very simple conversion of this resistance to the temperature. Program was very minimalistic and later I will use more advanced and accurate approaches of measuring temperature using thermistor. In next blog post and third part of this thermistor tutorial series I will use the same measurement circuit, but for computing temperature I will use Steinhart-hart equation instead of lookup table which I used in this program. Below this paragraph you can download Arduino sketch which I created as part of this minitutorial. Constants and temperature in the sketch are computed for 215272-3707 thermistor. It is thermistor #8 from the kit used as part of this contest. It is the only thermistor with red wire and blue epoxy in the kit. Running the program with different thermistor connected will result to wrong values. If you want to use different thermistor, than you need to replace temperature and resistance arrays.
Thank you for reading this blog post. I welcome any feedback, so feel free to comment about this tutorial in comments below.
Top Comments