Last active
November 7, 2022 23:35
-
-
Save pavelrevak/b655b5e0315fcf11fdbc2d04dd79b4e6 to your computer and use it in GitHub Desktop.
Micropython SHT30: reading temperature and humidity
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
"""Simple module for reading temperature and humidity | |
from SHT30 sensor for micropython | |
it is a simplest version only for inspiration, | |
where is no checking availability of sensor | |
and no checking CRC of data | |
for waiting to measured data is used clock stretching | |
""" | |
def read(i2c, address=68): | |
"""Read temperature and humidity from SHT30 sensor | |
Arguments: | |
i2c: instance of i2c | |
address: I2C address | |
Returns: c, f, h | |
c: temperature in degree Celsius (-45 .. 130) | |
f: temperature in degree Fahrenheit (-49 .. 266) | |
h: relative humidity in % (0 .. 100) | |
""" | |
i2c.writeto(address, b'\x2c\x06') | |
m = i2c.readfrom(address, 6) | |
c = int.from_bytes(m[0:2], 'big') * 175 / 65535 - 45 | |
f = int.from_bytes(m[0:2], 'big') * 315 / 65535 - 49 | |
h = int.from_bytes(m[3:5], 'big') * 100 / 65535 | |
return c, f, h |
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 machine | |
import sht30 | |
scl = machine.Pin(22) | |
sda = machine.Pin(23) | |
i2c0 = machine.I2C(0, scl=scl, sda=sda, freq=50000) | |
print(sht30.read(i2c0, 68)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment