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 https://github.com/adafruit/Adafruit_Python_DHT.git

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 setup.py install

sudo python3 setup.py install

pi@raspberrypi:~/Adafruit_Python_DHT $ sudo python setup.py install

pi@raspberrypi:~/Adafruit_Python_DHT $ sudo python3 setup.py 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

/usr/local/lib/python3.7/dist-packages/Adafruit_DHT/

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
Beaglebone_Black.py __pycache__
common.py Raspberry_Pi_2_Driver.cpython-37m-arm-linux-gnueabihf.so
common.py.bup Raspberry_Pi_2.py
__init__.py Raspberry_Pi.py
platform_detect.py Test.py

You will see a file named platform_detect.py, 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 platform_detect.py

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

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

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

elif match.group(1) == 'BCM2711':
#Pi 4b
return 3

Alternate way to edit this file

You can edit platform_detect.py 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.

nano platform_detect.py

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

Connecting DHT11 Sensor with Raspberry Pi 3 / 4 using Python

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

Coding

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))

Construction

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 “dht11.py“.

sudo python dht11.py

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.

Conclusion

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.

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

    Reply

Leave a Comment