Measuring temperature and humidity is the most common first project with an ESP32, an Arduino or a Raspberry Pi, and the choice of sensor decides whether the readings are useful. Four families dominate hobby shops: the DHT22 (also sold as AM2302), the Bosch BME280, the Aosong AHT20 and the Sensirion SHT4x. They differ in accuracy, interface and how much they are affected by their own heat. This guide compares them using the manufacturers' datasheets and gives practical advice on placement, which matters as much as the chip.
The short answer
- For accurate room or outdoor humidity: an SHT4x (SHT40, SHT41 or SHT45).
- For a cheap, decent I²C sensor: an AHT20.
- If you also want air pressure: a BME280, mounted away from heat sources.
- For new designs, avoid the DHT22, unless you already have one in a drawer.
- For temperature only, in water or on pipes: a waterproof DS18B20 probe, which we covered in a separate guide.
Comparison from the datasheets
| DHT22 / AM2302 | BME280 | AHT20 | SHT40 | SHT45 | |
|---|---|---|---|---|---|
| Interface | single-wire, custom protocol | I²C or SPI | I²C (address 0x38) | I²C (default 0x44) | I²C |
| Temperature accuracy | ±0.5 °C | ±0.5 °C over 0-65 °C | ±0.3 °C | typ. ±0.2 °C | typ. ±0.1 °C |
| Humidity accuracy | ±2 %RH (max ±5 %RH) | ±3 %RH | ±2 %RH at 25 °C | typ. ±1.8 %RH | typ. ±1.0 %RH |
| Extra | - | air pressure | - | built-in heater | built-in heater |
| Minimum interval | 2 s | - | 2 s recommended | - | - |
Numbers are as printed in each datasheet. Manufacturers define "typical" and "maximum" accuracy in different ways, so treat small differences with care. The large differences are real.
DHT22 / AM2302
The DHT22 was the default hobby sensor for years. The AM2302 datasheet lists ±0.5 °C and ±2 %RH accuracy (maximum ±5 %RH), 0.1 resolution and a collecting period of 2 seconds. It runs on 3.3-5.5 V.
Its weaknesses:
- A timing-based single-wire protocol. The microcontroller has to measure pulse lengths precisely. On busy or fast boards this leads to occasional failed readings. ESPHome's documentation, for example, lets you set the DHT model manually when readings fail and lists wrong pull-up resistor values as a common cause of problems.
- Variable quality. Many modules sold as DHT22 are clones of unknown origin.
- Slow and large compared with modern I²C chips.
If you have one, it works. If you are buying, an AHT20 costs about the same and is easier to use.
BME280
Bosch's BME280 measures temperature, humidity and pressure. The datasheet specifies ±3 %RH humidity accuracy, ±1.0 hPa pressure accuracy, and temperature accuracy of ±0.5 °C between 0 and 65 °C.
There is one important footnote. Bosch states that the temperature value "depends on the PCB temperature, sensor element self-heating and ambient temperature and is typically above ambient temperature". The temperature sensor exists mainly to compensate the pressure and humidity readings. On a small breakout board soldered next to an ESP32, a BME280 often reads warm, and because relative humidity depends on temperature, the humidity reading suffers too.
Use a BME280 when you want pressure (weather trends, altitude), and:
- take readings at long intervals with the sensor in forced (one-shot) mode rather than measuring continuously,
- mount it on a short cable away from the microcontroller and voltage regulator.
Also check what you bought. Many cheap boards labelled "BME280" carry a BMP280, which measures only pressure and temperature, without humidity. A genuine BME280 reports chip ID 0x60 in its ID register, so a library that reads the ID will tell you.
AHT20
The Aosong AHT20 is a small calibrated I²C sensor with a fixed address of 0x38. The Seeed Grove AHT20 module datasheet lists ±0.3 °C and ±2 %RH at 25 °C.
The AHT20 datasheet itself contains useful advice on self-heating: if you measure too often, the sensor warms up and accuracy suffers. To keep the temperature rise below 0.1 °C, the active time should not exceed 10% of the measurement time, and Aosong recommends measuring every 2 seconds at most. In a home automation sensor that reports every 30-60 seconds, this is not a problem.
In ESPHome the AHT20 uses the aht10 component with variant: AHT20. The ESPHome documentation also notes that the DHT20 is an AHT20 in a DHT-style housing and uses I²C.
SHT4x (SHT40, SHT41, SHT45)
Sensirion's SHT4x family is the most accurate in this list and still affordable. According to the current datasheet:
- SHT40 and SHT41: typically ±1.8 %RH and ±0.2 °C,
- SHT45: typically ±1.0 %RH and ±0.1 °C,
- long-term drift below 0.2 %RH per year and below 0.03 °C per year (typical),
- supply voltage 1.08-3.6 V and very low current (0.4 µA average),
- fully functional in condensing environments.
The SHT4x also has a built-in heater. According to Sensirion, it is meant to remove condensed water from the sensor surface and to keep readings accurate during long periods of very high humidity. It is not meant to run constantly: the datasheet limits it to a 10% duty cycle and notes that the specifications are not valid while it is heating. ESPHome's sht4x component can switch it on at a limited duty cycle (at most 5%), and it is off by default. For a bathroom, a basement or an outdoor sensor, this is a real advantage.
Note the supply range: the SHT4x is a 3.3 V part. Most breakout boards include a regulator, but check before connecting one to 5 V.
Placement matters as much as the chip
A ±0.1 °C sensor in a bad spot gives a worse result than a ±0.5 °C sensor in a good one. A few rules:
- Keep it away from heat. An ESP32 with Wi-Fi active, a linear voltage regulator or a USB charger warms anything nearby. Put the sensor at the end of a cable or at least on the far side of the board, with a slot cut in the PCB or case between them.
- Let air reach it. A sealed case measures the case, not the room. Use ventilation openings near the sensor, not near the electronics.
- Avoid direct sun, radiators and outside walls when you want the room temperature.
- Outdoors, use a radiation shield (a stack of plates that lets air through but blocks the sun). Without one, daytime readings in sunlight are meaningless.
- Avoid chemical vapours. The AM2302 datasheet warns that chemical vapours can affect the sensing element; the same applies to other polymer humidity sensors.
- Compare before trusting. Put two sensors next to each other for a few hours. If they disagree by more than their stated accuracy, look at placement first, then apply a calibration offset in software.