88
99This is a CircuitPython driver for the SI7021 temperature and humidity sensor.
1010
11- * Author(s): Radomir Dopieralski
11+ * Author(s): Radomir Dopieralski, Chris Balmer, Ian Grant
1212
1313Implementation Notes
1414--------------------
4040_RESET = const (0xFE )
4141_READ_USER1 = const (0xE7 )
4242_USER1_VAL = const (0x3A )
43+ _ID1_CMD = bytearray ([0xFA , 0x0F ])
44+ _ID2_CMD = bytearray ([0xFC , 0xC9 ])
4345
4446
4547def _crc (data ):
@@ -55,6 +57,36 @@ def _crc(data):
5557 return crc
5658
5759
60+ def _convert_to_integer (bytes_to_convert ):
61+ """Use bitwise operators to convert the bytes into integers."""
62+ integer = None
63+ for chunk in bytes_to_convert :
64+ if not integer :
65+ integer = chunk
66+ else :
67+ integer = integer << 8
68+ integer = integer | chunk
69+ return integer
70+
71+
72+ def _get_device_identifier (identifier_byte ):
73+ """
74+ Convert the identifier byte to a device identifier (model type).
75+ Values are based on the information from page 24 of the datasheet.
76+ """
77+ if identifier_byte in (0x00 , 0xFF ):
78+ identifier_string = "Engineering sample"
79+ elif identifier_byte == 0x0D :
80+ identifier_string = "Si7013"
81+ elif identifier_byte == 0x14 :
82+ identifier_string = "Si7020"
83+ elif identifier_byte == 0x15 :
84+ identifier_string = "Si7021"
85+ else :
86+ identifier_string = "Unknown"
87+ return identifier_string
88+
89+
5890class SI7021 :
5991 """
6092 A driver for the SI7021 temperature and humidity sensor.
@@ -141,3 +173,38 @@ def start_measurement(self, what):
141173 elif self ._measurement != what :
142174 raise RuntimeError ("other measurement in progress" )
143175 self ._measurement = what
176+
177+ @property
178+ def serial_number (self ):
179+ """The device's unique ID (serial number)."""
180+ return self ._get_device_info ()[0 ]
181+
182+ @property
183+ def device_identifier (self ):
184+ """A device identifier (model type) string."""
185+ return self ._get_device_info ()[1 ]
186+
187+ def _get_device_info (self ):
188+ """
189+ Get the serial number and the sensor identifier (model type).
190+ The identifier is part of the bytes returned for the serial number.
191+ Source: https://github.com/chrisbalmer/micropython-si7021
192+ """
193+ # Serial 1st half
194+ data = _ID1_CMD
195+ id1 = bytearray (8 )
196+ with self .i2c_device as i2c :
197+ i2c .write_then_readinto (data , id1 )
198+ # Serial 2nd half
199+ data = _ID2_CMD
200+ id2 = bytearray (6 )
201+ with self .i2c_device as i2c :
202+ i2c .write_then_readinto (data , id2 )
203+ # Combine the two halves
204+ combined_id = bytearray (
205+ [id1 [0 ], id1 [2 ], id1 [4 ], id1 [6 ], id2 [0 ], id2 [1 ], id2 [3 ], id2 [4 ]]
206+ )
207+ # Convert serial number and extract identifier part
208+ serial = _convert_to_integer (combined_id )
209+ identifier = _get_device_identifier (id2 [0 ])
210+ return serial , identifier
0 commit comments