Skip to main content

pymcu-lib-dht

DHT-family temperature & humidity sensor driver for PyMCU — DHT11, DHT22/AM2302, and DHT21/AM2301 — compiled to native machine code, with no interpreter on the chip.

pymcu install dht

Two module names, three APIs, one driver

Which module name and shape you get depends on the stdlib layer your project declares. Native and MicroPython both import from dht — that is real MicroPython's own module name; CircuitPython imports from adafruit_dht, matching that ecosystem's real name instead.

Native (stdlib unset):

from dht import DHT11, DHT22   # DHT21 is also available: an alias of DHT22

sensor = DHT22("PD2")
sensor.measure()
humidity = sensor.humidity()        # uint16, tenths of %RH: 653 = 65.3
temperature = sensor.temperature()  # int16, tenths of C: -55 = -5.5

MicroPython (stdlib = ["micropython"]) — a MicroPython script, unchanged:

from machine import Pin
from dht import DHT11

sensor = DHT11(Pin(2))
sensor.measure()
print(sensor.humidity(), sensor.temperature())

CircuitPython (stdlib = ["circuitpython"]) — an adafruit_dht script, unchanged:

import board
from adafruit_dht import DHT22

sensor = DHT22(board.D2)
print(sensor.temperature, sensor.humidity)   # float, matching upstream

None of those files contains a single match __CHIP__.arch: the layer adapters are plain Python written against each API as it is documented upstream. Everything that has to know about a chip lives in one private module, _dht.core, and the compiler folds it away; everything that has to know what a byte means for a given sensor model lives in _dht.decode, which needs no chip dispatch of its own.

DHT11 vs. DHT22 vs. DHT21

  • DHT11 — integer %RH and C, no fractional resolution, no negative temperatures. Cheapest, and the smallest to compile against (see below).
  • DHT22/AM2302 — one decimal digit, negative temperatures, wider and more accurate range.
  • DHT21/AM2301 — the same protocol and byte layout as the DHT22. This library exposes it as DHT21 = DHT22, a plain alias, not a second implementation.

See docs/sensors.md for the full comparison and docs/protocol.md for the byte-level differences.

Cost

The whole driver is @inline: no object is allocated on the device, and a read compiles down to the bit-banging routine at the call site (or a single shared subroutine where the compiler's outliner judges that cheaper). Nothing is held in SRAM between reads.

dht.py and compat/micropython/dht.py report tenths as a plain int16/uint16 rather than a float, on both DHT11 and DHT22: two isolated probes measured on the emulator put a float-returning driver at 4.9x the flash of an int16-tenths one (680 vs. 140 bytes) for the same logic. See docs/accuracy.md for the measurement. adafruit_dht.py is the one exception — it matches adafruit_dht's real float signature on purpose, since fidelity to that upstream API is the point of the module.

Measured figures per chip are published in the library index.

Supported hardware

The whole DHT family on AVR (ATmega328P and friends), data line on PD2-PD7. Timing is measured on the AVR emulator rather than assumed — see tests/test_timing.py for the start-signal and bit-decode-threshold checks.

Other architectures raise a compile-time error rather than returning something that looks like it worked. Ports are welcome: _dht/avr.py is the whole contract, and _dht/decode.py (the per-model byte decoding) needs no changes for a new port.

Your firmware may use any name it likes at module level: the driver's own parameters are ordinary words (mask, bit, start_low_ms), and a global that collides with one of them no longer reaches inside. That took a compiler fix, so the suite measures it rather than assuming it — see tests/test_timing.py.

See docs/ for the wire protocol, wiring diagrams, accuracy limits, sensor comparison, and a porting guide for a new architecture.

License

MIT.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymcu_lib_dht-0.1.1.tar.gz (34.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pymcu_lib_dht-0.1.1-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file pymcu_lib_dht-0.1.1.tar.gz.

File metadata

  • Download URL: pymcu_lib_dht-0.1.1.tar.gz
  • Upload date:
  • Size: 34.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pymcu_lib_dht-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4d557eabfca98b8e439608a1e76e0428619132818bfd6bbdcfc2f143f88c2308
MD5 c75854c33543c469fd8f2bd5a1cf44b0
BLAKE2b-256 92c9f44858ac2181dab86ca7654f8ab9506132ffcd06d77ebf9819eec7822f3c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pymcu_lib_dht-0.1.1.tar.gz:

Publisher: publish.yml on PyMCU/pymcu-lib-dht

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pymcu_lib_dht-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pymcu_lib_dht-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pymcu_lib_dht-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 00e0244f3e6fef50aecd58ef725d0c6a7082ff1b60461739c753bf8a27ed5e1f
MD5 07f8a1f21aaa9ee67ac2ba88761b576d
BLAKE2b-256 e62a8d518e5fb1a52ab9182ac93d5f4c668a33d338acb4b7d1028b471f863976

See more details on using hashes here.

Provenance

The following attestation bundles were made for pymcu_lib_dht-0.1.1-py3-none-any.whl:

Publisher: publish.yml on PyMCU/pymcu-lib-dht

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page