Your microcontroller board comes with an LC709203 lithium ion polymer (lipoly) battery monitor built right onto the board. The LC709203 is available over I2C.
The LC709203 comes with its own Adafruit CircuitPython library that makes it simple to write code to read data from it. This example will be using, among other things, the Adafruit CircuitPython LC709203F library.
The example simply reads data from the battery monitor and prints it to the serial console. It is designed to show you how to get data from the battery monitor.
The LC709203 battery monitor (highlighted in red) is located towards the top of the board, near the center, below the USB and 13 pin labels on the silk. Its I2C address is 0x0B. (0xb, which is returned by I2C scan, is the same as 0x0B, which simply has leading zeros.)
LC709203 Simple Data Example
To run this example, you need to first install the LC709203F library into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.
Your CIRCUITPY/lib folder should contain at least the following folder and file:
- adafruit_register/
- adafruit_lc709203f.mpy
# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries # SPDX-License-Identifier: MIT """ CircuitPython Simple Example for LC709203 Sensor """ import time import board from adafruit_lc709203f import LC709203F, PackSize # Create sensor object, using the board's default I2C bus. i2c = board.I2C() # uses board.SCL and board.SDA # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller battery_monitor = LC709203F(i2c) # Update to match the mAh of your battery for more accurate readings. # Can be MAH100, MAH200, MAH400, MAH500, MAH1000, MAH2000, MAH3000. # Choose the closest match. Include "PackSize." before it, as shown. battery_monitor.pack_size = PackSize.MAH400 while True: print("Battery Percent: {:.2f} %".format(battery_monitor.cell_percent)) print("Battery Voltage: {:.2f} V".format(battery_monitor.cell_voltage)) time.sleep(2)
Now, connect to the serial console to see the battery data printed out!
That's all there is to reading the LC709203 data using CircuitPython!
For more details, check out the guide for the LC709203.
Text editor powered by tinymce.