ROS2 Jazzy driver for the DHT22 (AM2302) temperature and humidity sensor over a single GPIO pin.
- Publishes
sensor_msgs/Temperatureondht22/temperature - Publishes
sensor_msgs/RelativeHumidityondht22/humidity fake_modefor testing without hardware (random Gaussian data)- BCM GPIO pin configurable (default: GPIO4)
- Runtime
publish_ratechange viaros2 param set - Calibration and reset services
There is also a dht11_env driver for DHT11. The DHT22 provides significantly better specifications:
| Feature | DHT11 (dht11_env) | DHT22 (this package) |
|---|---|---|
| Temperature range | 0 ~ 50 °C | -40 ~ 80 °C |
| Temperature accuracy | ±2 °C | ±0.5 °C |
| Humidity range | 20 ~ 90 %RH | 0 ~ 100 %RH |
| Humidity accuracy | ±5 %RH | ±2-5 %RH |
| Max sampling rate | ~1 Hz | ~0.5 Hz |
| Resolution | 1 °C / 1 %RH | 0.1 °C / 0.1 %RH |
Choose dht11_env for low-cost prototyping where accuracy is not critical. Choose this package for better accuracy and wider measurement range.
- ROS 2 Jazzy
- Python 3
- Real hardware only:
adafruit-circuitpython-dht(pip install adafruit-circuitpython-dht)libgpiod2(sudo apt install libgpiod2)
cd ~/ros2_ws colcon build --packages-select dht22_env --symlink-install source install/setup.bashros2 launch dht22_env dht22_launch.pyros2 run dht22_env dht22_node.pyros2 launch dht22_env dht22_launch.py \ params_file:=path/to/your_params.yamlSet fake_mode: false and gpio_pin in your YAML file.
ros2 topic echo /dht22/temperature ros2 topic echo /dht22/humidity| Parameter | Type | Default | Description |
|---|---|---|---|
fake_mode | bool | true | Generate random data without hardware |
gpio_pin | int | 4 | BCM GPIO pin number (2-27) |
publish_rate | float | 0.5 | Publishing rate in Hz (DHT22 max ~0.5 Hz) |
frame_id | string | dht22_link | TF frame ID |
temperature_variance | float | 0.0 | Temperature variance (0 = unknown) |
humidity_variance | float | 0.0 | Humidity variance (0 = unknown) |
| Service | Type | Description |
|---|---|---|
dht22/calibrate | std_srvs/srv/Trigger | Collect samples for 10 s, report averages |
dht22/reset | std_srvs/srv/Trigger | Clear bias, reinitialize sensor |
dht22_env/ ├── CMakeLists.txt ├── package.xml ├── config/ │ └── dht22_params.yaml ├── launch/ │ └── dht22_launch.py ├── dht22_env/ │ ├── __init__.py │ └── dht22_driver.py ├── nodes/ │ └── dht22_node.py ├── test/ │ └── test_dht22_node.py ├── .gitignore ├── LICENSE ├── CONTRIBUTING.md └── README.md Tested on Ubuntu 24.04 (WSL2) with fake_mode: true.
| Test Category | Test | Result |
|---|---|---|
| Topics | dht22/temperature publishes sensor_msgs/Temperature | PASS |
| Topics | dht22/humidity publishes sensor_msgs/RelativeHumidity | PASS |
| Services | dht22/calibrate returns success=True | PASS |
| Services | dht22/reset returns success=True | PASS |
| Parameters | publish_rate runtime change | PASS |
| Shutdown | Clean exit | PASS |
| Linting | pep257, flake8, copyright, xmllint | PASS |
MIT