Skip to content

Commit 5882d2b

Browse files
committed
adding temperature reading
1 parent ba029a6 commit 5882d2b

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

adafruit_mlx90393.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -520,14 +520,17 @@ def magnetic(self) -> Tuple[float, float, float]:
520520

521521
return x, y, z
522522

523-
def read_temp(self):
523+
@property
524+
def temperature(self) -> float:
524525
"""
525526
Reads a single temperature sample from the magnetometer.
527+
Temperature value in Celsius
526528
"""
527-
# Set conversion delay based on filter and oversampling
529+
# Read the temperature reference from register 0x24
528530
treference = self.read_reg(0x24)
529531

530-
# from maximum time of temperature conversion on the datasheet section 12. 1603 us
532+
# Value taken from maximum time of temperature conversion on the datasheet section 12.
533+
# maximum time for temperature conversion = 1603 us
531534
delay = 0.1
532535

533536
# Set the device to single measurement mode
@@ -544,6 +547,6 @@ def read_temp(self):
544547
# from https://www.melexis.com/-/media/files/documents/
545548
# application-notes/mlx90393-temperature-compensation-application-note-melexis.pdf
546549
tvalue = struct.unpack(">H", data[1:3])[0]
547-
temperature = 35 + ((tvalue - treference) / 45.2) # See previous link
550+
# See previous link for conversion formula
548551

549-
return temperature
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)