Skip to content

Commit

Permalink
Merge pull request #35 from jposada202020/adding_temp
Browse files Browse the repository at this point in the history
adding temperature reading
  • Loading branch information
tannewt authored Feb 17, 2023
2 parents 3a492b1 + 5882d2b commit a1546a6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
32 changes: 32 additions & 0 deletions adafruit_mlx90393.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
_CMD_NOP = const(0x00) # NOP

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

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

return x, y, z

@property
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

# Set the device to single measurement mode
self._transceive(bytes([_CMD_SM | _CMD_TEMP]))

time.sleep(delay)

# Read the 'temp' data
data = self._transceive(bytes([_CMD_RM | _CMD_TEMP]), 2)

# Unpack status and raw int values
self._status_last = data[0]

# from https://www.melexis.com/-/media/files/documents/
# application-notes/mlx90393-temperature-compensation-application-note-melexis.pdf
tvalue = struct.unpack(">H", data[1:3])[0]
# See previous link for conversion formula

return 35 + ((tvalue - treference) / 45.2)
9 changes: 9 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@ Ensure your device works with this simple test.
.. literalinclude:: ../examples/mlx90393_simpletest.py
:caption: examples/mlx90393_simpletest.py
:linenos:

Temperature test
-----------------

Example showing how to measure temperature with the sensor

.. literalinclude:: ../examples/mlx90393_temperature.py
:caption: examples/mlx90393_temperature.py
:linenos:
21 changes: 21 additions & 0 deletions examples/mlx90393_temperature.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# SPDX-FileCopyrightText: 2023 Jose D. Montoya
# SPDX-License-Identifier: MIT

import time
import board
import adafruit_mlx90393

i2c = board.I2C() # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
SENSOR = adafruit_mlx90393.MLX90393(i2c, gain=adafruit_mlx90393.GAIN_1X)


while True:
temp = SENSOR.temperature

print("Temperature: {} °C".format(temp))

# Display the status field if an error occurred, etc.
if SENSOR.last_status > adafruit_mlx90393.STATUS_OK:
SENSOR.display_status()
time.sleep(1.0)

0 comments on commit a1546a6

Please sign in to comment.