Temperature sensor comparison – DHT22 vs DS18B20 | Arduino tutorial

Which sensor you choose for your Arduino / IoT project? DHT22 Temperature and humidity vs. DS18B20 Temperature 1-Wire - complete review and tutorial

Monitoring temperature or humidity via IoT (internet of things) is one of the most common home DIY  projects implemented because of the large interest and benefits. Either you are monitoring temperature in your child room or you have an indoor self controlled heating system, this implies using one of the many temperature sensor available on the market compatible with Arduino or other development environments. Knowing the temperature with high accuracy can be crucial in many situations and for that you need to be well informed about sensors technical specifications. A temperature sensor can be find in the low price range between 0.5-10$ on most of the Asiatic known commerce platforms as well as in your local electronics shops.

DHT22 & DS18B20 Datasheet review and Arduino Tutorial

In this article I will do a quick review on both DHT22 Temperature and Humidity and DS18B20 Temperature sensor 1-Wire, and I will compare them to give you an idea on which one fits better in your projects. I also invite you to see my quick video review and hands-on to make a better idea about how the sensors look and feel.

First of all, I own 3 of this sensors and I already implemented them in my projects, so I will give you real feedback about how they behave after being in production.

Probably the best books to learn Arduino

Programming Arduino: Getting Started with Sketches, Second Edition

Arduino Workshop: A Hands-On Introduction with 65 Projects

Exploring Arduino: Tools and Techniques for Engineering Wizardry

DHT22 Overview

DHT22 is simple basic, cheap digital temperature and humidity sensor. Its officially sold by Adafruit but is manufactured by many producers, especially by China LTDs (Aosong Electronics Co). It is part of DHTxx series and is considered quite slow, but one of the best or hobbyists who want to embed it in data logging projects. I found it selling from 2.5$ to 10$ in different markets, but being encapsulated in a plastic shape is hard to make any differences in construction quality.

You can buy them in many versions, one is named AM2302 which stands for wired DHT22, the difference being in the fact that it comes with ready to use wires, other versions comes with the sensor soldered on the PCB having resistors and capacitors embedded and regular pins for connections.

You can see bellow what advantages or downsides come with different technologies:

Sensor advantages - GeeksTips.com

The working principle is simple. Having a capacitive humidity sensor and a thermistor it measures the surrounding air, and sends out a digital signal on the data pin. It doesn’t have any analog output. The only downside of this module is that it can only read data once every 2 seconds, but nothing other than that. Sellers claim that the sensors comes calibrated in accurate calibration chambers but no certificate comes in the box :).

DHT22 Technical Specifications:

Model DHT22
Power supply 3.3-6V DC
Output signal digital signal via single-bus
Sensing element Polymer capacitor
Operating range humidity 0-100%RH ; temperature -40~ 8 0Celsius
Accuracy humidity +-2%RH(Max +-5%RH) ; temperature <+-0.5Celsius
Resolution or sensitivity humidity 0.1%RH ; temperature 0.1Celsius
Repeatability humidity +-1%RH ; temperature +-0.2Celsius
Humidity hysteresis +-0.3%RH
Long-term Stability +-0.5%RH/year
Sensing period Average: 2 s
Interchangeability fully interchangeable
Dimensions small size 1 4 *1 8 *5.5mm; big size 22*28*5mm

Electrical details and power consumption

Power supply DC 3.3 5 6 V
Current supply Measuring 1 1.5 mA
Current supply Stand-by 40 NULL 50 uA
Collecting period Second 2 second

DHT22 pin description / pinout

Dht22 pin description

  • VCC – (between +3v and +6v)
  • Data Pin – digital output signal, the only output available, provides data for both temperature and humidty
  • NC / NA – not connected or not available
  • GND – ground pin plugged in circuit
I really have no idea why the third pin exists, maybe just for symmetry, or maybe i did not discovered any other reason still. In this version I can say that those pins are quite fragile and you can easily bend them or even loose. This pins cannot be used to connect with DUPONT wires because are much thinner than the usual pcb pins. Also I don not recommend you to use it on a standard breadboard when in testing or production because of the pin thickness that may not be in perfect contact with the breadbord slots. Almost all versions come with a screw hole so you can easily embed it in a project case or on a PCB. Check the standard size bellow:
DHT22 size
Attention!  Be very carefull when soldering / welding this sensor on a PCB. A temperature higher than 260C can easily destroy it. Also try to finish quickly the soldering process.

Reading DHT22 with micro-controllers

