HTU21D-F Humidity Sensor

The HTU21D-F [1] is an I2C digital humidity sensor with a typical accuracy of ±2 % over the range 5 % to 95 % RH. It also provides temperature with an accuracy of ±1 °C from -30 °C to 90 °C. It has an operating voltage range of 1.5 V to 3.6 V, so you'll need a regulator and logic-level conversion if you want to use it on a 5 V board.

A breakout is available from Adafruit [2]:

HTU21D-F.jpg

The I2C address is #x40 or 64.

Reading the temperature

The following floating-point function reads the temperature in degrees Celsius:

(defun htu21d-temp ()
  (with-i2c (s #x40)
    (write-byte #xE3 s)
    (restart-i2c s 2)
    (- 
     (* 175.72 
        (/ (logior (ash (read-byte s) 8) (read-byte s)) 65536))
     46.85)))

Reading the humidity

The following floating-point function reads the humidity in %RH:

(defun htu21d-humidity ()
(with-i2c (s #x40) (write-byte #xE5 s) (restart-i2c s 2) (round (- (* 125 (/ (logior (ash (read-byte s) 8) (read-byte s)) 65536)) 6))))

Enabling the heater

The sensor also incorporates a heater, to combat condensation. Here's a routine to enable or disable the heater:

(defun htu21d-heater (on)
(with-i2c (s #x40) (write-byte #xE6 s) (write-byte (if on #x06 #x02) s)))

To turn the heater on call:

(htu21d-heater t)

or to turn if off call:

(htu21d-heater nil)

  1. ^ HTU21D-F Datasheet on TE Connectivity.
  2. ^ Adafruit HTU21D-F Temperature & Humidity Sensor Breakout Board on Adafruit.