Skip to main content

DHT11 & DHT22 Temperature and Humidity Sensor using pigpio

Project description

DHT11 and DHT22 Driver Library based on the pigpio GPIO library.

Installation

pip install pigpio-dht

Supported Versions of Python

Tested against Python >= 3.5.

May work with earlier 3.X versions but this has not been confirmed. Does not work with Python 2.7.

DHT Sensors are Slow

DHT sensors are slow to take readings, and need to settle between reads. For instance, the maximum read rates for the sensors are:

  • DHT11 once every 1 seconds

  • DHT22 once every 2 seconds

This library monitors the read rate and will pause between successive calls to read() to honor these limits.

Examples

One-Shot Read

Take a single reading from the sensor.

Code

# NOTE: The import has a _ not - in the module name.
from pigpio_dht import DHT11, DHT22

gpio = 21 # BCM Numbering

sensor = DHT11(gpio)
#sensor = DHT22(gpio)

result = sensor.read()
print(result)

Output

{'temp_c': 20, 'temp_f': 68.0, 'humidity': 35, 'valid': True}

Also see

read()

Sampled Read

Take many readings (by repeating calling read()) from the sensor and return a normalised result.

Code

# NOTE: The import has a _ not - in the module name.
from pigpio_dht import DHT11, DHT22

gpio = 21 # BCM Numbering

sensor = DHT11(gpio)
#sensor = DHT22(gpio)

result = sensor.sample(samples=5)
print(result)

Output

{'temp_c': 20, 'temp_f': 68.0, 'humidity': 35, 'valid': True}

Also see

sample()

API

The classes DHT11 and DHT22 both extend the base class DHTXX and share a common API.

Constructor: DHT11 | DHT22(gpio, timeout_secs=0.5, use_internal_pullup=True, pi=None)

Parameters

  • gpio GPIO (BCM) pin that data leg of sensor is connected to

  • timeout_secs Sensor timeout in second. Default should be adequate unless you receive a TimeoutError advising you to increase the value wuth calling read() or sample()

  • use_internal_pullup - Enable internal pull-up resistor on gpio

  • pi a custom instance of pigpio.pi()

read(retries=0) raises TimeoutError

Take a single reading from the sensor.

Parameters

  • retries number of times to keep retrying when the result contains valid = False

Returns

A Dictionary in the form {'temp_c': 20, 'temp_f': 68.0, 'humidity': 35, 'valid': True}

Where:

  • temp_c is the temperature in degrees Celsius

  • temp_f is the temperature in degrees Fahrenheit

  • humidity is the relative humidity

  • valid is true only if sensors checksum matches with returned data.

Discard readings where value == False and try again.

Raises

TimeoutError

  • If the sensor on gpio does not respond

  • If the sensor responds within timeout_secs (see _Constructor), but the response cannot be understood by the library. Try increasing timeout_secs

Also see

DHT Sensors are Slow

sample(samples=5, max_retries=None) raises TimeoutError

Take many readings (by repeating calling read()) from the sensor and return a normalised result.

Please note that a call to sample() takes time. For example for the DHT11 with a maximum read rate of once every 1 seconds, 5 samples will take approximately 1 second * 5 samples = 5 seconds.

Parameters:

  • samples number of samples to take

  • max_retries maximum number of times to keep retrying per sample when the result contains valid = False. Default to samples * 2

Raises

TimeoutError

  • Same as for read(), plus

  • If max_retries is reached

Project details


Download files

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

Source Distribution

pigpio-dht-0.3.6.tar.gz (7.6 kB view details)

Uploaded Source

Built Distributions

pigpio_dht-0.3.6-py3-none-any.whl (20.2 kB view details)

Uploaded Python 3

pigpio_dht-0.3.6-py2-none-any.whl (20.2 kB view details)

Uploaded Python 2

File details

Details for the file pigpio-dht-0.3.6.tar.gz.

File metadata

  • Download URL: pigpio-dht-0.3.6.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.3

File hashes

Hashes for pigpio-dht-0.3.6.tar.gz
Algorithm Hash digest
SHA256 da616c75d4d66042c6daa0490a3b5568576b3b227949813d988a4188c037bc9a
MD5 4e1f8768fb3c4397aa6056e272c4122a
BLAKE2b-256 a2c24c422b4185bc516b5c07a9712e68d74390d37d41051cd8059880d0dc49c9

See more details on using hashes here.

File details

Details for the file pigpio_dht-0.3.6-py3-none-any.whl.

File metadata

  • Download URL: pigpio_dht-0.3.6-py3-none-any.whl
  • Upload date:
  • Size: 20.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.3

File hashes

Hashes for pigpio_dht-0.3.6-py3-none-any.whl
Algorithm Hash digest
SHA256 105292901434113047138ba5f4726f970e6902545c16af43d77e28804c4149d9
MD5 4126ee5cfb6b6808d04a732514dfbe20
BLAKE2b-256 31d9fd114417c7fd1585aa27aa9b7508465dd5235eb0b49416fa921931dfe60c

See more details on using hashes here.

File details

Details for the file pigpio_dht-0.3.6-py2-none-any.whl.

File metadata

  • Download URL: pigpio_dht-0.3.6-py2-none-any.whl
  • Upload date:
  • Size: 20.2 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.3

File hashes

Hashes for pigpio_dht-0.3.6-py2-none-any.whl
Algorithm Hash digest
SHA256 032936a8ce1bb0c56b29bd64c92382fea3a2540ea9765ce5036d8bd26563ee30
MD5 9f911207435ac500a5fba79373f9c0b7
BLAKE2b-256 aedc398f7fda1118916dc574610de6fd65e3aeef99600a09243dba8209949336

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page