The most reliable dht22 sensor reader!
This project implements a Raspberry Pi
kernel module
for the DHT22 temperature and humidity sensor.
The goal of the driver is to read multiple sensors so that
the GPIOs
can be dynamically set without reloading the kernel module.
The sensor data can easely read from /dev/dht22mX character devices.
This kernel module uses hardware interrupts
to collect sensor data, which results in much more reliable results (fewer crc errors) than
user-mode readers that constantly monitor the status of the gpio.
I was created a project which handle multiple DHT22 sensors. My code collected statistics on the number of successful and failed readings, and I found that some cases approximately 20 percent of the readings were erroneous. It is very big amount of error!
Initially, I attempted to improve the user-space reading code, but the DHT22 sensor transmits data using so very short pulses (20, 50, and 70 microseconds) which makes very hard to read without loosing a bit. Accurately capturing these signals with conventional GPIO polling proved to be unreliable. Due to the operation system task switching, interrupts, and system load, the number of failed readings remained high.
To achieve more precise timing and improve reading accuracy, I turned to kernel module development. By handling GPIO signal processing at the kernel level, the module ensures much finer timing control, significantly reducing read errors and enhancing overall reliability.
My moderately loaded Raspberri Pi with 4 sensor produce the following statistics:
With user mode reader
With kernel mode reader
It can be seen that with the traditional user mode reading method the percentage of errors is 5-20%, while with the dht22m kernel module it is less than 1%. (typically less than 0.1%)
To build the kernel module, you need to first install the base compiler tools and linux header packages:
sudo apt install build-essential
# On 64 bit systems:
sudo apt install linux-headers-rpi-v8
# On 32 bit systems
sudo apt install linux-headers-rpi-v7
Clone dht22m sources from github. To compile module enter the dht22m directory and build & install it.
git clone https://github.com/hyper-prog/dht22m
cd dht22m
sudo make
sudo make install
If success, you can load the module:
sudo modprobe dht22m
In case you would like to make the dht22m module loads after reboot run:
sudo echo "dht22m" > /etc/modules-load.d/dht22m.conf
You can send your dht22m sensor GPIOs numbers to a
sysfs file: /sys/class/dht22m/gpiolist
The GPIO numbers are separated by spaces. After If success the /dev/dht22mX
devices will be created in same order as the GPIOs listed in the sysfs file.
echo "514 515 534" > /sys/class/dht22m/gpiolist
You can always check the GPIOs settings of your dht22 devices
cat /sys/class/dht22m/gpiolist
514 515 534
ls /dev/dht22*
/dev/dht22m0 /dev/dht22m1 /dev/dht22m2
Note: The kernel module is written in such a way that if a configuration request is received matches the running configuration, recognizes the matching and does nothing. So the user programs can safely send the necessary settings at startup, it will not cause kernel overhead.
The dht22m module expects Linux global GPIO numbers, not BCM GPIO
numbers or physical pin numbers.
Install the gpiod tools if they are not already installed:
sudo apt install gpiod
List the available GPIO chips, and gpio ports:
#List gpio chips
gpiodetect
#List gpio lines
gpioinfo
Find the GPIO line you want to use. For example, BCM GPIO24 corresponds
to line offset 24 on the Raspberry Pi 4's pinctrl-bcm2711 GPIO chip.
Run the following command to display the base number of each GPIO chip:
for c in /sys/class/gpio/gpiochip*; do
echo "$(cat "$c/label"): base=$(cat "$c/base"), ngpio=$(cat "$c/ngpio")"
done
The Linux global GPIO number is calculated as:
Linux global GPIO number = GPIO chip base + GPIO line offset
For example, on a Raspberry Pi 4 where the pinctrl-bcm2711 GPIO chip
has a base number of 512:
| BCM GPIO | Base | Linux global GPIO number |
|---|---|---|
| 22 | 512 | 534 |
| 23 | 512 | 535 |
| 27 | 512 | 539 |
Important: GPIO chip base numbers may vary depending on the Raspberry Pi model and kernel configuration. Always check the values on your own system.
Write the Linux global GPIO numbers to /sys/class/dht22m/gpiolist,
separated by spaces.
For the Raspberry Pi 4 example above:
echo "534 535 539" | sudo tee /sys/class/dht22m/gpiolist
To verify the configuration:
cat /sys/class/dht22m/gpiolist
The corresponding device nodes (/dev/dht22m0, /dev/dht22m1, etc.)
are created in the same order as the GPIO numbers in the list.
You can read temperature and humidity values by simple read from /dev/dht22mX device files.
cat /dev/dht22m*
Ok;19.7;38.2
Ok;20.1;42.1
Ok;11.7;26.0
The device files could send the following strings
| Received string | Possible events / meaning |
|---|---|
Ok;TEMPERATURE;HUMIDITY |
Successful read, with the read values. |
ChecksumError |
Checksum error during the read. Wait 2 sec and read again! |
ReadTooSoon |
Read the sensor too quickly. Read later! |
IOError |
GPIO/Sensor/Other error |
NotRead |
Data not read |
The hardware connection of the dht22 sensor is the same as the method described in user mode reading, which can be found here https://github.com/hyper-prog/DHT22-Raspberry-Pi
To learn more about Linux kernel modules in general, see The Linux Kernel Module Programming Guide. To learn more about the DHT22 / AM2302 sensor, see the data sheet.
- Written by Péter Deák (C) hyper80@gmail.com, License GPLv2
- Thanks to the Google and Lars Engebretsen for sensor handling tips and codes!
- The author wrote this project entirely as a hobby. Any help is welcome!



