Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
51 lines (38 sloc) 1.74 KB

The HTU21D temperature/humidity sensor.

A breakout board is available from Sparkfun.

This driver was derived from the synchronous Pyboard-specific driver here. It is designed to be multi-platform and uses uasyncio to achieve asynchronous (non- blocking) operation. The driver maintains temperature and humidity bound variables as a non-blocking background task. Consequently reading the values is effectively instantaneous.



  1. The asynchronous driver.
  2. Test/demo program.

The driver

This provides a single class HTU21D.

This takes two args, i2c (mandatory) and an optional read_delay=10. The former must be an initialised I2C bus instance. The read_delay (secs) determines how frequently the data values are updated.

Public bound values

  1. temperature Latest value in Celcius.
  2. humidity Latest value of relative humidity (%).

Initial readings will not be complete until about 120ms after the class is instantiated. Prior to this the values will be None. To avoid such invalid readings the class is awaitable and may be used as follows.

async def show_values():
    htu = htu21d_mc.HTU21D(i2c)
    await htu  # Will pause ~120ms
    # Data is now valid
    while True:
        fstr = 'Temp {:5.1f} Humidity {:5.1f}'
        print(fstr.format(htu.temperature, htu.humidity))
        await asyncio.sleep(5)

Thermal inertia of the chip packaging means that there is a lag between the occurrence of a temperature change and the availability of accurate readings. There is therefore little practical benefit in reducing the read_delay.