
Learn with this tutorial how to connect a DHT11 temperature & humidity sensor to a Raspberry Pi Pico. With a MicroPython script, we will read every two seconds the ambient temperature and humidity.
- Prepare the hardware
– First you need a computer to run Thonny. In this tutorial we’ll use a Raspberry Pi 4 as our computer. And Thonny is a user-friendly Python IDE to interact with the Raspberry Pi Pico board. If you never used Thonny to program the Raspberry Pi Pico, before continuing, you better have a look at our tutorial “How to start programming the Raspberry Pi Pico“.
– Next you need an USB cable with micro-USB plug.
– You also need a Raspberry Pi Pico of course. For this tutorial you need pin headers soldered to the GPIO-pins of your board.
And finally you’ll need some extra components :
– a breadboard (we are using a 400 points breadboard)
– an DHT11 or DHT22 temperature and humidity sensor (we are using the DHT11 sensor)
– Dupont jumper wires
Visit our shop if you miss any components. - Get to know the DHT11 temperature and humidity sensor
The DHT11 is a low-cost and popular sensor for measuring temperature and humidity. The device on the module requires 3 connections to the Raspberry Pi Pico : 3,3V ; GND and a GPIO input pin. As the output signal is 3,3V , it can be directly connected to a GPIO input pin of the Raspberry Pi Pico.
The temperature range is 0-50°C (+/-2°C) and the humidity range is 20-90% (+/-5%). The sensor is quite slow and has limited accuracy, but it is an ideal device for your experiments.
Be careful ! Before starting to connect components to the GPIO pins of your Raspberry Pi Pico, make sure it is not connected to your computer. - Setup the hardware part
– place the DHT11 sensor onto the breadbord
– connect the DATA pin of the sensor to GP16 (yellow wire)
– connect the VCC pin of the sensor to 3V3(OUT) (red wire)
– connect the GND of the sensor to a GND of the Pico (black wire) - Write the code
The aim is to write a basic MicroPython script that allows us to visualize the measured ambient temperature and humidity.
Install the Library first
To avoid extensive and complicated code writing, libraries are often used. For our DHT11 temperature and humidity sensor, we will also be using a library. We found the most appropriate library from ikornaselur. As the file from this quite specific library doesn’t come automatically with MicroPython, we have to install it ourselves.
So, before writing the code, we’ll have to upload the files to our Raspberry Pi Pico. You can download a ZIP-folder containing the file to be installed here.
Once downloaded and unzipped on our computer, we upload the ‘dht.py’ file to our Raspberry Pi Pico. If you don’t know how to do that, have a look at our tutorial ‘Transfer files between computer and Raspberry Pi Pico‘. If you have multiple folders on your Raspberry Pi Pico, make sure you upload the file in the same folder as the new file we will create for our main code. And don’t change the filename of the library of course.
OK, now the library is uploaded to our Raspberry Pi Pico, write or paste following code in the IDE:
from machine import Pin
import time
from dht import DHT11, InvalidChecksum
sensor = DHT11(Pin(16, Pin.OUT, Pin.PULL_DOWN))
while True:
temp = sensor.temperature
humidity = sensor.humidity
print("Temperature: {}°C Humidity: {:.0f}% ".format(temp, humidity))
time.sleep(2)
Be careful, MicroPython is whitespace-sensitive. Don’t remove the “tabs”.
Some explanations about the code :from machine import Pin
: to partially import the machine module to have access to the GPIO pins.import time
: to import the time module. This will allow us to use time-related tasks.from dht import DHT11, InvalidChecksum
: to import the file from the DHT library.sensor = DHT11(Pin(16, Pin.OUT, Pin.PULL_DOWN))
: to create a DHT11 object. The data wire is connected to GP16 in our case.while True:
is an infinitive loop (until we stop the program).
In the 3 next lines we read the measurements and print them on the screen.time.sleep(2)
: finally, before repeating the loop, we wait for 2 seconds - Run your script
Now, it’s time to save your script. Just make sure you save this MicroPython file in the same folder as the file from the library you uploaded earlier.
Then click on the Run button. You will see the current temperature and humidity appearing every 2 seconds on the screen.
Congratulations! You now know how to read temperature and humidity with the DHT11 sensor connected to your Raspberry Pi Pico. You can further use the script to combine it with our tutorial ‘How to connect an LCD display to a Raspberry Pi Pico‘. The measurements will then be displayed on the LCD. Have fun with it!