Connecting DHT22 to any microcontrollers or development boards is very easy, you just need three wires. After plugging the power wires you need to connect the data pin from DHT to any digital input GPIO from your microcontroller.  A 4.7K – 10KΩ resistor should be pulled between DATA pin and VCC in order to have valid logic signal and don’t melt anything when changing GPIO from output to input. Also I recommend to use a low size capacitor near the sensor pins to cut the power peeks. You can easily connect it with Arduino UNO like in the following picture:

DHT22 connection with Arduino

After proper connections are made, next you should download and activate the DHT sensor library provided by Adafruit in case you are using an Arduino to read the sensor. Just open your Arduino IDE, go to Sketch – > Include Library – > Library manager and search for dht keyword. You should find something like in below image:

Arduino DHT sensor library

The library comes with sketch examples ready to upload into your Arduino board which you can find in the Examples menu. Here is a basic example to read your DHT sensor:

The “DHT.h” library gives you easy to use methods to read the temperature and humidity and also for the index formulas. You can see in the example a 2 seconds delay line inserted just to avoid to display the same readings because of the sensor lag. Readings look like this:

DHT22 Arduino Serial reading

A very project to start with is displaying DHT22 readings on a 2×16 LCD like in the following example:

Arduino DHT LCD display

See the Arduino code example below:

Complete DHT22 Datasheet can be found here provided by sparkfun.

DS18B20 Temperature sensor – 1 Wire technology

So, you’ll probably say that this comparison is not fair because the DS18B20 cannot sense humidity. Well that’s true, but if your project does not require measuring humidity then both can be a good candidate in your projects, being in the same range of price.

DS18B20 Arduino compatible temperature sensor

DS18B20 temperature sensor overview

DS18B20 is 1-Wire interface Temperature sensor manufactured by Dallas Semiconductor Corp, very common sensor in PCB embedded electrical circuits that needs temperature sensing. It’s unique 1-Wire interface requires only one port pin for communication and needs no other external components to work. It can be powered from DATA line with a power supply range between 3.3v and 5.5v and in standby mode it does not need any power at all, or so they claim … Thermometer resolution is programmable from 9 to 12 bits and it converts 12-bit temperature to digital word in less than 750 ms.

The biggest advantage at this device is that, because each one contains a unique silicon serial number, multiple DS18B20 sensors can coexist on the same 1-Wire bus, which can be a huge advantage in applications like HVAC environmental controls, sensing temperatures inside buildings, equipment or machinery, and process monitoring and control. Here you can see the DS18B20 block diagram:

DS18B20 Block Diagram

A very good example of application is what a friend of mine realized. He implemented a floor heating system in his large living and to be able to send heating energy to each zone of the floor, he used 10 DS18B20 sensors in a 1-Wire network. Using this technology he was able to read 10 Temperature sensors using a single GPIO from his Arduino Nano.

From what i could find, this sensor can be found in 3-4 construction versions, the 3 pin one that looks exactly like an ordinary transistor, an 8 pin SOIC version which is called DS18B20z, his little brother DS18B20u which stands for micro because is smaller, and in a waterproof probe style which can be more useful in industrial application, underwater or under the ground.

Also many producers sell it mounted nicely in a little PCB whit additional resistors and capacitors and common breadboard pins. The DS18B20 temperature sensor has four main data components:

  • 64-bit lasered ROM
  • Temperature sensor
  • Nonvolatile temperature alarm triggers TH and TL
  • Configuration register

In the block diagram above you can see the PARASITE POWER circuitry which “stealspower whenever the DQ or Vdd pins are high. DQ will provide enough power when the specified timing and voltage are met. The advantages of parasite power are that by parasiting off this pin, no local power source is required for remote sensing and the ROM may be read in absence of normal power. You can see below two circuit sketch examples that can teach you how to supply enough current to work in parameters.

DS18B20 Pullup for current suply

 DS18B20 DC electrical characteristics and power consumption

DS18B20 DC electrical characteristics
Here is a good example of how to connect many DS18B20 on one wire using 1-Wire NORMAL MODE.

Many DS18B20 1-Wire connected

As you can see, this sketch does not use PARASITE MODE, each sensor has individual power supply connection to the VCC wire. You can see how a central data wire connects all sensors creating a monitoring network.

The awesomeness comes in the next image where you can see multiple DS18B20 in a network connecting in PARASITE MODE.

DS18B20 1-Wire parasite mode

Now this is cool! You just have made an entire sensors network using just two wires !! You still have a tones of GPIOs free on your MCU ready to do other jobs.

Reading DS18B20 temperature sensor with micro-controllers

Talking with the DS18B20 can seem a bit complicated than with DHTxx ones, but I assure you that is not. Connecting the sensor with the Arduino its very easy. Just connect the power pins in NORMAL or PARASITE mode and plug the DATA PIN to any arduino digital GPIO with a 4.7K resistor pulled HIGH. You can see below an example.

DS18B20 Arduino UNO connected

You can also connect it with ESP8266 (See tutorial here) if you want to send data over the internet. See an example in next image.

DS18B20 conected at ESP8266

After the connections are made, open your Arduino IDE, go to library manager and search for DS18B20 temperature sensor. Install the library and then open from the examples menu the sketch name OneWireSearch.

DS18B20 Arduino library

This is like a prerequisite before loading your main program. First you need to find your DS18B20 temperature sensor address in the 1-Wire network by scanning the DATA channel. You can also use search() functions provided by Dallas Arduino libraries in the code to scan automatically next device, but i found them being tricky and didn’t work each time so i don’t recommend it. Load this program into Arduino and run it.

You should see a result like this:

DS18B20 1 Wire device address search

Copy your sensor address printed in console and than go to DallasTemperature example menu and load the sketch called Single or copy the code below:

This code itself explains trough comments how to make it work for you. Just paste the address if you choose to use manually assigning method. Load the code and you should see a result like this:

DS18B20 Arduino reading serial

Another cool thing about DS18B20 is that it gives you the ability to set alarm points. Things can become handy when you need to create temperature protection thresholds or you need to control things. Here is an example of how to set alarms with DS18B20 temperature sensor:

After uploading this into your Arduino you should see following results:

DS18B20 Arduino alarm serial

Complete DS18B20 temperature sensor datasheet can be found here provided by sparkfun.

Quick comparison between DHT22 and DS18B20 features

Now, we connected both sensors, we saw that both are working pretty well, but which one is better? Well let’s see a list of features and maybe this will make things clear:

Price 2-6$ 1-3$
Range Hum 0-100%RH; Temp -40C to + 80C Temp -55C to +125C
Accuracy Hum +-2%RH(Max +-5%RH); Temp <+-0.5C +- 0.5C from -10C to +85C
Resolution / Sensitivity Hum 0.1%RH; Temp 0.1C resolution is programmable from 9 to 12 bits
Builtin Alarm No Yes
Technology 1 channel Temp and Hum 1-Wire Network
Size 14*18*5.5mm much smaller !
Waterproof NO YES (probe version)
Sensing period ~2s <1s
Connection 3 wires 3 / 2 wires
DC Voltage 3.3 - 6V 3.0 - 5.5V
Standby consumption 50 uA 750 nA
Measuring consumption 1.5 mA 1.5 mA
Protection case / enclosure YES NO
Mounting spot YES, screw NO
Interchangeability YES, fully NO, unique address
If you are still confused, i can tell you that, in my opinion both sensors are pretty good for various projects. It really depends on what kind of project you are building and what are your needs. If you can answer the following questions maybe you’ll get an idea about which one fits for you:
  1. Do you need to measure Humidity ??
  2. Is your project outdoor or indoor?
  3. Do you need one sensor or many sensors?
  4. What is your temperature range?
  5. Should the temperature sensor stay in high moisture or water?
  6. What is your measurement interval?
  7. Is your project powered by batteries?
  8. What is your budget?

Final thoughts

I choose to use DS18B20 temperature sensor for projects that require only Temperature measurements and DHT22 for projects that needs to measure Humidity too. I think that if you only need temperature, DS18B20 is a better choice because of the price and also because its viability an 1-Wire technology. DHT series are a good choice for ambient weather monitoring and IoT data logging, and also looks better if you choose to display them in a public place. Check my ESP8266 Tutorial if you want to find out how to send sensors data over the internet.

Hoping that this article inspired you, i kindly invite you share this articlesubscribe my YouTube channel and join the communities on social networks. Please feel free to comment or send suggestions / remarks so i can improve the content quality!





7 comments » Write a comment

  1. Awesome comparison. I’m planning on using to use both in my setup. The DS18B20 to replace the some thermostats, which will not have power but have two wires, and the DHT22 powered in a few places.

    I’ve read that star configurations of 1-wire sensors are the hardest to get working. Would you have any guess if I could expect 5 of these sitting on the end of 5 different thermostat wires to work if I joined them all together?

    If it’s unlikely to work I might pick up a DS2408 and give them each their own channel.

  2. Pingback: Arduino temperature sensor tutorial – DHT22 vs. DS18B20 1-Wire – aaronsworld.com

  3. Pingback: How to send push notifications to Android or IOS with ESP8266 - Arduino tutorial

  4. Pingback: Smart House - Intelligent Home with Internet of Things - Introduction

Leave a Reply

Required fields are marked *.