Raspberry Pi Pico - DHT22 Temperature Humidity Sensor
This guide shows how to use the Raspberry Pi Pico with the DHT22 temperature and humidity sensor. We will cover:
- Connecting a DHT22 Sensor with a Raspberry Pi Pico
- Linking a DHT22 Module to a Raspberry Pi Pico
- Programming a Raspberry Pi Pico to Measure Temperature and Humidity Using a DHT22 Sensor and Module
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of DHT22 Temperature and Humidity Sensor
DHT22 | |
---|---|
Temperature Range | -40°C to 80°CW |
Temperature Accuracy | ± 0.5°C |
Humidity Range | 0% to 100% |
Humidity Accuracy | ± 2 to 5% |
Reading Rate | 0.5Hz (once every 2 seconds) |
Operating Voltage | 3 to 5V |
Pinout
There are two forms of DHT22: one is just the sensor, and the other is a complete module.
The DHT22 sensor has four pins.
- GND pin: Connect this to GND (0 volts).
- VCC pin: Connect this to VCC (5 volts or 3.3 volts).
- DATA pin: Use this pin for the sensor to talk with the Raspberry Pi Pico.
- NC pin: This is not used and you can ignore it.
The DHT22 module has three pins:
- GND pin: connect to GND (0 volts)
- VCC pin: connect to VCC (5 volts or 3.3 volts)
- DATA pin: used for sending information to and from the sensor and Raspberry Pi Pico
Some manufacturers provide the DHT22 sensor as a module with three marked pins: GND, VCC, and DATA (or sometimes labeled as -, +, and OUT).
Wiring Diagram
Connect a resistor ranging from 5K to 10K Ohms between the DHT22 sensor and the Raspberry Pi Pico to keep the data line high and enable communication.
Raspberry Pi Pico - DHT22 Sensor Wiring
This image is created using Fritzing. Click to enlarge image
Raspberry Pi Pico - DHT22 Module Wiring
Most DHT22 sensor modules include a built-in resistor, so you don't need to add another one. This makes the wiring or soldering process easier.
This image is created using Fritzing. Click to enlarge image
Raspberry Pi Pico Code - DHT22
Detailed Instructions
Please follow these instructions step by step:
- Ensure that Thonny IDE is installed on your computer.
- Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
- If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
- Wire the components according to the provided diagram.
- Connect the Raspberry Pi Pico to your computer using a USB cable.
- Launch the Thonny IDE on your computer.
- On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
- In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
- Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
- Copy the above code and paste it to the Thonny IDE's editor.
- Save the script to your Raspberry Pi Pico by:
- Click the Save button, or use Ctrl+S keys.
- In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
- Save the file as main.py
- Click the green Run button (or press F5) to run the script. The script will execute.
- Adjust the temperature near the DHT22 sensor by increasing or decreasing the heat.
- Check out the message in the Shell at the bottom of Thonny.
If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.