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:
i2c : I2C

The I2C bus the TMP117 is connected to.

address : int

The I2C device address. Defaults to 0x48

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 sensor

from machine import Pin, I2C
import micropython_tmp117 import tmp117

Once this is done you can define your machine.I2C object and define your sensor object

i2c = I2C(sda=Pin(8), scl=Pin(9))
tmp117 = tmp117.TMP117(i2c)

Now you have access to the temperature attribute

temp = tmp117.temperature
property alert_mode : str

Sets the behavior of the low_limit, high_limit, and alert_status properties.

When set to ALERT_WINDOW, the high_limit property will unset when the measured temperature goes below high_limit. Similarly low_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, the high_limit property will be set to False when the measured temperature goes below low_limit. In this mode, the low_limit property of alert_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 with averaging_measurements set to AVERAGE_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 the alert_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 the alert_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 measurement

tmp117.SHUTDOWN_MODE

Take a single measurement with the current number of averaging_measurements and switch to SHUTDOWN when finished. temperature will return the new measurement until measurement_mode is set to CONTINUOUS or ONE_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 new measurement_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.

validate_value(value: int) int[source]

Validates for values to be in the range of -256 and 255, then return the value divided by the _TMP117_RESOLUTION