Setting Up A DHT11 Sensor On Your RASPBERRY PI — The Easy Way

Pierre Averous
3 min readApr 21, 2021

This is the first of a a two-article series, in which I will show you how I set my DHT11 sensor up at home, to collect Temperature and Humidity measurements using Python. In the second one, I will detail how I exposed those measurements as Prometheus metrics and displayed them in this beautiful Grafana dashboard !

Beautiful Grafana dashboard showing Temperature & Humidity of my living room.

The Hardware

Let’s look at the hardware required to make this project.

A Raspberry Pi, which will serve as a controller. We will be running the Python code on it, that will measure Temperature and Humidity through the sensor. I used a Raspberry pi 3B+ here, simply because I had one lying around. Any Raspberry will do fine to follow this tutorial I guess, you will just have to adapt the pinout.

The DHT11 sensor. You can get one for pretty cheap on Amazon, or even cheaper on Aliexpress. I actually had one lying around from an Arduino starter kit I bought a few years back.

The Wiring

Now that we got all our components ready, it’s time to wire things up !

The DHT11 sensor I used has three pins, because it is already soldered with a resistor to a circuit board. Most DHT11 sensors are shipped like this. If yours has 4 pins, your setup will probably very close to this one, but you might have to throw in an additional resistor. Check out your sensor’s manual to get things right !

Back to my example : from left to right, the pins are for data transmission (blue), 5V (red) and ground (black).

I hooked those up like so :

  • Red to pin 2 on the Raspberry Pi, which is a 5V Output
  • Black to pin 6, Ground
  • Blue to pin 7, GPIO 4

Let’s move on to the fun part : the code !

The Code

We will here be writing a bunch of Python code here. Nothing too complicated, so let’s get started.

Preparing the coding environment

First of all, we will be using pipenv to create a virtual environment. To install it, simply run sudo apt install pipenv.

Then, create a folder, say dht11_control and create file named Pipfile in it with the following contents :

The run pipenv install in this folder. This will create a python 3.7 virtual environment, which we will use to run our python script.

Next, we will install the Adafruit Python DHT Sensor library from https://github.com/adafruit/Adafruit_Python_DHT. To do this, you need to :

  • Pull the code to the repo : git clone https://github.com/adafruit/Adafruit_Python_DHT.git
  • Activate the Python environment : pipenv shell
  • Enter the library folder : cd Adafruit_Python_DHT
  • Install the library into the environment : sudo python setup.py install

Now there we go, we have a full environment, all that’s left is to write a couple lines of code.

Writing the script

The script in itself is fairly simple. A couple imports, making a measurement, and printing the output. Here are the contents of main.py.

And there you have it ! All that’s left to do is to run it with pipenv run measure :

$ pipenv run measure
Measured Temp=19.0°C | Hum=46.0%
Measurement took 2.0763556957244873s

Magical, it was quite easy in the end, right ? Some very basic code to measure temperature and humidity in your home, but it can be used as a base to fuel your projects !

--

--

Pierre Averous

DevOps enthusiast, curious about ever evolving tech. Currently working at https://padok.fr/.