Skip to content

Fix gpio/dht_sensor - #793

Open
keytouch wants to merge 1 commit into
raspberrypi:developfrom
keytouch:fix-dht
Open

keytouch wants to merge 1 commit into
raspberrypi:developfrom
keytouch:fix-dht

Conversation

@keytouch

Copy link
Copy Markdown

As I was testing, without prior sleep_us(2) before waiting for low signal, it turned out to be Bad data.
Tested on DHT11, I don't own a DHT22, but the line protocol should be the same.

  • sleep_us(2) before waiting for low signal

  • check iteration variable j to avoid out of bound write on data[5]

  • flip last directly instead of polling again

  • 1 bit is 70us and 0 bit is about 26-28us, fix the check on polling count

@github-actions

Copy link
Copy Markdown

Please do not submit against master, use develop instead

@keytouch
keytouch changed the base branch from master to develop September 26, 2026 10:30
@keytouch
keytouch marked this pull request as draft September 26, 2026 10:31
@keytouch
keytouch marked this pull request as ready for review September 26, 2026 10:31
Comment thread gpio/dht_sensor/dht.c Outdated
Comment thread gpio/dht_sensor/dht.c Outdated
Comment thread gpio/dht_sensor/dht.c Outdated
* sleep_us(2) before waiting for low signal

* check iteration variable j to avoid out of bound write on data[5]

* flip last directly instead of polling again

* 1 bit is 70us and 0 bit is about 26-28us, fix the check on polling count
@lurch

lurch commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

Changes look reasonable to me, but I don't have a DHT-11 or DHT-22 sensor to check that the code still works correctly.

@keytouch

Copy link
Copy Markdown
Author

I purchased a DHT-22 sensor, with the following debug code insertion, it reads well.

diff --git a/gpio/dht_sensor/dht.c b/gpio/dht_sensor/dht.c
index 14cb7fa..4178a46 100644
--- a/gpio/dht_sensor/dht.c
+++ b/gpio/dht_sensor/dht.c
@@ -47,6 +47,7 @@ void read_from_dht(dht_reading *result) {
     int data[5] = {0, 0, 0, 0, 0};
     uint last = 1;
     uint j = 0;
+    uint count_seq[85] = {0};
 
     gpio_set_dir(DHT_PIN, GPIO_OUT);
     gpio_put(DHT_PIN, 0);
@@ -65,6 +66,7 @@ void read_from_dht(dht_reading *result) {
             sleep_us(1);
             if (count == 255) break;
         }
+        count_seq[i] = count;
         if (count == 255) break;
         last = !last;
 
@@ -78,6 +80,18 @@ void read_from_dht(dht_reading *result) {
     gpio_put(LED_PIN, 0);
 #endif
 
+    printf("count_seq: ");
+    for (uint i = 0; i < 85; i++) {
+        printf("%d,", count_seq[i]);
+    }
+    printf("\n");
+    printf("j: %d\n", j);
+    printf("data: ");
+    for (uint i = 0; i < 5; i++) {
+        printf("%d,", data[i]);
+    }
+    printf("\n");
+
     if ((j >= 40) && (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF))) {
         result->humidity = (float) ((data[0] << 8) + data[1]) / 10;
         if (result->humidity > 100) {

I recorded the actual loop count, my output is as follows:

count_seq: 32,81,82,53,22,51,22,52,22,51,22,51,22,52,22,51,68,52,24,51,69,51,22,51,22,51,69,51,68,52,22,51,22,51,25,52,22,51,22,51,22,51,22,52,22,51,22,51,22,51,71,52,22,51,22,51,22,51,22,52,68,51,23,51,22,51,25,51,69,51,22,51,68,52,22,51,22,51,22,51,69,51,68,0,0,
j: 40
data: 2,152,1,8,163,
Humidity = 66.4%, Temperature = 26.4C (79.5F)
count_seq: 32,82,81,54,22,51,22,51,22,52,22,51,22,52,22,51,68,52,24,51,69,51,22,52,22,51,69,51,22,52,68,52,68,51,72,51,22,52,22,51,22,51,23,51,22,51,22,52,22,51,71,51,23,51,22,51,22,52,22,51,69,51,22,51,22,52,25,51,69,51,22,51,69,51,22,52,22,51,22,51,69,51,22,0,0,
j: 40
data: 2,151,1,8,162,
Humidity = 66.3%, Temperature = 26.4C (79.5F)
count_seq: 32,81,81,53,22,52,22,51,22,51,22,51,22,51,22,51,69,51,24,51,68,51,22,52,22,51,68,51,69,51,22,51,22,51,25,51,22,51,22,51,22,51,22,51,22,51,22,52,22,51,70,51,22,51,23,51,22,51,22,51,68,51,22,51,22,51,26,51,68,51,22,51,69,51,22,51,22,51,22,51,68,52,68,0,0,
j: 40
data: 2,152,1,8,163,
Humidity = 66.4%, Temperature = 26.4C (79.5F)
count_seq: 32,81,81,53,22,51,22,51,22,52,22,51,22,51,22,51,68,51,24,51,69,51,22,51,22,51,68,51,69,51,22,51,22,51,25,51,22,51,22,51,22,51,22,51,22,51,22,51,22,52,70,51,22,51,22,52,22,51,22,51,68,51,22,51,22,52,25,51,68,51,22,51,69,51,22,51,22,51,22,51,68,51,69,0,0,
j: 40
data: 2,152,1,8,163,
Humidity = 66.4%, Temperature = 26.4C (79.5F)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants