Skip to content

Extended library for TMP116, TMP117, TMP119 #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
22 changes: 14 additions & 8 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ Introduction
:target: https://github.com/psf/black
:alt: Code Style: Black

CircuitPython library for the TI TMP117 Temperature sensor
CircuitPython library for the TI TMP116, TMP117, TMP119 Temperature sensor

It is forked from Adafruit_CircuitPython_TMP117, updated and extended to use it with TMP116 and TMP119

Comment on lines +22 to +23
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
It is forked from Adafruit_CircuitPython_TMP117, updated and extended to use it with TMP116 and TMP119
``

All those sensor have common register map

WARNING: Library may not run on some boards with less RAM such as boards using the SAMD21

Expand All @@ -41,13 +45,13 @@ PyPI <https://pypi.org/project/adafruit-circuitpython-tmp117/>`_. To install for

.. code-block:: shell

pip3 install adafruit-circuitpython-tmp117
pip3 install git+https://github.com/ami3go/Adafruit_CircuitPython_TMP11X.git
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you mean to do this.

Suggested change
pip3 install git+https://github.com/ami3go/Adafruit_CircuitPython_TMP11X.git
pip3 install adafruit-circuitpython-tmp11x

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello thanks for review, and your feedback.
That was my worry too, that you sell only one part and there is no logic to merge updated lib, because it could be confusing for your customers.
Thank for idea to integrate all 3 device into 1 class instead of 3 different.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding readme . i will correct it if you going to merge it.
just for now it would be confusing if someone going to install original lib and it will not work with tmp116


To install system-wide (this may be required in some cases):

.. code-block:: shell

sudo pip3 install adafruit-circuitpython-tmp117
sudo pip3 install git+https://github.com/ami3go/Adafruit_CircuitPython_TMP11X.git
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same


To install in a virtual environment in your current project:

Expand All @@ -56,7 +60,7 @@ To install in a virtual environment in your current project:
mkdir project-name && cd project-name
python3 -m venv .venv
source .venv/bin/activate
pip3 install adafruit-circuitpython-tmp117
pip3 install git+https://github.com/ami3go/Adafruit_CircuitPython_TMP11X.git
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same


Usage Example
=============
Expand All @@ -65,13 +69,15 @@ Usage Example

import time
import board
import adafruit_tmp117
import adafruit_tmp11X
from adafruit_tmp11X import TMP117, TMP116, TMP119, AverageCount, MeasurementMode, MeasurementDelay

i2c = board.I2C() # uses board.SCL and board.SDA
tmp117 = adafruit_tmp117.TMP117(i2c)

t1 = TMP116(i2c_bus=i2c, address=0x49)
# t2 = TMP117(i2c_bus=i2c, address=0x49)
# t2 = TMP119(i2c_bus=i2c, address=0x49)
Comment on lines +76 to +78
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the I2C addresses all the same? If so don't bother to specify the address here, since the default is fine and makes usage easier.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears the default I2C address is the same. It's just that it can be varied via pin settings.

while True:
print("Temperature: %.2f degrees C"%tmp117.temperature)
print("Temperature:", t1.temperature)
time.sleep(1)

Documentation
Expand Down
71 changes: 55 additions & 16 deletions adafruit_tmp117.py → adafruit_tmp11X.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# SPDX-FileCopyrightText: Copyright (c) 2020 Bryan Siepert for Adafruit Industries

Check failure on line 1 in adafruit_tmp11X.py

View workflow job for this annotation

GitHub Actions / test

reformatted

Check failure on line 1 in adafruit_tmp11X.py

View workflow job for this annotation

GitHub Actions / test

Module name "adafruit_tmp11X" doesn't conform to '(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$' pattern
#
# SPDX-License-Identifier: MIT
"""
`adafruit_tmp117`
`adafruit_tmp11X`
Copy link
Contributor

@dhalbert dhalbert May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use lower-case x for filename

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and TMP11X (upper case) for new classname.

================================================================================

CircuitPython library for the TI TMP117 Temperature sensor
CircuitPython library for the TI TMP116,TMP117,TMP119 Temperature sensor

* Author(s): Bryan Siepert, Ian Grant

Expand Down Expand Up @@ -53,7 +53,7 @@
pass

__version__ = "0.0.0+auto.0"
__repo__ = "https:#github.com/adafruit/Adafruit_CircuitPython_TMP117.git"
__repo__ = "https://github.com/ami3go/Adafruit_CircuitPython_TMP11X.git"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
__repo__ = "https://github.com/ami3go/Adafruit_CircuitPython_TMP11X.git"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TMP11x.git"



_I2C_ADDR = 0x48 # default I2C Address
Expand All @@ -67,7 +67,10 @@
_TEMP_OFFSET = const(0x07)
_EEPROM3 = const(0x08)
_DEVICE_ID = const(0x0F)
_DEVICE_ID_VALUE = 0x0117
_TMP117_DEVICE_ID_VALUE = const(0x0117)
_TMP116_DEVICE_ID_VALUE = const(0x1116)
_TMP119_DEVICE_ID_VALUE = const(0x0117)

_TMP117_RESOLUTION = (
0.0078125 # Resolution of the device, found on (page 1 of datasheet)
)
Expand Down Expand Up @@ -113,10 +116,12 @@
"""Validate that a given value is a member"""
return value in cls.string


class AverageCount(CV):
"""Options for `averaged_measurements`"""

AVERAGE_1X = None
AVERAGE_8X = None
AVERAGE_32X = None
AVERAGE_64X = None
Comment on lines +121 to +124
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These make the library take more RAM, which is a problem on small boards. Don't include these even if it is more convenient for IDE's.

Suggested change
AVERAGE_1X = None
AVERAGE_8X = None
AVERAGE_32X = None
AVERAGE_64X = None


AverageCount.add_values(
(
Expand All @@ -128,9 +133,17 @@
)



class MeasurementDelay(CV):
"""Options for `measurement_delay`"""

DELAY_0_0015_S = None
DELAY_0_125_S = None
DELAY_0_250_S = None
DELAY_0_500_S = None
DELAY_1_S = None
DELAY_4_S = None
DELAY_8_S = None
DELAY_16_S = None
Comment on lines +139 to +146
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DELAY_0_0015_S = None
DELAY_0_125_S = None
DELAY_0_250_S = None
DELAY_0_500_S = None
DELAY_1_S = None
DELAY_4_S = None
DELAY_8_S = None
DELAY_16_S = None


MeasurementDelay.add_values(
(
Expand All @@ -148,16 +161,20 @@

class AlertMode(CV):
"""Options for `alert_mode`. See `alert_mode` for more information."""


WINDOW = None
HYSTERESIS = None
Comment on lines +164 to +165
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
WINDOW = None
HYSTERESIS = None

AlertMode.add_values(
(("WINDOW", 0, "Window", None), ("HYSTERESIS", 1, "Hysteresis", None))
(
("WINDOW", 0, "Window", None),
("HYSTERESIS", 1, "Hysteresis", None))
)


class MeasurementMode(CV):
"""Options for `measurement_mode`. See `measurement_mode` for more information."""

CONTINUOUS = None
ONE_SHOT = None
SHUTDOWN = None
Comment on lines +175 to +177
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
CONTINUOUS = None
ONE_SHOT = None
SHUTDOWN = None


MeasurementMode.add_values(
(
Expand All @@ -170,7 +187,6 @@

class TMP117:
"""Library for the TI TMP117 high-accuracy temperature sensor"""

_part_id = ROUnaryStruct(_DEVICE_ID, ">H")
_raw_temperature = ROUnaryStruct(_TEMP_RESULT, ">h")
_raw_high_limit = UnaryStruct(_T_HIGH_LIMIT, ">h")
Expand All @@ -192,18 +208,22 @@

def __init__(self, i2c_bus: I2C, address: int = _I2C_ADDR):
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
if self._part_id != _DEVICE_ID_VALUE:
raise AttributeError("Cannot find a TMP117")
self._check_dev_id()
# currently set when `alert_status` is read, but not exposed
self.reset()
self.initialize()

# thsi methods is unique for each device
Copy link
Contributor

@dhalbert dhalbert May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# thsi methods is unique for each device
# this method is unique for each device

def _check_dev_id(self):
id = _TMP117_DEVICE_ID_VALUE
if self._part_id != id:
raise AttributeError("Cannot find a TMP117")
def reset(self):
"""Reset the sensor to its unconfigured power-on state"""
self._soft_reset = True

def initialize(self):
"""Configure the sensor with sensible defaults. `initialize` is primarily provided to be

Check failure on line 226 in adafruit_tmp11X.py

View workflow job for this annotation

GitHub Actions / test

Redefining built-in 'id'
called after `reset`, however it can also be used to easily set the sensor to a known
configuration"""
# Datasheet specifies that reset will finish in 2ms however by default the first
Expand All @@ -218,6 +238,10 @@

return self._read_temperature()

@property
def temperature_updated(self):
return self._wait_for_measurement()

@property
def temperature_offset(self):
"""User defined temperature offset to be added to measurements from `temperature`
Expand Down Expand Up @@ -520,9 +544,11 @@
def _set_mode_and_wait_for_measurement(self, mode: int) -> float:
self._mode = mode
# poll for data ready
return self._wait_for_measurement()

def _wait_for_measurement(self)-> float:
while not self._read_status()[2]:
time.sleep(0.001)

return self._read_temperature()

# eeprom write enable to set defaults for limits and config
Expand All @@ -539,4 +565,17 @@
return (high_alert, low_alert, data_ready)

def _read_temperature(self) -> float:
return self._raw_temperature * _TMP117_RESOLUTION
return round(self._raw_temperature * _TMP117_RESOLUTION, 3)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't round here, because the extra digits are still interesting when summing or averaging. Leave it up to the caller to round as needed.

Suggested change
return round(self._raw_temperature * _TMP117_RESOLUTION, 3)
return self._raw_temperature * _TMP117_RESOLUTION```

Also this specifically mentions the TMP117 resolution: does the value change? Should it be TMP11X_RESOLUTION?



class TMP119(TMP117):
def _check_dev_id(self):
id = _TMP119_DEVICE_ID_VALUE
if self._part_id != id:
raise AttributeError("Cannot find a TMP119")

class TMP116(TMP117):
def _check_dev_id(self):
id = _TMP116_DEVICE_ID_VALUE
if self._part_id != id:
raise AttributeError("Cannot find a TMP116")
Binary file added docs/TMP116-TMP117-TMP119.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions examples/Sensors_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# import usb

Check failure on line 1 in examples/Sensors_array.py

View workflow job for this annotation

GitHub Actions / test

reformatted
# import usb.util
# dev = usb.core.find(idVendor=0x0403, idProduct=0x6014)
# print(dev)
import os
os.environ['BLINKA_FT232H'] = "1"
import board
from adafruit_tmp11X import TMP117, TMP116, TMP119, AverageCount, MeasurementMode, MeasurementDelay
import time
import datetime

deg_sybm = u'\N{DEGREE SIGN}'

i2c = board.I2C() # uses board.SCL and board.SDA
t1 = TMP116(i2c_bus=i2c, address=0x49)
# t2 = TMP116(i2c_bus=i2c, address=0x47)
# t3 = TMP116(i2c_bus=i2c, address=0x48)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, they do have different I2C addresses.



# add to this array multiple sensors.
# address limitation for single device allow to add up to 4 devises
# However you may add different I2C busses to array
sensors = [t1,]
print(AverageCount.AVERAGE_32X)

for sensor in sensors:
sensor.measurement_mode = MeasurementMode.CONTINUOUS
sensor.averaged_measurements = AverageCount.AVERAGE_32X
sensor.measurement_delay = MeasurementDelay.DELAY_0_0015_S


def get_temperature():
temp = []
for sensor in sensors:
temp.append(sensor.temperature_updated)
return temp


while True:
txt = f"{datetime.datetime.now()},Temperature:[{deg_sybm}C]: {get_temperature()} "
print(txt)
# time.sleep(1)
2 changes: 1 addition & 1 deletion examples/tmp117_limits_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SPDX-License-Identifier: Unlicense
import time
import board
from adafruit_tmp117 import TMP117, AlertMode
from adafruit_tmp11X import TMP117, AlertMode

i2c = board.I2C() # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
Expand Down
4 changes: 2 additions & 2 deletions examples/tmp117_offset_test.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# SPDX-FileCopyrightText: 2020 Bryan Siepert, written for Adafruit Industries

Check failure on line 1 in examples/tmp117_offset_test.py

View workflow job for this annotation

GitHub Actions / test

reformatted
#
# SPDX-License-Identifier: Unlicense
import time
import board
import adafruit_tmp117
from adafruit_tmp11X import TMP117

i2c = board.I2C() # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller

tmp117 = adafruit_tmp117.TMP117(i2c)
tmp117 = TMP117(i2c)

print("Temperature without offset: %.2f degrees C" % tmp117.temperature)
tmp117.temperature_offset = 10.0
Expand Down
6 changes: 3 additions & 3 deletions examples/tmp117_rate_and_averaging_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
# such as the one built into the Mu editor.
import time
import board
from adafruit_tmp117 import TMP117, AverageCount, MeasurementDelay
from adafruit_tmp11X import TMP117, AverageCount, MeasurementDelay

i2c = board.I2C() # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
tmp117 = TMP117(i2c)

# uncomment different options below to see how it affects the reported temperature
# tmp117.averaged_measurements = AverageCount.AVERAGE_1X
# tmp117.averaged_measurements = AverageCount.AVERAGE_8X
tmp117.averaged_measurements = AverageCount.AVERAGE_8X
# tmp117.averaged_measurements = AverageCount.AVERAGE_32X
# tmp117.averaged_measurements = AverageCount.AVERAGE_64X

# tmp117.measurement_delay = MeasurementDelay.DELAY_0_0015_S
# tmp117.measurement_delay = MeasurementDelay.DELAY_0_125_S
tmp117.measurement_delay = MeasurementDelay.DELAY_0_125_S
# tmp117.measurement_delay = MeasurementDelay.DELAY_0_250_S
# tmp117.measurement_delay = MeasurementDelay.DELAY_0_500_S
# tmp117.measurement_delay = MeasurementDelay.DELAY_1_S
Expand Down
4 changes: 2 additions & 2 deletions examples/tmp117_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
# SPDX-License-Identifier: Unlicense
import time
import board
import adafruit_tmp117
from adafruit_tmp11X import TMP117

i2c = board.I2C() # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
tmp117 = adafruit_tmp117.TMP117(i2c)
tmp117 = TMP117(i2c)

while True:
print("Temperature: %.2f degrees C" % tmp117.temperature)
Expand Down
21 changes: 12 additions & 9 deletions examples/tmp117_single_measurement_test.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
# SPDX-FileCopyrightText: 2020 Bryan Siepert, written for Adafruit Industries

Check failure on line 1 in examples/tmp117_single_measurement_test.py

View workflow job for this annotation

GitHub Actions / test

reformatted
#
# SPDX-License-Identifier: Unlicense
import os
os.environ['BLINKA_FT232H'] = "1"
import board
from adafruit_tmp117 import TMP117, AverageCount

from adafruit_tmp11X import TMP116, AverageCount

i2c = board.I2C() # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
tmp117 = TMP117(i2c)
t1 = TMP116(i2c_bus=i2c, address=0x49)

# uncomment different options below to see how it affects the reported temperature
# and measurement time

# tmp117.averaged_measurements = AverageCount.AVERAGE_1X
# tmp117.averaged_measurements = AverageCount.AVERAGE_8X
# tmp117.averaged_measurements = AverageCount.AVERAGE_32X
# tmp117.averaged_measurements = AverageCount.AVERAGE_64X
# t1.averaged_measurements = AverageCount.AVERAGE_1X
# t1.averaged_measurements = AverageCount.AVERAGE_8X
t1.averaged_measurements = AverageCount.AVERAGE_32X
# t1.averaged_measurements = AverageCount.AVERAGE_64X

print(
"Number of averaged samples per measurement:",
AverageCount.string[tmp117.averaged_measurements],
AverageCount.string[t1.averaged_measurements],
)
print(
"Reads should take approximately",
AverageCount.string[tmp117.averaged_measurements] * 0.0155,
AverageCount.string[t1.averaged_measurements] * 0.0155,
"seconds",
)

while True:
print("Single measurement: %.2f degrees C" % tmp117.take_single_measurement())
print("Single measurement: %.2f degrees C" % t1.take_single_measurement())
# time.sleep(1)
Loading