Skip to content

Fixed DHT timing error, Issue #5848 - #6044

Closed
seiuvwcdhol wants to merge 1 commit into
micropython:masterfrom
seiuvwcdhol:dhtfix
Closed

seiuvwcdhol wants to merge 1 commit into
micropython:masterfrom
seiuvwcdhol:dhtfix

Conversation

@seiuvwcdhol

Copy link
Copy Markdown

I fixed an error in the DHT driver which occurs if the system load is high: If there are many Python threads running, the DHT driver constantly reports a timeout error ETIMEDOUT. If the system load is low, there is no error.

The solution is to wait uninterruptable with mp_hal_delay_us_fast instead of interruptable with mp_hal_delay_ms.

As a further optimization if have added a parameter to the function dht_readinto to know if we have DHT11 or DHT22. The reason: DHT11 needs 18ms waiting after pulling the bus low, but DHT22 needs only 1-3ms waiting after pulling the bus low.

The python classes DHT11 and DHT22 have also been extended to store the DHT version in a member variable.

This solves the issue #5848 as was discussed with dpgeorge.

Stefan Hammes, Karlsruhe

@EddieParis

Copy link
Copy Markdown
Contributor

The huge problem with this is on low end targets like esp8266, if you block interrupts for such a long time you interfere with PWM, making light flashes when leds are connected (even 3ms is too much here).

On my side, since ESP8266 is not multi threaded, I took opposite approach and removed the interrupt blocking even for reading the values, to avoid these PWM issues.

So the fix you propose is perfectly fine for your issue but is a total no-go for esp 8266 (and maybe other targets ?)

Maybe it is time to differentiate dht implementations across different ports ?

@projectgus

Copy link
Copy Markdown
Contributor

This is an automated heads-up that we've just merged a Pull Request
that removes the STATIC macro from MicroPython's C API.

See #13763

A search suggests this PR might apply the STATIC macro to some C code. If it
does, then next time you rebase the PR (or merge from master) then you should
please replace all the STATIC keywords with static.

Although this is an automated message, feel free to @-reply to me directly if
you have any questions about this.

@dpgeorge

Copy link
Copy Markdown
Member

Sorry no one got to review this PR until now.

I think it's a good idea to be able to configure the initial timing values for the DHT protocol. I think it should be possible to configure both the 250ms and the 18ms delay, to arbitrary values.

This PR itself is outdated and won't rebase or merge (due to dht.py being in a submodule now). So I'll close it. But it would be nice to be able to configure the timings as I say above.

@dpgeorge dpgeorge closed this Jul 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants