Home CodeCircuitpython Raspberry Pi Pico and HTU31D Sensor circuitpython example

Raspberry Pi Pico and HTU31D Sensor circuitpython example

by rp2040guy71

In this article we connect a HTU31D to a Raspberry Pi Pico running Circuitpython

Sensor Information

The HTU31 is one of the smallest and most accurate humidity sensors on the market. Our precision engineering and 20+ years of experience in humidity and temperature combination sensors, enables us to provide fast response time, precision measurement, low hysteresis and sustained performance even when exposed to extreme temperature (-40° to 125°C) and humidity environments (0%- 100%).

The HTU31 humidity sensor includes both digital (D) and analog (V) versions, and combines multiple functions and various interfaces (I2C, analog, voltage output) with an application-friendly operating voltage range (3.3-5.5V with 5V typical).


  • Accuracy:±2% RH
  • Humidity Range:0 ~ 100% RH
  • Mounting Type:Surface Mount
  • Operating Temperature:-40°C ~ 125°C
  • Package / Case:6-VDFN Exposed Pad
  • Response Time : 5s
  • Sensor Type:Humidity, Temperature
  • Supplier Device Package:6-DFN (2.5×2.5)
  • Voltage – Supply : 3V ~ 5.5V

Parts Required


Name Link
Pico Raspberry Pi Pico Development Board
HTU31D HTU31D temperature and humidity sensor module
Connecting cables Aliexpress product link

Amazon.com link

Ebay link



I used a HTU31D sensor

Black for GND
Red for V+
Blue for SDA
Yellow for SCL

rp2040 and HTU31D

rp2040 and HTU31D


Code Example

I used Thonny for development

The following is based on a library , I copied the adafruit_htu31d.mpy library for this device to the lib folder on my Raspberry Pi Pico – https://circuitpython.org/libraries

This is the basic example which comes with the library

[codesyntax lang=”python”]

import time
import board
import adafruit_htu31d
import busio

# Create sensor object, communicating over the board's default I2C bus
i2c = busio.I2C(scl=board.GP1, sda=board.GP0) # uses board.SCL and board.SDA
htu = adafruit_htu31d.HTU31D(i2c)
print("Found HTU31D with serial number", hex(htu.serial_number))

htu.heater = True
print("Heater is on?", htu.heater)
htu.heater = False
print("Heater is on?", htu.heater)

while True:
    temperature, relative_humidity = htu.measurements
    print("Temperature: %0.1f C" % temperature)
    print("Humidity: %0.1f %%" % relative_humidity)



Here is what I saw in Thonny REPL window

Found HTU31D with serial number 0xd62fc4e
Heater is on? True
Heater is on? False
Temperature: 27.1 C
Humidity: 48.5 %

Temperature: 27.0 C
Humidity: 48.7 %

Temperature: 26.9 C
Humidity: 49.0 %

Temperature: 26.8 C
Humidity: 49.2 %

Temperature: 26.7 C
Humidity: 49.4 %



You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More