Skip to content

Commit

Permalink
Merge pull request #47 from brian-eng/tref
Browse files Browse the repository at this point in the history
Speed up temperature readings
  • Loading branch information
FoamyGuy authored Jan 31, 2025
2 parents ea1cb8d + d03ab19 commit 4e6ba64
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions adafruit_mlx90393.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ def __init__( # pylint: disable=too-many-arguments
self._osr = oversampling
self._gain_current = gain
self._temperature_compensation = temperature_compensation
# Typical value according the application note
self._tref = 0xB668
self._off_x = self._off_y = self._off_z = offset

# Put the device in a known state to start
Expand Down Expand Up @@ -519,6 +521,12 @@ def reset(self) -> None:
print("Resetting sensor")
time.sleep(0.002)
self._transceive(bytes([_CMD_RT]))

# Read the temperature reference from register 0x24
self._tref = self.read_reg(0x24)
if self._debug:
print("Tref = {}".format(hex(self._tref)))

# burn a read post reset
try:
self.magnetic
Expand Down Expand Up @@ -614,9 +622,6 @@ def temperature(self) -> float:
Reads a single temperature sample from the magnetometer.
Temperature value in Celsius
"""
# Read the temperature reference from register 0x24
treference = self.read_reg(0x24)

# Value taken from maximum time of temperature conversion on the datasheet section 12.
# maximum time for temperature conversion = 1603 us
delay = 0.1
Expand All @@ -637,4 +642,4 @@ def temperature(self) -> float:
tvalue = struct.unpack(">H", data[1:3])[0]
# See previous link for conversion formula

return 35 + ((tvalue - treference) / 45.2)
return 35 + ((tvalue - self._tref) / 45.2)

0 comments on commit 4e6ba64

Please sign in to comment.