Skip to content

Commit ba029a6

Browse files
committed
adding temperature reading
1 parent 3a492b1 commit ba029a6

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

adafruit_mlx90393.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
_CMD_NOP = const(0x00) # NOP
5858

5959
_CMD_AXIS_ALL = const(0xE) # X+Y+Z axis bits for commands
60+
_CMD_TEMP = const(0x01) # Temperature bit for commands
6061

6162
_CMD_REG_CONF1 = const(0x00) # Gain
6263
_CMD_REG_CONF2 = const(0x01) # Burst, comm mode
@@ -518,3 +519,31 @@ def magnetic(self) -> Tuple[float, float, float]:
518519
z *= _LSB_LOOKUP[hallconf_index][self._gain_current][self._res_z][1]
519520

520521
return x, y, z
522+
523+
def read_temp(self):
524+
"""
525+
Reads a single temperature sample from the magnetometer.
526+
"""
527+
# Set conversion delay based on filter and oversampling
528+
treference = self.read_reg(0x24)
529+
530+
# from maximum time of temperature conversion on the datasheet section 12. 1603 us
531+
delay = 0.1
532+
533+
# Set the device to single measurement mode
534+
self._transceive(bytes([_CMD_SM | _CMD_TEMP]))
535+
536+
time.sleep(delay)
537+
538+
# Read the 'temp' data
539+
data = self._transceive(bytes([_CMD_RM | _CMD_TEMP]), 2)
540+
541+
# Unpack status and raw int values
542+
self._status_last = data[0]
543+
544+
# from https://www.melexis.com/-/media/files/documents/
545+
# application-notes/mlx90393-temperature-compensation-application-note-melexis.pdf
546+
tvalue = struct.unpack(">H", data[1:3])[0]
547+
temperature = 35 + ((tvalue - treference) / 45.2) # See previous link
548+
549+
return temperature

0 commit comments

Comments
 (0)