Skip to content

Add thermistor support #13

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

Merged
merged 3 commits into from
Jun 22, 2021
Merged
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
37 changes: 37 additions & 0 deletions adafruit_lc709203f.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
LC709203F_CMD_INITRSOC = const(0x07)
LC709203F_CMD_CELLVOLTAGE = const(0x09)
LC709203F_CMD_CELLITE = const(0x0F)
LC709203F_CMD_CELLTEMPERATURE = const(0x08)
LC709203F_CMD_THERMISTORB = const(0x06)
LC709203F_CMD_STATUSBIT = const(0x16)


class CV:
Expand Down Expand Up @@ -121,6 +124,18 @@ def cell_percent(self):
"""Returns percentage of cell capacity"""
return self._read_word(LC709203F_CMD_CELLITE) / 10

@property
def cell_temperature(self):
"""Returns the temperature of the cell"""
return self._read_word(LC709203F_CMD_CELLTEMPERATURE) / 10 - 273.15

@cell_temperature.setter
def cell_temperature(self, value):
"""Sets the temperature in the LC709203F"""
if self.thermistor_enable:
raise AttributeError("temperature can only be set in i2c mode")
self._write_word(LC709203F_CMD_CELLTEMPERATURE, int(value + 273.15) * 10)

@property
def ic_version(self):
"""Returns read-only chip version"""
Expand Down Expand Up @@ -159,6 +174,28 @@ def pack_size(self, size):
raise AttributeError("pack_size must be a PackSize")
self._write_word(LC709203F_CMD_APA, size)

@property
def thermistor_bconstant(self):
"""Returns the thermistor B-constant"""
return self._read_word(LC709203F_CMD_THERMISTORB)

@thermistor_bconstant.setter
def thermistor_bconstant(self, bconstant):
"""Sets the thermistor B-constant"""
self._write_word(LC709203F_CMD_THERMISTORB, bconstant)

@property
def thermistor_enable(self):
"""Returns the current temperature source"""
return self._read_word(LC709203F_CMD_STATUSBIT)

@thermistor_enable.setter
def thermistor_enable(self, status):
"""Sets the temperature source to Tsense"""
if not status in (True, False):
raise AttributeError("thermistor_enable must be True or False")
self._write_word(LC709203F_CMD_STATUSBIT, status)

# pylint: disable=no-self-use
def _generate_crc(self, data):
"""8-bit CRC algorithm for checking data"""
Expand Down
21 changes: 21 additions & 0 deletions examples/lc709203f_thermistortest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# SPDX-FileCopyrightText: 2021 Daniel Griswold
#
# SPDX-License-Identifier: MIT

import time
import board
from adafruit_lc709203f import LC709203F

print("LC709203F thermistor test")
print("Make sure a thermistor is connected to the board!")

sensor = LC709203F(board.I2C())

# check your NTC thermistor datasheet for the appropriate B-Constant
sensor.thermistor_bconstant = 3950
sensor.thermistor_enable = True

print("IC version:", hex(sensor.ic_version))
while True:
print("Cell Temperature: %0.2f C" % (sensor.cell_temperature))
time.sleep(1)