Temperature and Humidity Sensors Compared: the short answer
Temperature sensing is the first measurement most projects need, and each common sensor suits different jobs. 3 V boards use a different sensor. Example: The Uno reads 28 °C in steps of about 0.5 °C. Switching to analogReference(INTERNAL) improves resolution for readings up to 110 °C.
- Level
- Beginner
- Time
- About 6 min
- Topic
- Sensors and actuators
Choose between LM35, DS18B20, DHT22, AHT20/SHT31 and thermistors, and wire each one correctly.
Before this: Sensor Interfaces
Temperature sensing is the first measurement most projects need, and each common sensor suits different jobs. The LM35 is an analog sensor that outputs 10 mV per °C (250 mV at 25 °C); it needs 4 V or more to run, so on 3.3 V boards use a different sensor. An NTC thermistor is a resistor whose value falls as it warms; in a divider with a fixed resistor it is cheap and fast but needs a formula (the Beta equation) to convert to °C. The DS18B20 is a digital sensor on the 1-Wire bus with ±0.5 °C accuracy, available in a waterproof probe, and many can share one pin, each with its own ID.
For humidity as well as temperature, the DHT11 is the cheapest but coarse (±2 °C, ±5 % RH, one reading per second). The DHT22 (AM2302) is better (±0.5 °C, ±2 to 5 % RH) but slow, reading only every two seconds. Both use a single-wire timing protocol that is sensitive to long cables. Newer I2C sensors such as the AHT20, SHT31 and SHT40 are more accurate, faster and easier to read, and they share the bus with displays and other sensors.
Placement matters more than the sensor. A sensor on the same small board as an ESP32 reads several degrees high because the chip and regulator warm it. Keep it away from the board on short wires, give it air flow and shade from sunlight, and avoid enclosing it in a sealed box. For humidity, wait a few minutes after power-up and keep the sensor out of condensation.
Different sensors, different strengths
An analogue sensor such as the LM35 gives 10 mV per degree Celsius and needs only an ADC. A thermistor changes resistance with temperature, and you read it in a divider and convert the reading with the Steinhart–Hart or beta equation. The DS18B20 is digital, sends the temperature over one wire, and several can share the same wire.
For humidity as well as temperature, the DHT22 or a more accurate chip such as the SHT31 or BME280 can be used. The BME280 also reads pressure over I²C or SPI.
Accuracy and placement
A sensor reports its own temperature, not the air's. If it sits near a regulator, a processor or a hot LED, it reads high, and a board mounted in a closed box shows the box's heat. Mount it away from heat sources, with some air around it, and for outdoor use shield it from the sun.
Accuracy figures differ: the DS18B20 is typically within about half a degree, while a cheap analogue part and a noisy ADC can be worse. Calibrate against a trusted thermometer if you need better.
Reading well
Allow time for the sensor to respond, since a sensor in a protective case reacts slowly. Take a few readings and average them, and check for the error values that some sensors return when they fail, such as −127 °C on the DS18B20 when it is disconnected.
- Mounting the sensor next to a heat-producing part.
- Treating an error value such as −127 as a real temperature.
- Assuming the number of decimal places shown is the accuracy.
- R = R25 × e^(B(1/T − 1/298.15))
- NTC thermistor, T in kelvin
Resistance against temperature
Work it with your own numbers: NTC thermistor
Read an LM35 on an Arduino Uno
- LM35 output is 10 mV per °C, so 28 °C gives 0.28 V.
- The Uno's 10-bit ADC with a 5 V reference gives 5 ÷ 1,024 ≈ 4.9 mV per step.
- 0.28 V reads as about 57 steps, so temperature = reading × 4.9 ÷ 10 ≈ 27.9 °C.
- Each step is about 0.5 °C; using the 1.1 V internal reference makes each step about 0.1 °C.
Result. The Uno reads 28 °C in steps of about 0.5 °C. Switching to analogReference(INTERNAL) improves resolution for readings up to 110 °C.
Read a DS18B20 (with its 4.7 kΩ pull-up resistor) and a DHT22 side by side on the same Arduino or ESP32 for 30 minutes. Plot both temperatures, then move one sensor close to the board's regulator and watch the difference.
3 quick questions
Which sensor works best for many probes on one pin?
Why can't you run an LM35 from 3.3 V?
A sensor on the ESP32 board reads 4 °C high. What is the most likely cause?
