matthew Champion
Published

SHt3x Raspberry Pi

A short, simple Python program to read the SHT31-D and probably other types of sht3x-dis sensors directly into Raspberry Pi (I2C interface).

BeginnerShowcase (no instructions)1 hour5,288
SHt3x Raspberry Pi

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
and a closed cube SHT-31-d sensor breakout board
×1

Story

Read more

Code

Sht31-d.py

Python
#!/usr/bin/env python

import time

import smbus
import sys


#Write the read sensor command
bus.write_byte_data(0x45, 0x24, 0x00)
time.sleep(0.01) #This is so the sensor has tme to preform the mesurement and write its registers before you read it

# Read data back, 8 bytes, temperature MSB first then lsb, Then skip the checksum bit then humidity MSB the lsb.
data0 = bus.read_i2c_block_data(0x45, 0x00, 8)
s = data0[0]

i = data0[1]

t = data0[2]

y = data0[3]

u= data0[4]

o= data0[5]

p = data0[6]

q = data0[7]




t_val = (data0[0]<<8) + i #convert the data

h_val = (data0[3] <<8) + u     # Convert the data
T = ((175.72 * t_val) / 65536.0 ) - 45 #do the maths from datasheet
H = ((100 * h_val) / 65536.0 )

print ("{:.2f}".format(T))
print ("{:.2f}".format(H))

Credits

matthew Champion
6 projects • 4 followers
Llandovery, Wales
Contact

Comments

Please log in or sign up to comment.