Skip to content

Commit ff30657

Browse files
authored
Add files via upload
1 parent d181870 commit ff30657

File tree

5 files changed

+649
-0
lines changed

5 files changed

+649
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from micropython import const
2+
from utime import sleep_ms
3+
4+
5+
AHT15_I2C_Address = const(0x38)
6+
7+
AHT15_INIT_CMD = const(0xE1)
8+
AHT15_TRIGGER_MEASURMENT_CMD = const(0xAC)
9+
AHT15_SOFT_RESET_CMD = const(0xBA)
10+
11+
AHT15_INIT_CAL_ENABLE = const(0x08)
12+
AHT15_DATA_MEASURMENT_CMD = const(0x33)
13+
AHT15_DATA_NOP = const(0x00)
14+
15+
16+
class AHT15():
17+
18+
def __init__(self, _i2c):
19+
self.i2c = _i2c
20+
self.i2c_address = AHT15_I2C_Address
21+
22+
self.buf = bytearray(0x07)
23+
for i in range(0, 7):
24+
self.buf[i] = 0x00
25+
26+
self.init()
27+
28+
29+
def init(self):
30+
self.soft_reset()
31+
self.calibrate()
32+
sleep_ms(200)
33+
34+
35+
def write_bytes(self, cmd_1, cmd_2):
36+
self.buf[0] = cmd_1
37+
self.buf[1] = cmd_2
38+
self.buf[2] = AHT15_DATA_NOP
39+
40+
self.i2c.writeto(self.i2c_address, self.buf[0:3])
41+
42+
43+
def read_sensor(self):
44+
t = 0
45+
rh = 0
46+
crc = 0
47+
status = 0
48+
self.write_bytes(AHT15_TRIGGER_MEASURMENT_CMD, AHT15_DATA_MEASURMENT_CMD)
49+
sleep_ms(10)
50+
value = self.i2c.readfrom_into(self.i2c_address, self.buf, 0x07)
51+
52+
status = self.buf[0]
53+
rh = ((self.buf[1] << 12) | (self.buf[2] << 4) | (self.buf[3] >> 4))
54+
t = (((self.buf[3] & 0x0F) << 16) | (self.buf[4] << 8) | (self.buf[5]))
55+
crc = self.buf[6]
56+
57+
rh = ((rh * 100.0) / 0x100000)
58+
t = (((t * 200.0) / 0x100000) - 50.0)
59+
60+
return rh, t, status, crc
61+
62+
63+
def calibrate(self):
64+
self.write_bytes(AHT15_INIT_CMD, AHT15_INIT_CAL_ENABLE)
65+
sleep_ms(600)
66+
67+
68+
def soft_reset(self):
69+
self.buf[0] = AHT15_SOFT_RESET_CMD
70+
self.i2c.writeto(self.i2c_address, self.buf[0:1])
71+
sleep_ms(200)

0 commit comments

Comments
 (0)