Simple test
Ensure your device works with this simple test.
examples/tmp117_simpletest.py
1# SPDX-FileCopyrightText: 2020 Bryan Siepert, written for Adafruit Industries
2#
3# SPDX-License-Identifier: Unlicense
4import time
5
6import board
7
8import adafruit_tmp117
9
10# First try the STEMMA_I2C on Feathers and QtPy among others
11if hasattr(board, "STEMMA_I2C"):
12 i2c = board.STEMMA_I2C()
13else:
14 i2c = board.I2C() # uses board.SCL and board.SDA
15
16tmp117 = adafruit_tmp117.TMP117(i2c)
17
18
19def _c_to_f(tempc):
20 return tempc * 9 / 5 + 32
21
22
23while True:
24 tempc = tmp117.temperature
25 print(f"Temperature: {tempc:.2f} degrees C {_c_to_f(tempc):.2f} degrees F")
26 time.sleep(1)
Temperature limits and alerts
Set high and low temperature limits and be alerted when they are surpassed.
examples/tmp117_limits_test.py
1# SPDX-FileCopyrightText: 2020 Bryan Siepert, written for Adafruit Industries
2#
3# SPDX-License-Identifier: Unlicense
4import time
5
6import board
7
8from adafruit_tmp117 import TMP117, AlertMode
9
10# First try the STEMMA_I2C on Feathers and QtPy among others
11if hasattr(board, "STEMMA_I2C"):
12 i2c = board.STEMMA_I2C()
13else:
14 i2c = board.I2C() # uses board.SCL and board.SDA
15
16tmp117 = TMP117(i2c)
17
18tmp117.high_limit = 25
19tmp117.low_limit = 10
20
21print("\nHigh limit", tmp117.high_limit)
22print("Low limit", tmp117.low_limit)
23
24# Try changing `alert_mode` to see how it modifies the behavior of the alerts.
25# tmp117.alert_mode = AlertMode.WINDOW #default
26# tmp117.alert_mode = AlertMode.HYSTERESIS
27
28print("Alert mode:", AlertMode.string[tmp117.alert_mode])
29print("\n\n")
30while True:
31 print(f"Temperature: {tmp117.temperature:.2f} degrees C")
32 alert_status = tmp117.alert_status
33 print("High alert:", alert_status.high_alert)
34 print("Low alert:", alert_status.low_alert)
35 print("")
36 time.sleep(1)
Measurement averaging and rate
Adjust the number of samples averaged for every reported temperature, and adjust the time beween new measurement reports
examples/tmp117_rate_and_averaging_test.py
1# SPDX-FileCopyrightText: 2020 Bryan Siepert, written for Adafruit Industries
2#
3# SPDX-License-Identifier: Unlicense
4# pylint:disable=no-member
5
6# This example is best viewed using a serial plotter
7# such as the one built into the Mu editor.
8import time
9
10import board
11
12from adafruit_tmp117 import TMP117, AverageCount, MeasurementDelay
13
14# First try the STEMMA_I2C on Feathers and QtPy among others
15if hasattr(board, "STEMMA_I2C"):
16 i2c = board.STEMMA_I2C()
17else:
18 i2c = board.I2C() # uses board.SCL and board.SDA
19
20tmp117 = TMP117(i2c)
21
22# uncomment different options below to see how it affects the reported temperature
23# tmp117.averaged_measurements = AverageCount.AVERAGE_1X
24# tmp117.averaged_measurements = AverageCount.AVERAGE_8X # Default
25# tmp117.averaged_measurements = AverageCount.AVERAGE_32X
26# tmp117.averaged_measurements = AverageCount.AVERAGE_64X
27
28# tmp117.measurement_delay = MeasurementDelay.DELAY_0_0155_S
29# tmp117.measurement_delay = MeasurementDelay.DELAY_0_125_S
30# tmp117.measurement_delay = MeasurementDelay.DELAY_0_250_S
31# tmp117.measurement_delay = MeasurementDelay.DELAY_0_500_S
32# tmp117.measurement_delay = MeasurementDelay.DELAY_1_S # Default
33# tmp117.measurement_delay = MeasurementDelay.DELAY_4_S
34# tmp117.measurement_delay = MeasurementDelay.DELAY_8_S
35# tmp117.measurement_delay = MeasurementDelay.DELAY_16_S
36
37print(
38 "Number of averaged samples per measurement:",
39 AverageCount.string[tmp117.averaged_measurements],
40)
41print(
42 "Minimum time between measurements:",
43 MeasurementDelay.string[tmp117.measurement_delay],
44 "seconds",
45)
46print("")
47
48while True:
49 print("Temperature:", tmp117.temperature)
50 time.sleep(0.01)
Temperature offset
Set an offset that will be applied to each measurement, to account for measurement biases in the sensor’s environment
examples/tmp117_offset_test.py
1# SPDX-FileCopyrightText: 2020 Bryan Siepert, written for Adafruit Industries
2#
3# SPDX-License-Identifier: Unlicense
4import time
5
6import board
7
8import adafruit_tmp117
9
10# First try the STEMMA_I2C on Feathers and QtPy among others
11if hasattr(board, "STEMMA_I2C"):
12 i2c = board.STEMMA_I2C()
13else:
14 i2c = board.I2C() # uses board.SCL and board.SDA
15
16tmp117 = adafruit_tmp117.TMP117(i2c)
17
18print(f"Temperature without offset: {tmp117.temperature:.2f} degrees C")
19tmp117.temperature_offset = 10.0
20time.sleep(0.5) # Let settle
21while True:
22 print(f"Temperature w/ offset: {tmp117.temperature:.2f} degrees C")
23 time.sleep(1)
Single Measurement Test
Take different sample number and average to give a single temperature measure
examples/tmp117_single_measurement_test.py
1# SPDX-FileCopyrightText: 2020 Bryan Siepert, written for Adafruit Industries
2#
3# SPDX-License-Identifier: Unlicense
4import board
5
6from adafruit_tmp117 import TMP117, AverageCount
7
8# First try the STEMMA_I2C on Feathers and QtPy among others
9if hasattr(board, "STEMMA_I2C"):
10 i2c = board.STEMMA_I2C()
11else:
12 i2c = board.I2C() # uses board.SCL and board.SDA
13
14tmp117 = TMP117(i2c)
15
16# uncomment different options below to see how it affects the reported temperature
17# and measurement time
18
19# tmp117.averaged_measurements = AverageCount.AVERAGE_1X
20# tmp117.averaged_measurements = AverageCount.AVERAGE_8X # Default
21# tmp117.averaged_measurements = AverageCount.AVERAGE_32X
22# tmp117.averaged_measurements = AverageCount.AVERAGE_64X
23
24print(
25 "Number of averaged samples per measurement:",
26 AverageCount.string[tmp117.averaged_measurements],
27)
28print(
29 "Reads should take approximately",
30 AverageCount.string[tmp117.averaged_measurements] * 0.0155,
31 "seconds",
32)
33
34while True:
35 print(f"Single measurement: {tmp117.take_single_measurement():.2f} degrees C")
36 # time.sleep(1)
DisplayIO Simpletest
This is a simple test for boards with built-in display.
examples/tmp117_displayio_simpletest.py
1# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
2# SPDX-FileCopyrightText: 2024 Jose D. Montoya
3#
4# SPDX-License-Identifier: MIT
5
6import time
7
8import board
9from adafruit_display_text.bitmap_label import Label
10from displayio import Group
11from terminalio import FONT
12
13import adafruit_tmp117
14
15# Simple demo of using the built-in display.
16# create a main_group to hold anything we want to show on the display.
17main_group = Group()
18
19# First try the STEMMA_I2C on Feathers and QtPy among others
20if hasattr(board, "STEMMA_I2C"):
21 i2c = board.STEMMA_I2C()
22else:
23 i2c = board.I2C() # uses board.SCL and board.SDA
24
25tmp117 = adafruit_tmp117.TMP117(i2c)
26
27# Create Label(s) to show the readings. If you have a very small
28# display you may need to change to scale=1.
29display_output_label = Label(FONT, text="", scale=2)
30
31# place the label(s) in the middle of the screen with anchored positioning
32display_output_label.anchor_point = (0, 0)
33display_output_label.anchored_position = (
34 4,
35 board.DISPLAY.height // 2 - 60,
36)
37
38# add the label(s) to the main_group
39main_group.append(display_output_label)
40
41# set the main_group as the root_group of the built-in DISPLAY
42board.DISPLAY.root_group = main_group
43
44# begin main loop
45while True:
46 # update the text of the label(s) to show the sensor readings
47 display_output_label.text = f"Temperature: {tmp117.temperature:.1f} C"
48 # wait for a bit
49 time.sleep(0.5)