How to Use Temperature Sensors on the Arduino - Ultimate Guide to the Arduino #37
### How to Set Up and Program a Thermistor with Arduino: A Step-by-Step Guide
Thermistors are versatile components that can measure temperature or control devices based on temperature changes. In this guide, we’ll walk you through how to set up and program a thermistor using an Arduino board. We'll also show you how to use the readings from the thermistor to control an LED.
---
#### What Is a Thermistor?
A thermistor is a type of resistor whose resistance changes with temperature. Unlike fixed resistors, thermistors are variable, meaning their resistance decreases (for Negative Temperature Coefficient, NTC) or increases (for Positive Temperature Coefficient, PTC) as the temperature rises. For most Arduino projects, we use NTC thermistors.
The resistance of a thermistor at 25°C is specified by its R25 value. The thermistor used in this guide has an R25 value of 10 kilo ohms and a beta value of 3435.
---
#### Setting Up the Circuit
To measure the temperature using a thermistor, we need to create a voltage divider circuit. Here's how:
1. **Components Needed**:
- Thermistor (10kΩ NTC)
- Resistor (10kΩ, same as R25 value of your thermistor)
- Arduino board
- Breadboard and jumper wires
2. **Circuit Connection**:
- Connect one lead of the thermistor to 5V.
- Connect the other lead of the thermistor to a resistor (10kΩ).
- The free end of the resistor connects to ground (GND).
- The middle point of the voltage divider (between the thermistor and resistor) connects to an analog input pin (A0) on your Arduino board.
**Note**: Ensure that the resistor value matches the R25 value of your thermistor for accurate readings.
---
#### Understanding the Steinhardt-Hart Equation
The relationship between resistance and temperature for a thermistor is not linear—it’s logarithmic. To calculate the exact temperature from the thermistor's resistance, we use the Steinhardt-Hart equation:
\[
\frac{1}{T} = a \ln(R) + b (\ln(R))^3 + c
\]
Where:
- \( R \) is the resistance of the thermistor.
- \( T \) is the temperature in Kelvin.
- \( a, b, c \) are constants specific to the thermistor.
To find these constants, we need three resistance values at different temperatures. These can be obtained from your thermistor's datasheet or measured using a multimeter. For our 10kΩ NTC thermistor:
- At -40°C: \( R = 190,953 \Omega \)
- At 25°C: \( R = 10,000 \Omega \)
- At 105°C: \( R = 858.33 \Omega \)
Using an online calculator, we find the constants:
- \( a = 0.00749146 \)
- \( b = 0.0203924 \)
- \( c = -0.0031217 \)
---
#### Writing the Arduino Sketch
Here’s the complete code for reading temperature from the thermistor and displaying it on the serial monitor:
```cpp
int thermistorPin = A0;
float R1 = 10000; // Value of resistor in voltage divider (same as R25)
float a = 0.00749146; // Steinhardt-Hart coefficient
float b = 0.0203924; // Steinhardt-Hart coefficient
float c = -0.0031217; // Steinhardt-Hart coefficient
void setup() {
Serial.begin(9600);
pinMode(5, OUTPUT); // LED control pin
}
void loop() {
int val = analogRead(thermistorPin); // Read voltage drop across thermistor
float R2 = (R1 * 1023) / (val - 1); // Calculate resistance of thermistor
// Apply Steinhart-Hart equation
float logR2 = log(R2);
float logR2Cube = pow(logR2, 3);
float TK = 1 / (a + b * logR2 + c * logR2Cube);
// Convert Kelvin to Celsius and Fahrenheit
float TC = TK - 273.15;
float TF = (TC * 9) / 5 + 32;
// Print temperature readings
Serial.print("Temperature: ");
Serial.println(TC);
Serial.println('C');
delay(500); // Wait for 500ms before next reading
// Control LED based on temperature
if (TC > 29) {
digitalWrite(5, LOW); // Turn off LED when temperature is above 29°C
} else {
digitalWrite(5, HIGH); // Turn on LED when temperature is below 29°C
}
}
```
---
#### Testing the Circuit
1. **Reading Temperature**:
- Open the serial monitor in Arduino IDE.
- The temperature reading should match your ambient temperature.
- Hold the thermistor to increase its temperature and observe the readings.
2. **Controlling the LED**:
- The LED will turn on when the temperature drops below 29°C and off when it exceeds 29°C.
- Test by heating or cooling the thermistor.
---
#### Applications of Thermistors
Thermistors are widely used in various applications, including:
- Over-temperature protection for motors and electronics.
- Temperature control in incubators, ovens, and plant monitors.
- Humidity and temperature sensing in IoT devices.
---
#### Next Steps
In the next video, we’ll explore another method to measure temperature using a digital sensor. If you’re interested in learning more about Arduino and electronics, check out the [3-in-1 Smart Car and IoT Learning Kit](#) from SunFounder, which includes everything you need to build projects like a remote-controlled car or a plant monitor.
---
By following this guide, you’ll be able to measure temperature using a thermistor and control devices based on temperature readings. Happy experimenting!