Arduino MicroPython DHT22 Temperature Humidity Sensor
This guide shows you how to use the DHT22 temperature and humidity sensor with Arduino and MicroPython. You will learn:
- How to connect the DHT22 sensor to an Arduino.
- How to connect the DHT22 module to an Arduino.
- How to write MicroPython code for Arduino to read temperature and humidity data from the 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 types of DHT22: one is just the sensor, and the other is a complete module.
The DHT22 sensor has four pins:
- GND pin: Connect to GND (0 volts)
- VCC pin: Connect to VCC (5 volts or 3.3 volts)
- DATA pin: Used for communication between the sensor and Arduino
- NC pin: Not used, 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 between the sensor and Arduino
Some manufacturers provide the DHT22 sensor as a module with three pins labeled GND, VCC, and DATA (or sometimes -, +, and OUT).
Wiring Diagram
Arduino - DHT22 Sensor Wiring
Connect a resistor ranging from 5K to 10K Ohms between the DHT22 sensor and the Arduino to keep the data line high and enable communication. We need to use a breadboard
This image is created using Fritzing. Click to enlarge image
Arduino - 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
Arduino MicroPython Code - DHT22
Detailed Instructions
Here’s instructions on how to run the above MicroPython code on Arduino with Thonny IDE:
- Make sure Thonny IDE is installed on your computer.
- Make sure MicroPython firmware is installed on your Arduino board.
- If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
- Connect the Arduino board to the DHT22 sensor/module according to the provided diagram.
- Connect the Arduino board to your computer with a USB cable.
- Open Thonny IDE and go to Tools Options.
- Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
- Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
- Copy the provided MicroPython code and paste it into Thonny\'s editor.
- Save the MicroPython code to your Arduino by:
- Clicking the Save button or pressing Ctrl+S.
- In the save dialog, choose MicroPython device and name the file main.py.
- Click the green Run button (or press F5) to execute the code.
- Change the temperature near the DHT22 sensor by making it warmer or cooler.
- Check out the message in the Shell at the bottom of Thonny.