Python library for reading data over i2c (tested on raspberry pi)
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
.gitignore Initial commit Oct 26, 2013
LICENSE Updated Licence to BSD Oct 30, 2013 Update to fix issue with sht31 example. Feb 20, 2016 Added Fahrenheit calculation Aug 21, 2016


Python library for reading data over i2c for a Sensirion SHT21 (tested on Raspberry Pi).

This is a python port of the c version found here:

I have only tried it on my first generation Raspberry Pi, but there have been reports of this working on newer Raspberry Pis. This code should also work with all sensors in the SHT2x series; there have been report of it working with a SHT25.


  • i2c must be enabled
  • you must run the script as root

To enable i2c check

Depending on your raspberry pi model, you may need to use /dev/i2c-0 or /dev/i2c-1


import sht21
with sht21.SHT21(0) as sht21:
    print "Temperature: %s"%sht21.read_temperature()
    print "Humidity: %s"%sht21.read_humidity()

Note that the 0 represents the i2c bus number (as discussed), to use /dev/i2c-1 on a newer Raspberry Pi you need to update the device number:

If you are not root, this is likely to throw IOErrors. Also the calls to read_xxx() may also return None if the checksum fails


Thanks to the kind contribution of an initial version by @nadanks7 and some testing by @jimbowarrior there is now also a basic implementation for the sht31 sensor. More work is required here to support all features of the sensor, but the initial framework is in place.

Usage is similar, but as reading temerature and humidity is now a single operation there is single call to do that.

Note that in this example we are using /dev/i2c-1.

import sht31
with sht31.SHT31(1) as sht31:
    print sht31.check_heater_status()
    print sht31.check_heater_status()
    print sht31.check_heater_status()
    temperature, humidity = sht31.get_temp_and_humidity()
    print "Temperature: %s" % temperature
    print "Humidity: %s" % humidity