Skip to content

fix: restore correct COUNT values for DHT timing threshold#28

Merged
baorepo merged 1 commit intomasterfrom
fix/dht-count-16mhz
Apr 28, 2026
Merged

fix: restore correct COUNT values for DHT timing threshold#28
baorepo merged 1 commit intomasterfrom
fix/dht-count-16mhz

Conversation

@baorepo
Copy link
Copy Markdown
Member

@baorepo baorepo commented Apr 28, 2026

Summary

  • Restore COUNT timing threshold values that were incorrectly reduced in commit 69cabab
  • The bad commit lowered all COUNT values across all CPU frequencies, breaking DHT11/DHT22 sensor reads

Problem

COUNT is the pulse-width threshold used to distinguish '0' bits (~26µs) from '1' bits (~70µs) in the DHT single-wire protocol. The read loop counts delayMicroseconds(1) iterations while the pin stays in a given state, then compares counter > _count.

On 16 MHz AVR, each loop iteration takes ~4-5µs (due to digitalRead overhead), so:

  • '0' bit → counter ≈ 5-6
  • '1' bit → counter ≈ 14-17

With COUNT=2 (the broken value), even a '0' bit exceeds the threshold, causing all bits to be read as '1' → garbage data.

Fix

Restore the battle-tested COUNT values from before 69cabab. Also fix the new frequency entries (10 MHz, 20 MHz, 40 MHz) that were added with incorrect values.

Freq Broken COUNT Restored COUNT
8 MHz 1 3
10 MHz 1 3
16 MHz 2 6
20 MHz 2 6
40 MHz 4 10
48 MHz 5 18
64 MHz 6 20
80 MHz 8 22
160 MHz 16 32
168 MHz 16 40

Fixes #27.

Test plan

  • Verify DHT11 readings on Arduino Uno R3 (16 MHz AVR)
  • Verify DHT22 readings on Arduino Uno R3
  • Verify readings on 8 MHz AVR (e.g. Arduino Pro Mini 3.3V)
  • Verify readings on SAMD21 (48 MHz) platforms like Seeeduino Xiao

Commit 69cabab incorrectly reduced all COUNT values, breaking DHT11
sensor reads on 16 MHz AVR (Arduino Uno R3) and likely other platforms.

COUNT is the threshold to distinguish '0' bits (~26µs pulse) from '1'
bits (~70µs pulse) in the DHT protocol. The reduced values caused all
bits to be read as '1', producing garbage data.

Restore the previously working values from before 69cabab.

Fixes #27.
@baorepo baorepo merged commit 7d973d0 into master Apr 28, 2026
2 checks passed
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.

[Bug] DHT.h : Incorrect COUNT value for Arduino Uno R3 (16 MHz) causes sensor communication failure

2 participants