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.

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.0.tar.gz (29.3 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.0-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pymcu_lib_dht-0.1.0.tar.gz
  • Upload date:
  • Size: 29.3 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.0.tar.gz
Algorithm Hash digest
SHA256 c68a39cb52b6adfa4b750805380d234fd4a6e66ecc6c810a27b24a43ad7ef9f9
MD5 1f1ea29340e0c60e06e51e39f02dd71b
BLAKE2b-256 6ca09d3f135d1b857b334384917e98eaf7f319fb855e282c6d82f43a8b6c3d45

See more details on using hashes here.

Provenance

The following attestation bundles were made for pymcu_lib_dht-0.1.0.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.0-py3-none-any.whl.

File metadata

  • Download URL: pymcu_lib_dht-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.7 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 265917d3a126a813dd6bea9b7048e7917002753c45a035cd7a42d5347e07c1d1
MD5 ce48d09ae6ff9b5946f043279435f190
BLAKE2b-256 802f06811c0fb2bb54b29791d6d070a1407e8ee6de72c1408a2720cdac415657

See more details on using hashes here.

Provenance

The following attestation bundles were made for pymcu_lib_dht-0.1.0-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

0.1.1

2 files

This release

0.1.0 This release

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