Created
April 7, 2018 02:21
-
-
Save timdenholm/98dea4c08cbe99afbf6acfc73bdcc14f to your computer and use it in GitHub Desktop.
Raspberry Pi SHT31 Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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