|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2022 ladyada for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Unlicense |
| 4 | + |
| 5 | +import time |
| 6 | +import board |
| 7 | +import adafruit_max1704x |
| 8 | + |
| 9 | +i2c = board.I2C() # uses board.SCL and board.SDA |
| 10 | +max17 = adafruit_max1704x.MAX17048(i2c) |
| 11 | + |
| 12 | +print("Found MAX1704x with chip version", hex(max17.chip_version), "and id", hex(max17.chip_id)) |
| 13 | + |
| 14 | +# Quick starting allows an instant 'auto-calibration' of the battery. However, its a bad idea |
| 15 | +# to do this right when the battery is first plugged in or if there's a lot of load on the battery |
| 16 | +# so uncomment only if you're sure you want to 'reset' the chips charge calculator. |
| 17 | +# print("Quick starting") |
| 18 | +max17.quick_start = True |
| 19 | + |
| 20 | +# The reset voltage is what the chip considers 'battery has been removed and replaced' |
| 21 | +# The default is 3.0 Volts but you can change it here: |
| 22 | +#max17.reset_voltage = 2.5 |
| 23 | +print("MAX1704x reset voltage = %0.1f V" % max17.reset_voltage) |
| 24 | + |
| 25 | +# The analog comparator is used to detect the rest voltage, if you don't think the battery |
| 26 | +# will ever be removed this can reduce current usage (see datasheet on VRESET.Dis) |
| 27 | +print("Analog comparator is ", end="") |
| 28 | +if max17.comparator_disabled: |
| 29 | + print("disabled") |
| 30 | +else: |
| 31 | + print("enabled") |
| 32 | + |
| 33 | +# Hibernation mode reduces how often the ADC is read, for power reduction. There is an automatic |
| 34 | +# enter/exit mode but you can also customize the activity threshold both as voltage and charge rate |
| 35 | +#max17.activity_threshold = 0.15 |
| 36 | +print("MAX1704x activity threshold = %0.2f V" % max17.activity_threshold) |
| 37 | + |
| 38 | +#max17.hibernation_threshold = 5 |
| 39 | +print("MAX1704x hibernation threshold = %0.2f %%" % max17.hibernation_threshold) |
| 40 | + |
| 41 | +# You can also 'force' hibernation mode! |
| 42 | +#max17.hibernate() |
| 43 | +# ...or force it to wake up! |
| 44 | +#max17.wake() |
| 45 | + |
| 46 | +# The alert pin can be used to detect when the voltage of the battery goes below or |
| 47 | +# above a voltage, you can also query the alert in the loop. |
| 48 | +max17.voltage_alert_min = 3.5 |
| 49 | +print("Voltage alert minimum = %0.2f V" % max17.voltage_alert_min) |
| 50 | +max17.voltage_alert_max = 4.1 |
| 51 | +print("Voltage alert maximum = %0.2f V" % max17.voltage_alert_max) |
| 52 | + |
| 53 | +print("") |
| 54 | +while True: |
| 55 | + print(f'Battery voltage: {max17.cell_voltage:.2f} Volts') |
| 56 | + print(f'Battery state : {max17.cell_percent:.1f} %') |
| 57 | + |
| 58 | + # we can check if we're hibernating or not |
| 59 | + if max17.hibernating: |
| 60 | + print("Hibernating!") |
| 61 | + |
| 62 | + if max17.active_alert: |
| 63 | + print("Alert!") |
| 64 | + if max17.reset_alert: |
| 65 | + print(" Reset indicator") |
| 66 | + max17.reset_alert = False # clear the alert |
| 67 | + |
| 68 | + if max17.voltage_high_alert: |
| 69 | + print(" Voltage high") |
| 70 | + max17.voltage_high_alert = False # clear the alert |
| 71 | + |
| 72 | + if max17.voltage_low_alert: |
| 73 | + print(" Voltage low") |
| 74 | + max17.voltage_low_alert = False # clear the alert |
| 75 | + |
| 76 | + if max17.voltage_reset_alert: |
| 77 | + print(" Voltage reset") |
| 78 | + max17.voltage_reset_alert = False # clear the alert |
| 79 | + |
| 80 | + if max17.SOC_low_alert: |
| 81 | + print(" Charge low") |
| 82 | + max17.SOC_low_alert = False # clear the alert |
| 83 | + |
| 84 | + if max17.SOC_change_alert: |
| 85 | + print(" Charge changed") |
| 86 | + max17.SOC_change_alert = False # clear the alert |
| 87 | + print("") |
| 88 | + time.sleep(1) |
0 commit comments