1+ from micropython import const
2+ from utime import sleep_ms
3+
4+
5+ AHT25_I2C_Address = const (0x38 )
6+
7+ AHT25_INIT_CMD = const (0xE1 )
8+ AHT25_TRIGGER_MEASURMENT_CMD = const (0xAC )
9+ AHT25_SOFT_RESET_CMD = const (0xBA )
10+
11+ AHT25_INIT_CAL_ENABLE = const (0x08 )
12+ AHT25_DATA_MEASURMENT_CMD = const (0x33 )
13+ AHT25_DATA_NOP = const (0x00 )
14+
15+
16+ class AHT25 ():
17+
18+ def __init__ (self , _i2c ):
19+ self .i2c = _i2c
20+ self .i2c_address = AHT25_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 ] = AHT25_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 (AHT25_TRIGGER_MEASURMENT_CMD , AHT25_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 (AHT25_INIT_CMD , AHT25_INIT_CAL_ENABLE )
65+ sleep_ms (600 )
66+
67+
68+ def soft_reset (self ):
69+ self .buf [0 ] = AHT25_SOFT_RESET_CMD
70+ self .i2c .writeto (self .i2c_address , self .buf [0 :1 ])
71+ sleep_ms (200 )
0 commit comments