Skip to content

Commit b7b3c8a

Browse files
committed
Adding LC/MAX measurement code.
1 parent c88c68c commit b7b3c8a

File tree

1 file changed

+40
-0
lines changed
  • CircuitPython_Templates/power_management_measuring_battery

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 Kattni Rembor for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
import time
6+
import board
7+
from adafruit_max1704x import MAX17048
8+
from adafruit_lc709203f import LC709203F, PackSize
9+
10+
#
11+
i2c = board.I2C()
12+
while not i2c.try_lock():
13+
pass
14+
i2c_address_list = i2c.scan()
15+
i2c.unlock()
16+
17+
device = None
18+
19+
if 0x0b in i2c_address_list:
20+
lc709203 = LC709203F(board.I2C())
21+
# Update to match the mAh of your battery for more accurate readings.
22+
# Can be MAH100, MAH200, MAH400, MAH500, MAH1000, MAH2000, MAH3000.
23+
# Choose the closest match. Include "PackSize." before it, as shown.
24+
lc709203.pack_size = PackSize.MAH400
25+
26+
device = lc709203
27+
28+
elif 0x36 in i2c_address_list:
29+
max17048 = MAX17048(board.I2C())
30+
31+
device = max17048
32+
33+
else:
34+
raise Exception("Battery monitor not found.")
35+
36+
while device:
37+
print(f"Battery voltage: {device.cell_voltage:.2f} Volts")
38+
print(f"Battery percentage: {device.cell_percent:.1f} %")
39+
print("")
40+
time.sleep(1)

0 commit comments

Comments
 (0)