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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
77 changes: 61 additions & 16 deletions adafruit_tmp117.py → adafruit_tmp11X.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
#
# 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 @@ def is_valid(cls, value: int) -> bool:
"""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 AverageCount(CV):
)



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 MeasurementDelay(CV):

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 @@ -166,11 +183,15 @@ class MeasurementMode(CV):
("SHUTDOWN", 1, "Shutdown", None),
)
)
class Symbols:
deg = u'\N{DEGREE SIGN}'
degC = deg + "C"
degK = deg + "K"
degF = deg + "F"


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,12 +213,17 @@ class TMP117:

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
Expand All @@ -218,6 +244,10 @@ def temperature(self):

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 +550,11 @@ def serial_number(self):
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 +571,17 @@ def _read_status(self) -> Tuple[int, int, int]:
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.
19 changes: 19 additions & 0 deletions examples/Scanner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SPDX-FileCopyrightText: 2020 Bryan Siepert, written for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense
import time
import os
os.environ['BLINKA_FT232H'] = "1"
import board
import time
import busio
import datetime
i2c = busio.I2C(board.SCL, board.SDA)

while not i2c.try_lock():
pass

while True:
print(datetime.datetime.now(),"I2C addresses found:", [hex(device_address)
for device_address in i2c.scan()])
time.sleep(2)
78 changes: 78 additions & 0 deletions examples/Sensors_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import os
os.environ['BLINKA_FT232H'] = "1"
import board
from adafruit_tmp11X import TMP117, TMP116, TMP119, AverageCount, MeasurementMode, MeasurementDelay, Symbols
import time
import datetime
import Logging_class
global sensors



i2c = board.I2C() # uses board.SCL and board.SDA
t1 = TMP117(i2c_bus=i2c, address=0x48)
t2 = TMP117(i2c_bus=i2c, address=0x49)
t3 = TMP117(i2c_bus=i2c, address=0x4a)
t4 = TMP117(i2c_bus=i2c, address=0x4b)


# 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, t2, t3, t4]
# 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(ret_as="array"):
global sensors
for i in range(10):
try:
temp_array = []
temp_dict = {}
for sensor in sensors:
temp_array.append(sensor.temperature_updated)
if ret_as == "arrya":
return temp_array
else:
for i, item in enumerate(temp_array):
key = f"T{i}"
temp_dict[key] = item
return temp_dict
except Exception as e:

print(f"query[{i}]: Error: {e}")
time.sleep(5)
i2c = board.I2C() # uses board.SCL and board.SDA
t1 = TMP117(i2c_bus=i2c, address=0x48)
t2 = TMP117(i2c_bus=i2c, address=0x49)
t3 = TMP117(i2c_bus=i2c, address=0x4a)
t4 = TMP117(i2c_bus=i2c, address=0x4b)
sensors = [t1, t2, t3, t4]
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_csv_keys():
global sensors
keys = []
for i in range(len(sensors)):
keys.append(f"T{i}")
return keys

if __name__ == "__main__":
logger = Logging_class.txt_logger()
logger.init("temperature_TMP117")
z = 0
while True:

txt = f"[{z}]{get_temperature('dict')} Temperature:[{Symbols.degC}] "
logger.print_and_log(txt)
time.sleep(1)
z = z + 1
Loading
Loading