Skip to content
GEEKnodes

Electronics

DHT22 vs DS18B20: Which Temperature Sensor Should You Choose?

DHT22 vs DS18B20 for Arduino and ESP32: range, accuracy, humidity, wiring with a pull-up, multiple sensors on one wire and example code for each sensor.

The DHT22 and the DS18B20 are the two most popular temperature sensors for Arduino and ESP32 projects. They’re cheap, they connect with a single data pin and there are libraries for everything. But they’re not made for the same job: one also measures air humidity, and the other can go underwater or be chained by the dozen.

Here we compare them and show you how to wire and read each one.

In short

  • DHT22: temperature and humidity, from −40 to 80 °C. Ideal for rooms, greenhouses or weather stations.
  • DS18B20: temperature only, from −55 to 125 °C, with a waterproof version. Ideal for liquids, outdoors or fridges.
  • Both have an accuracy of about ±0.5 °C in their useful range.
  • The DS18B20 lets you put several sensors on the same pin thanks to the 1-Wire bus.
  • Both need a pull-up resistor on the data line: 10 kΩ for the DHT22 and 4.7 kΩ for the DS18B20.

Comparison table

FeatureDHT22 (AM2302)DS18B20
What it measuresTemperature and humidityTemperature only
Temperature range−40 to 80 °C−55 to 125 °C
Temperature accuracy±0.5 °C±0.5 °C (between −10 and 85 °C)
Humidity0–100 %, ±2–5 %No
Supply voltage3.3–6 V3.0–5.5 V
ProtocolProprietary single-wire1-Wire
ReadingsAt most one every 2 sAbout 750 ms per conversion at 12 bits
Several on one pinNoYes, each with its own unique address
Form factorsBare part or 3-pin moduleTO-92 package or waterproof probe

Its little brother, the DHT11, is cheaper but much worse: 0 to 50 °C with ±2 °C. We use it in the I2C LCD with DHT11 project, but for serious measurements the DHT22 is worth it.

Comparison card of the DHT22, which measures temperature and humidity from −40 to 80 °C, and the DS18B20, temperature only from −55 to 125 °C with several on one wire
Same accuracy, different uses: humidity vs. ruggedness and a shared bus.

Which one to choose for your project?

  • Room temperature and humidity: DHT22.
  • Water in an aquarium, a pool or a tank: DS18B20 waterproof probe.
  • Several zones with a single microcontroller (for example, heating supply and return): DS18B20, all on the same wire.
  • Outdoors with a long cable: DS18B20; 1-Wire handles distance better.
  • Fridge or freezer: DS18B20; the DHT22 falls short below −40 °C and humidity condenses on it.

If you also need barometric pressure or more accurate humidity, look at I2C sensors like the BME280. We cover it with a Raspberry Pi in the BME280 with Python guide, and here are the differences between I2C, SPI and UART.

How to wire the DHT22

The bare DHT22 has four pins. Looking at it from the front, with the grille facing you:

  1. VCC: to 3V3 on the ESP32 or to 5V on an Arduino Uno.
  2. DATA: to a digital pin (for example, GPIO4).
  3. NC: not connected.
  4. GND: to ground.

A 10 kΩ resistor goes between DATA and VCC. If you buy the 3-pin module, it usually has one soldered on already. If you’re wondering why it’s needed, we explain it in pull-up and pull-down resistors.

With an ESP32, power it from 3.3 V: that way the data line never exceeds the voltage its pins can handle.

DHT22 wired to an ESP32: VCC to 3V3, DATA to GPIO4 with a 10 kΩ resistor to VCC, pin 3 unconnected and GND to GND
The four-pin DHT22 needs a 10 kΩ pull-up on DATA.

Reading the DHT22 with the Arduino IDE

From the Library Manager, install Adafruit’s DHT sensor library (it will also ask for Adafruit Unified Sensor).

#include <DHT.h>

DHT dht(4, DHT22);  // data pin and model

void setup() {
  Serial.begin(115200);
  dht.begin();
}

void loop() {
  delay(2000);  // the DHT22 can't take more than one reading every 2 s
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  if (isnan(t) || isnan(h)) {
    Serial.println("Error reading the DHT22");
    return;
  }
  Serial.printf("Temperature: %.1f °C  Humidity: %.1f %%\n", t, h);
}

Serial.printf works on the ESP32; on an Arduino Uno use several Serial.print calls.

How to wire the DS18B20

The waterproof probe usually comes with three wires:

WireFunctionConnection
RedVCC3V3 (ESP32) or 5V (Arduino)
BlackGNDGND
Yellow (sometimes white or blue)DATAA digital pin, for example GPIO4

A 4.7 kΩ resistor goes between DATA and VCC, just one for the whole bus. To add more probes, connect their three wires in parallel with those of the first one. Each DS18B20 has a 64-bit serial number burned in at the factory, which is how the library tells them apart.

With cables several meters long or many probes, if readings fail, try lowering the pull-up to about 2.2 kΩ.

1-Wire bus with three DS18B20 probes in parallel on the 3V3, DATA and GND lines, with a 4.7 kΩ resistor between DATA and 3V3, connected to GPIO4
Several DS18B20s share three wires and a single 4.7 kΩ resistor.

Reading the DS18B20 with the Arduino IDE

You need two libraries: OneWire and DallasTemperature.

#include <OneWire.h>
#include <DallasTemperature.h>

OneWire bus(4);
DallasTemperature sondas(&bus);

void setup() {
  Serial.begin(115200);
  sondas.begin();
  Serial.print("Probes found: ");
  Serial.println(sondas.getDeviceCount());
}

void loop() {
  sondas.requestTemperatures();
  for (int i = 0; i < sondas.getDeviceCount(); i++) {
    float t = sondas.getTempCByIndex(i);
    Serial.print("Probe ");
    Serial.print(i);
    Serial.print(": ");
    Serial.println(t);
  }
  delay(5000);
}

Two values give problems away:

  • −127: the sensor isn’t responding. Check the wiring and the pull-up.
  • 85: this is the sensor’s power-on value. It usually means the reading was requested before the conversion finished, or the power supply is insufficient.

The index order can change if you add probes. To know which is which reliably, read each one’s address and use it in the code.

Common mistakes

  • High readings: the sensor sits next to the ESP32 or the regulator, which heat up. Move it away on wires.
  • Fake probes: there are lots of counterfeit DS18B20s. They usually work, but with worse accuracy or failures in parasite mode.
  • DHT22 returning NaN: readings too close together, missing pull-up or wrong pin.
  • Ignoring the datasheet: if you doubt a value, learn to read a datasheet; the range and accuracy conditions are always in there.

Frequently asked questions

Does the DS18B20 measure humidity? No, only temperature. If you need humidity, use a DHT22 or an I2C sensor like the BME280 or one from the SHT family.

Can I use these sensors with ESPHome? Yes, both have their own component. In the ESPHome with Home Assistant guide we set up a DHT22 without writing any code.

Can the DS18B20 be powered with only two wires? Yes, in what’s called parasite mode, tying VCC to GND and drawing power from the data line. It’s less reliable; with three wires you avoid trouble.

How many DS18B20s fit on one bus? In theory, a huge number. In practice, with DIY wiring, about ten work without problems if the pull-up and cables are suitable.

How do I send the readings to my home automation system? With MQTT from the ESP32 or with ESPHome. For the hardware, also check out ESP32 vs Arduino.

Accessibility preferences

Quick profiles

Text and reading

Text size
Default
Line height
Default
Letter spacing
Default
Word spacing
Default

Colour

Motion and orientation

Cursor