09-29-2016, 09:35 AM
So I got an answer with code from DonH, thank you very much! :thumbs_up:
In order to share the knowledge I am pasting the code he sent me. Essentially you need to tell the sensor to do a measurement and then read the result - which, ironically, is what I did to start with. Except that I only waited 0.02 seconds whereas this code waits 0.5 seconds...
Here's the code, originally from ControlEverything.com and modified for this purpose by DonH:
# Distributed with a free-will license.
# Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
# STS21
# This code is designed to work with the STS21_I2CS I2C Mini Module available from ControlEverything.com.
# https://www.controleverything.com/conten...t_tabset-2
import smbus
import time
# Get I2C bus
bus = smbus.SMBus(1)
# STS21 address, 0x4A(74)
# Select Command
# 0xF3(243) Temperature measurement in NO HOLD mode
bus.write_byte(0x40, 0xF3)
time.sleep(0.5)
# STS21 address, 0x4A(74)
# Read data back, 2 bytes, MSB first
data0 = bus.read_byte(0x40)
data1 = bus.read_byte(0x40)
# Convert the data
temp = (data0 * 256 + data1) & 0xFFFC
cTemp = -46.85 + (175.72 * temp / 65536.0)
fTemp = cTemp * 1.8 + 32
# Output data to screen
print ("Temperature in Celsius is : %.2f C") %cTemp
print ("Temperature in Fahrenheit is : %.2f F") %fTemp
bus.write_byte(0x40, 0xF5)
time.sleep(0.5)
data2 = bus.read_byte(0x40)
data3 = bus.read_byte(0x40)
humidity = (data2 * 256 + data3) & 0xFFFC
rHumidity = ((125*humidity)/65536)-6
print ("Relative humidity is : %.2f ") %rHumidity
In order to share the knowledge I am pasting the code he sent me. Essentially you need to tell the sensor to do a measurement and then read the result - which, ironically, is what I did to start with. Except that I only waited 0.02 seconds whereas this code waits 0.5 seconds...
Here's the code, originally from ControlEverything.com and modified for this purpose by DonH:
# Distributed with a free-will license.
# Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
# STS21
# This code is designed to work with the STS21_I2CS I2C Mini Module available from ControlEverything.com.
# https://www.controleverything.com/conten...t_tabset-2
import smbus
import time
# Get I2C bus
bus = smbus.SMBus(1)
# STS21 address, 0x4A(74)
# Select Command
# 0xF3(243) Temperature measurement in NO HOLD mode
bus.write_byte(0x40, 0xF3)
time.sleep(0.5)
# STS21 address, 0x4A(74)
# Read data back, 2 bytes, MSB first
data0 = bus.read_byte(0x40)
data1 = bus.read_byte(0x40)
# Convert the data
temp = (data0 * 256 + data1) & 0xFFFC
cTemp = -46.85 + (175.72 * temp / 65536.0)
fTemp = cTemp * 1.8 + 32
# Output data to screen
print ("Temperature in Celsius is : %.2f C") %cTemp
print ("Temperature in Fahrenheit is : %.2f F") %fTemp
bus.write_byte(0x40, 0xF5)
time.sleep(0.5)
data2 = bus.read_byte(0x40)
data3 = bus.read_byte(0x40)
humidity = (data2 * 256 + data3) & 0xFFFC
rHumidity = ((125*humidity)/65536)-6
print ("Relative humidity is : %.2f ") %rHumidity