-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAHT20.py
71 lines (48 loc) · 1.83 KB
/
AHT20.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from micropython import const
from utime import sleep_ms
AHT20_I2C_Address = const(0x38)
AHT20_INIT_CMD = const(0xE1)
AHT20_TRIGGER_MEASURMENT_CMD = const(0xAC)
AHT20_SOFT_RESET_CMD = const(0xBA)
AHT20_INIT_CAL_ENABLE = const(0x08)
AHT20_DATA_MEASURMENT_CMD = const(0x33)
AHT20_DATA_NOP = const(0x00)
class AHT20():
def __init__(self, _i2c):
self.i2c = _i2c
self.i2c_address = AHT20_I2C_Address
self.buf = bytearray(0x07)
for i in range(0, 7):
self.buf[i] = 0x00
self.init()
def init(self):
self.soft_reset()
self.calibrate()
sleep_ms(200)
def write_bytes(self, cmd_1, cmd_2):
self.buf[0] = cmd_1
self.buf[1] = cmd_2
self.buf[2] = AHT20_DATA_NOP
self.i2c.writeto(self.i2c_address, self.buf[0:3])
def read_sensor(self):
t = 0
rh = 0
crc = 0
status = 0
self.write_bytes(AHT20_TRIGGER_MEASURMENT_CMD, AHT20_DATA_MEASURMENT_CMD)
sleep_ms(10)
value = self.i2c.readfrom_into(self.i2c_address, self.buf, 0x07)
status = self.buf[0]
rh = ((self.buf[1] << 12) | (self.buf[2] << 4) | (self.buf[3] >> 4))
t = (((self.buf[3] & 0x0F) << 16) | (self.buf[4] << 8) | (self.buf[5]))
crc = self.buf[6]
rh = ((rh * 100.0) / 0x100000)
t = (((t * 200.0) / 0x100000) - 50.0)
return rh, t, status, crc
def calibrate(self):
self.write_bytes(AHT20_INIT_CMD, AHT20_INIT_CAL_ENABLE)
sleep_ms(600)
def soft_reset(self):
self.buf[0] = AHT20_SOFT_RESET_CMD
self.i2c.writeto(self.i2c_address, self.buf[0:1])
sleep_ms(200)