Reading temperature & humidity from DHT sensors with Raspberry

DHT sensors

Have you ever wanted to build your own weather station? Then read further, because today we are going to look at how to measure temperature and humidity with our Raspberry! We are going to use DHT11/22 sensor, which you can get on Ebay for 1 to 4 dollars, depending on the type and accesorry that comes with it. These sensors are smaller that s match box and quite accurate. There are two basic models of the sensor - DHT11 and DHT22, you can find their chacateristics below. Generally DHT11 is cheaper and less accurate than DHT22, but the price of DHT22 is still so low that there is no reason why you should hesitate and not buy the 22 version.

The sensor can come as a bare “mini plastic box” with 4 pinouts or it can be mounted on a board and have 3 pinouts. The difference is that the version mounted on the board already has a pull-down resistor connected to it and you don’t have to worry about a NULL pin, which doesn’t have any purpose for us, on the “bare” version. The three remaining pinouts are for power inout, ground and data output, how simple! Bare in mind that if you have the version without the board, you will also need a 4.7kΩ-10kΩ resistor to make it work properly!

This sensors is able to measure temperature and humidity. If you would cut the plastic box guarding the inner parts of the sensor, you would find two separate sensors there. A thermistor for temperature readings, which is simply a variable resistor that changes its resistance according to the temperature. And second sensor for humidity readings, that consists of two electrodes and a moisture holding substrate between them. This substrate sucks humidity from its surroundings - changing its conductivity, which then changes the resistance between the two electrodes.


attribute value
cost starting on $1
power 3-5V
humidity reads values from 20% to 80% with 5% accuarcy
temperature reads values from 0°C to 50°C ±2°C accuracy
reading interval 1Hz - can read values every second

DHT11 DHT11 on board


attribute value
cost starting on $3
power 3-5V
humidity reads values from 0% to 100% with 2-5% accuarcy
temperature reads values from -40°C to 80°C ±0.5°C accuracy
reading interval 0.5Hz - can read values every 2 seconds

DHT22 DHT22 on board

Connecting it to Raspberry

Connecting the sensor to Raspberry is pretty simple. You can choose weather to use 3.3V or 5V output from your Raspberry to connect it to VCC pin, then connect Raspberry’s GROUND to GND and choose one GPIO to connect in to DATA pin. If you have the version not mounted on the board, connect 4.7kΩ-10kΩ resistor between VCC and DATA.

Circuit Circuit schematics

Reading the temperature

Now we have everything connected and ready to read the actual temperature and humidity from our sensor. To read the actual values, we will use a python library from Adafruit, because these amazing guys already did the hard work and figured out how to translate the changing resistance of temperature/humidity sensors to human readable values. I have forked the library and you can freely download it from my Gitlab. Remember that you need python and git to be installed on your Raspberry to make this work! Lets go step by step and measure something finally!

Open a terminal on your Raspberry and install python and git if you don’t have it yet

sudo apt-get update
sudo apt-get install git build-essential python-dev python-openssl

Now download the Adafruit DHT library and open the folder

git clone

Install the library

sudo python install

Great, now go to the examples folder

cd examples

And here we go, there is a python script that takes two arguments. First one is type of your sensor, so 11 for DHT11 and 22 for DHT22. The second one is the GPIO number (BCM number) that the DATA pinout is connected to. So if you have connected the sensor to GPIO 4, that translates to number 7 in BCM, just execute

sudo ./ 22 4

And you should see the measured temperature and humidity, something like this

Temp=22.0*  Humidity=60.0%

Congratz! You are now running a small weather station!