Skip to content

Instantly share code, notes, and snippets.

@timdenholm
Created April 7, 2018 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timdenholm/98dea4c08cbe99afbf6acfc73bdcc14f to your computer and use it in GitHub Desktop.
Save timdenholm/98dea4c08cbe99afbf6acfc73bdcc14f to your computer and use it in GitHub Desktop.
Raspberry Pi SHT31 Example
import smbus2
import time
# Get I2C bus; SHT31 address 0x44(68)
bus = smbus2.SMBus(1)
bus.write_i2c_block_data(0x44, 0x2C, [0x06])
time.sleep(0.5)
# SHT31 address 0x44(68)
# Read data back from 0x00(00), 6 bytes
# Temp MSB, Temp LSB, Temp CRC, Humididty MSB, Humidity LSB, Humidity CRC
data = bus.read_i2c_block_data(0x44, 0x00, 6)
# Convert data
temp = data[0] * 256 + data[1]
cTemp = -45 + (175 * temp / 65535.0)
fTemp = -49 + (315 * temp / 65535.0)
humidity = 100 * (data[3] * 256 + data[4]) / 65535.0
# Output data
print "Temperature: %.2fC" %cTemp
print "Temperature: %.2fF" %fTemp
print "Humidity: %.2f%%RH" %humidity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment