MicroPython TMP117 Library¶
tmp117
¶
MicroPython Driver for the TMP117 temperature sensor
Author(s): Jose D. Montoya
Implementation Notes¶
Software and Dependencies:
This library depends on Micropython
- class micropython_tmp117.tmp117.AlertStatus(high_alert, low_alert)¶
Create new instance of AlertStatus(high_alert, low_alert)
- high_alert¶
Alias for field number 0
- low_alert¶
Alias for field number 1
-
class micropython_tmp117.tmp117.TMP117(i2c, address=
0x48
)[source]¶ Main class for the Sensor
- Parameters:¶
- Raises:¶
RuntimeError – if the sensor is not found
Quickstart: Importing and using the device
Here is an example of using the
TMP117
class. First you will need to import the libraries to use the sensorfrom machine import Pin, I2C import micropython_tmp117 import tmp117
Once this is done you can define your
machine.I2C
object and define your sensor objecti2c = I2C(sda=Pin(8), scl=Pin(9)) tmp117 = tmp117.TMP117(i2c)
Now you have access to the
temperature
attributetemp = tmp117.temperature
- property alert_mode : str¶
Sets the behavior of the
low_limit
,high_limit
, andalert_status
properties.When set to
ALERT_WINDOW
, thehigh_limit
property will unset when the measured temperature goes belowhigh_limit
. Similarlylow_limit
will be True or False depending on if the measured temperature is below (False
) or above(True
)low_limit
.When set to
ALERT_HYSTERESIS
, thehigh_limit
property will be set toFalse
when the measured temperature goes belowlow_limit
. In this mode, thelow_limit
property ofalert_status
will not be set.The default is
ALERT_WINDOW
Mode
Value
tmp117.ALERT_WINDOW
0b0
tmp117.ALERT_HYSTERESIS
0b1
- property alert_status : AlertStatus¶
The current triggered status of the high and low temperature alerts as a AlertStatus named tuple with attributes for the triggered status of each alert.
from machine import Pin, I2C import micropython_tmp117.tmp117 as tmp117 i2c = I2C(sda=Pin(8), scl=Pin(9)) # Correct I2C pins for UM FeatherS2 tmp = tmp117.TMP117(i2c) tmp.low_limit = 20 tmp.high_limit = 23 print("Alert mode:", tmp.alert_mode) print("High limit", tmp.high_limit) print("Low limit", tmp.low_limit) while True: print("Temperature: %.2f degrees C" % tmp.temperature) alert_status = tmp.alert_status if alert_status.high_alert: print("Temperature above high set limit!") if alert_status.low_alert: print("Temperature below low set limit!") print("Low alert:", alert_status.low_alert) time.sleep(1)
- property averaging_measurements : str¶
Users can configure the device to report the average of multiple temperature conversions with the
averaging_measurements
to reduce noise in the conversion results. When the TMP117 is configured to perform averaging withaveraging_measurements
set toAVERAGE_8X
, the device executes the configured number of conversions to eight. The device accumulates those conversion results and reports the average of all the collected results at the end of the process. The average is an accumulated average and not a running average.Mode
Value
tmp117.AVERAGE_1X
0b00
tmp117.AVERAGE_8X
0b01
tmp117.AVERAGE_32X
0b10
tmp117.AVERAGE_64X
0b11
- property high_limit : float¶
The high temperature limit in Celsius. When the measured temperature exceeds this value, the
high_alert
attribute of thealert_status
property will be True. The range is ±256 °C. Following power-up or a general-call reset, the high-limit register is loaded with the stored value from the EEPROM. The factory default reset value is 192 °C (0x6000)
- property low_limit : float¶
The low temperature limit in Celsius. When the measured temperature goes below this value, the
low_alert
attribute of thealert_status
property will be True. The range is ±256 °C. Following power-up or a general-call reset, the low-limit register is loaded with the stored value from the EEPROM. The factory default reset value is -256 °C (0x8000)
- property measurement_mode : str¶
- Sets the measurement mode, specifying the behavior of how often measurements are taken.
measurement_mode
must be one of:
When we use the sensor in One shot mode, the sensor will take the average_measurement value into account. However, this measure is done with the formula (15.5 ms x average_time), so in normal operation average_time will be 8, therefore time for measure is 124 ms. (See datasheet. 7.3.2 Averaging for more information). If we use 64, time will be 15.5 x 65 = 992 ms, the standby time will decrease, but the measure is still under 1 Hz cycle. (See Fig 7.2 on the datasheet)
Mode
Behavior
tmp117.CONTINUOUS_CONVERSION_MODE
Measurements are made at the interval determined by
averaging_measurements
.temperature
returns the most recent measurementtmp117.SHUTDOWN_MODE
Take a single measurement with the current number of
averaging_measurements
and switch toSHUTDOWN
when finished.temperature
will return the new measurement untilmeasurement_mode
is set toCONTINUOUS
orONE_SHOT
is set again.tmp117.ONE_SHOT_MODE
The sensor is put into a low power state and no new measurements are taken.
temperature
will return the last measurement until a newmeasurement_mode
is selected.
- property temperature : float¶
The current measured temperature in Celsius Following a reset, the temperature reads -256 °C until the first conversion, including averaging, is complete. See the Power Up section in the datasheet for more information.
- property temperature_offset : float¶
User defined temperature offset to be added to measurements from
temperature
. In order the see the new change in the temperature we need for the data to be ready. There is a time delay calculated according to current configuration. This is used as a user-defined temperature offset register during system calibration. The offset will be added to the temperature result after linearization. It has a same resolution of 7.8125 m°C and same range of ±256 °C as the temperature result register. If the added result is out of boundary, then the temperature result will show as the maximum or minimum value.