Connecting DHT11 Sensor with Raspberry Pi 4 / 3 using Python

Connecting DHT11 Sensor with Raspberry Pi 4 / 3 using Python

You might have heard of Raspberry Pi which is a single board computer and it is very popular among student and electronics hobbyist. It is compact and advanced device. We will also use a DHT11 Temperature and Humidity sensor. In this tutorial we will learn connecting DHT11 Sensor with Raspberry Pi 4 / 3 using Python.

The sensor is connected using one wire for sending the data signal. These are quite popular and used in different projects like soil monitors, remote weather stations, home automation systems etc. You can also used DHT11 sensor with Raspberry Pi 4 using python code.

Collecting data using DHT11 Temperature and humidity sensor using Raspberry Pi is simple. In this tutorial, we will demonstrate how to connect the DHT11 sensor with Raspberry Pi and get the output i.e. temperature and humidity readings. We will be doing this using python programming. In fact we will be extracting DHT11 sensor data using Python.

Parts List

You can use any raspberry Pi you have.

Raspberry Pi Pin Configuration

Below diagram shows pin definitions of Raspberry Pi. In this tutorial we have connected it with Pin 7 which is GPIO4 of Raspberry Pi. GPIO pin configurations are same for all Raspberry Pi.

Connecting DHT11 Sensor with Raspberry Pi 3 / 4 using Python

Connection Diagram

Here we have connected DHT11 temperature and Humidity sensor with Raspberry Pi. You can also connect DHT11 sensor using WeMos D1 Mini wifi module.

Connecting DHT11 Sensor with Raspberry Pi 3 / 4 using Python

Popular Projects:

Installation of required Libraries

First we have to install Adafruit DHT library for this tutorial. Login to raspberry pi via VNC Viewer or SSH (Using putty) and run the below code.

git clone

It will download the necessary files under a folder named Adafruit_Python_DHT. Once your download in completed run the command “ls” for listing the files and folders. You will see Adafruit_Python_DHT is listed there.

pi@raspberrypi:~ $ ls
Adafruit_Python_DHT Desktop

Now go to Adafruit_Python_DHT folder as shown below

pi@raspberrypi:~ $ cd Adafruit_Python_DHT/

Run the below command for installation of Python version 2 and 3

sudo python install

sudo python3 install

pi@raspberrypi:~/Adafruit_Python_DHT $ sudo python install

pi@raspberrypi:~/Adafruit_Python_DHT $ sudo python3 install

Once the python installation please make sure GPIO is enabled in your raspberry Pi. Run the below command and enable it.

pi@raspberrypi:~ $ sudo raspi-config
pi@raspberrypi:~/Adafruit_Python_DHT $ cd

How to run the same code in Raspberry Pi 4?

For executing the same python code in Raspberry Pi 4 we have to make some changes. The above library supports till Raspberry Pi 3. Now to make it compatible with Raspberry Pi 4 we have to make below changes.

Go to below directory


pi@raspberrypi:~ $ cd /usr/local/lib/python3.7/dist-packages/Adafruit_DHT/

Again list the files and folders with “ls” command as shown below

pi@raspberrypi:/usr/local/lib/python3.7/dist-packages/Adafruit_DHT $ ls __pycache__

You will see a file named, This is the file we have to edit. Since this file has read only permissions hence we can’t make changes and save it. In order to change this file we have to change the permission of this file. you can run below command to change the permission.

sudo chmod a+w

pi@raspberrypi:/usr/local/lib/python3.7/dist-packages/Adafruit_DHT $ sudo chmod a+w

Now go to directory /usr/local/lib/python3.7/dist-packages/Adafruit_DHT and open the file

Once you open it, add the below lines of code at the bottom and save it.

elif == 'BCM2711':
#Pi 4b
return 3

Alternate way to edit this file

You can edit using putty by remote SSH session. First navigate to usr/local/lib/python3.7/dist-packages/Adafruit_DHT directory and then run the below command.


pi@raspberrypi:/usr/local/lib/python3.7/dist-packages/Adafruit_DHT $ nano

Connecting DHT11 Sensor with Raspberry Pi 3 / 4 using Python

You can add the same set of code here and save the file.


You can copy paste the below code in a empty text file and save it with extension .py. You can create a new file in /home/pi directory and name it anything you want. Make sure the extension should be “.py“.

Now right click and open that file, the file will get opened using Thonny python editor. Now you can copy the code below and paste it there and save it.

import Adafruit_DHT
sensor = Adafruit_DHT.DHT11
pin = 4
while True:
     humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
     print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))


As per the circuit diagram we have connected the sensor with Raspberry Pi 3 and same done with Raspberry Pi 4 as well since the GPIO pins are same.

Connecting DHT11 Sensor with Raspberry Pi 3 using Python

Connection with Raspberry Pi 4

Connecting DHT11 Sensor with Raspberry Pi 4 using Python

Executing the Code & Testing

Executing the code is slightly different in Raspberry Pi 3 and Pi 4.

  • In Raspberry Pi 3

You can run the code either logging into Raspberry Pi or via taking SSH using Putty. Once you have the code file ready then you can run the below command. Here we have named the code file as ““.

sudo python

If everything is correct the you will get a output on you SSH terminal as shown below.

  • In Raspberry Pi 4

For Raspberry Pi 4 you have to open the code file and by default it will open using Thonny python editor. Now click the Play button which is green in color. Just below that window you will get the output as shown below.


This is a simple project interfacing DHT11 temperature and humidity sensor with Raspberry Pi 3 and Raspberry Pi 4 using a simple python code. Any beginner can try this and build this as a project to understand the concept of interfacing DHT11 module with Pi’s. Try to build this and comment below if you facing any challenges.

6 thoughts on “Connecting DHT11 Sensor with Raspberry Pi 4 / 3 using Python”

  1. Really appreciate this guide. I couldn’t get my sensor running on the pi 4 but this guide worked a treat. Thank you very much. 5 stars.

  2. Traceback (most recent call last):
    File “/home/pi/”, line 6, in
    print(‘Temp={0:0.1f}*C Humidity={1:0.1f}%’.format(temperature, humidity))
    TypeError: unsupported format string passed to NoneType.__format__


Leave a Comment