Unable to read DHT22 sensor #191
-
I'm trying to port old pi4j-v1 code to the new API. Reading a DHT22 involves using a pin alternatively as an input and an output. When reading input, the duration between the edges encodes the desired information with the need to differentiate between 70us and 27us pulses. I am loosely following this pigpio python implementation I create the input and output for the pin like so:
A reading is then triggered with the pin as an output before switching to an input and registering a listener (this is run every 30 seconds so the created input and output are recycled with
The listener simply tallies up the edges and logs the
When set as an input, I am only able to detect from 0 to 2 edges, a far cry from the 40 that the chip emits. And the spacing between those edges is measured as ~3ms up to ~15ms, an order of magnitude longer than what is expected. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 1 reply
-
In the CrowPi project, students also tried many different approaches to read correct values from the DHT22. But it seems this type of component is not well-suited to be used in combination with an operating system, compared to a realtime system or microcontroller. They found a solution by using a Linux system driver as you can read here: https://github.com/Pi4J/pi4j-example-crowpi/blob/main/src/main/java/com/pi4j/crowpi/components/HumiTempComponent.java |
Beta Was this translation helpful? Give feedback.
-
There is also an open issue regarding multipurpose GPIO: #26 |
Beta Was this translation helpful? Give feedback.
-
Possible workaround (for timing issues): If not occupied otherwise, maybe the SPI pins can be misappropriated for such purposes. I'm doing something similar to control WS2812B LEDs which require timings in the range of a few hundred nano seconds. |
Beta Was this translation helpful? Give feedback.
-
Turns out it works well enough by ditching the edge listeners for active polling. You only need to do this for a few ms on the DHT22 so GC pauses are no big issue. The hardest part remains the delay to reconfigure the pin which often exceeds the 40-80us window meaning some bits can be lost. But you can always repeat the operation on a loop until you get a clean read.
|
Beta Was this translation helpful? Give feedback.
-
@aburnett397, do you mind sharing the complete example of it? I'm trying to follow your steps here by reading data from AM2302 (same as DHT22). And this trivial "hello PI world" task turned into an endless number of trials and errors that end up constantly receiving zero values. I've also tried the HumiTempComponent.java suggested by @FDelporte, but it also giving me zeros back. At the same time, these measurements work pretty well through Python's Adafruit_DHT project. Here is what I've got: Debian GNU/Linux 11 (bullseye) / openjdk 11.0.15+10 / Pi4J 2.1.1 / AM2302 with data pin connected to PI's Physical Pin 7 (GPIO4). |
Beta Was this translation helpful? Give feedback.
-
Works like magic! Thanks a lot, @aburnett397 for sharing that! 🙏 |
Beta Was this translation helpful? Give feedback.
Turns out it works well enough by ditching the edge listeners for active polling. You only need to do this for a few ms on the DHT22 so GC pauses are no big issue. The hardest part remains the delay to reconfigure the pin which often exceeds the 40-80us window meaning some bits can be lost. But you can always repeat the operation on a loop until you get a clean read.
For anyone interested I used this instead of listeners and got a similar result to what I had with wiringPi: