Skip to content

Commit a1546a6

Browse files
authored
Merge pull request #35 from jposada202020/adding_temp
adding temperature reading
2 parents 3a492b1 + 5882d2b commit a1546a6

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

adafruit_mlx90393.py

Lines changed: 32 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,34 @@ 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+
@property
524+
def temperature(self) -> float:
525+
"""
526+
Reads a single temperature sample from the magnetometer.
527+
Temperature value in Celsius
528+
"""
529+
# Read the temperature reference from register 0x24
530+
treference = self.read_reg(0x24)
531+
532+
# Value taken from maximum time of temperature conversion on the datasheet section 12.
533+
# maximum time for temperature conversion = 1603 us
534+
delay = 0.1
535+
536+
# Set the device to single measurement mode
537+
self._transceive(bytes([_CMD_SM | _CMD_TEMP]))
538+
539+
time.sleep(delay)
540+
541+
# Read the 'temp' data
542+
data = self._transceive(bytes([_CMD_RM | _CMD_TEMP]), 2)
543+
544+
# Unpack status and raw int values
545+
self._status_last = data[0]
546+
547+
# from https://www.melexis.com/-/media/files/documents/
548+
# application-notes/mlx90393-temperature-compensation-application-note-melexis.pdf
549+
tvalue = struct.unpack(">H", data[1:3])[0]
550+
# See previous link for conversion formula
551+
552+
return 35 + ((tvalue - treference) / 45.2)

docs/examples.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,12 @@ Ensure your device works with this simple test.
66
.. literalinclude:: ../examples/mlx90393_simpletest.py
77
:caption: examples/mlx90393_simpletest.py
88
:linenos:
9+
10+
Temperature test
11+
-----------------
12+
13+
Example showing how to measure temperature with the sensor
14+
15+
.. literalinclude:: ../examples/mlx90393_temperature.py
16+
:caption: examples/mlx90393_temperature.py
17+
:linenos:

examples/mlx90393_temperature.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SPDX-FileCopyrightText: 2023 Jose D. Montoya
2+
# SPDX-License-Identifier: MIT
3+
4+
import time
5+
import board
6+
import adafruit_mlx90393
7+
8+
i2c = board.I2C() # uses board.SCL and board.SDA
9+
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
10+
SENSOR = adafruit_mlx90393.MLX90393(i2c, gain=adafruit_mlx90393.GAIN_1X)
11+
12+
13+
while True:
14+
temp = SENSOR.temperature
15+
16+
print("Temperature: {} °C".format(temp))
17+
18+
# Display the status field if an error occurred, etc.
19+
if SENSOR.last_status > adafruit_mlx90393.STATUS_OK:
20+
SENSOR.display_status()
21+
time.sleep(1.0)

0 commit comments

Comments
 (0)