https://github.com/qengineering/dht22-raspberry-pi
DHT22 sensor on Raspberry Pi with WiringPi
https://github.com/qengineering/dht22-raspberry-pi
dht11 dht22 humidity raspberry-pi raspberry-pi-3 raspberry-pi-4 raspberry-pi-64-os relative-humidity-sensors temperature temperature-sensor wiringpi
Last synced: 5 months ago
JSON representation
DHT22 sensor on Raspberry Pi with WiringPi
- Host: GitHub
- URL: https://github.com/qengineering/dht22-raspberry-pi
- Owner: Qengineering
- License: bsd-3-clause
- Created: 2021-07-07T13:56:42.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-10-01T10:27:24.000Z (9 months ago)
- Last Synced: 2024-11-27T11:36:28.066Z (7 months ago)
- Topics: dht11, dht22, humidity, raspberry-pi, raspberry-pi-3, raspberry-pi-4, raspberry-pi-64-os, relative-humidity-sensors, temperature, temperature-sensor, wiringpi
- Language: C++
- Homepage: https://qengineering.eu/opencv-c-examples-on-raspberry-pi.html
- Size: 402 KB
- Stars: 8
- Watchers: 3
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# DHT22 sensor on Raspberry Pi

## The DHT22 sensor software for a Raspberry Pi with WiringPi
[](https://opensource.org/licenses/BSD-3-Clause)The DHT22 is a temperature and relative humidity sensor. It has a digital converter on board, which communicates with the Raspberry Pi via a 1-wire interface.
----------------
## Hardware connections.
The sensor requires a 3.3V power supply, available at pin 1 of the RPi. Do not connect the sensor to the 5V power supply. It will not break, but the 'high' signals from the Raspberry are 3.3V and, in this case, are not detected. In the diagram, the output is connected to pin 3, but can be connected to most GPIO pins as the pin is defined in the software.
-----------------
## 1-Wire communication.
The DHT22 has its own rather obscure 1-wire interface protocol. It is very different from the standard Dallas 1-wire protocol supported by the Raspberry Pi GPIOs.

The transfer is initiated by a low of 18 mS, followed by a high of 40 µS to give the sensor some time to set up the communication flow. At the same time, the GPIO pin must be configured as an input to receive the pulse train from the sensors.
As can be seen, the length of a high determines a 0 or a 1. So the software comes down to measuring pulse lengths. It can be done through interrupt or polling techniques. Since interrupts are cumbersome and error-prone, it is better to use polling. It means continuously fetching the level of the input pin. When a change occurs, the elapsed time is measured.
Accurately measuring microseconds is also another concern. We modified [Daniel's](https://www.raspberrypi.org/forums/viewtopic.php?t=284053) software. He uses the WiringPi delayMicroseconds() function. As shown below, not the most accurate solution. We use the clock_gettime() function, which turns out to be much better.
Because the Raspberry Pi operating system runs many tasks and threads simultaneously, it happens regularly that a transition is missed and subsequently the outcome errors. It is marked by the boolean Valid.
Below are a few histograms showing the difference between the WiringPi and the clock_gettime() function.
First, only the application runs without other demanding processes.

Next, the same situation but now with different apps.

Finally, when the Raspberry Pi underclocked to 200MHz, something WiringPi's delayMicroseconds() brings into serious problems.
------------------
## Pin numbers.
Please find the correct WiringPi pin numbers in the diagram below.
------------
[](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=CPZTM5BB3FCYL)