raspberrypi / pico-examples

BSD 3-Clause "New" or "Revised" License
2.83k stars 820 forks source link

fix dht.c error: can't read data from DHT 11 #485

Open ZhongUncle opened 6 months ago

ZhongUncle commented 6 months ago

Hello, I notice when I use the example gpio/dht_sensor in right way, Pico can't read data from DHT11. It toke a night and after reading datasheet https://www.mouser.com/datasheet/2/758/DHT11-Technical-Data-Sheet-Translated-Version-1143054.pdf, I find this error according 2 errors:

  1. In Line 49~53, it doesn't set high signal after low signal and wait some time. MCU pulls up voltage and waits for DHT11 response (20-40us).
  2. In Line 73, the range of count (accumulated time) is 26~28, because 26~28 means data "0", If it is longer, it means data "1". But for redundancy, I choose 35 (I test it works well).

This is first time I find this error [Why DHT11 not work after some times using C in pico - Raspberry Pi Forums](https://forums.raspberrypi.com/viewtopic.php?t=355462#p2130252), in this post, I use USB and minicom on Mac.

Recently, I buy a Raspberry Pi 4B to re-test this error following the document Raspberry Pi Pico C/C++ SDK by UART, this error still exists. It displays:

截屏2024-03-28 23 07 16

The print's dislocation may be according to I ssh to raspberry pi. But the data from DHT11 also are 0, and print Bad data.

After fixing, it displays:

截屏2024-03-28 23 30 45

It works well.

hasenradball commented 5 months ago

@ZhongUncle have a look at:

I completly reworked this example, see:

a) PR #440 b) PR #450 c) https://github.com/hasenradball/AM2302-Sensor