It's easy to use the LC709203F and the Adafruit CircuitPython LC709203F module. This library allows you to easily write Python code that provides information about the connected battery including voltage, charge percentage, and temperature.

You can use this sensor with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.

CircuitPython Microcontroller Wiring

Wire up a LC709203F to your board and a battery exactly as shown below. Here's an example of wiring a Feather M4 to the sensor with I2C using STEMMA QT and a solderless breadboard.

  • Connect board VIN (red wire) to Feather 3V
  • Connect board GND (black wire) to Feather GND
  • Connect board SCL (yellow wire) to Feather SCL
  • Connect board SDA (blue wire) to Feather SDA
  • Plug a 3.7/4.2V lithium polymer or lithium ion rechargeable battery into either of the JST battery ports on the board.
  • Plug the other board JST Battery port into the Feather JST port using the cable included with the board.
Watch out for battery polarity! A reversed battery will damage the monitor. There are + and - symbols on the PCB to indicate which is which.

Python Computer Wiring

Since there's dozens of Linux computers/boards you can use, we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported

Here's the Raspberry Pi wired to the sensor with I2C using STEMMA QT and a solderless breadboard.

  • Connect board VIN (red wire) to Pi 3V
  • Connect board GND (black wire) to Pi GND
  • Connect board SCL (yellow wire) to Pi SCL
  • Connect board SDA (blue wire) to Pi SDA

Plug a 3.7/4.2V lithium polymer or lithium ion rechargeable battery into either of the JST battery ports.

CircuitPython Installation of LC709203F Library

You'll need to install the Adafruit CircuitPython LC709203F library on your CircuitPython board.

First make sure you are running the latest version of Adafruit CircuitPython for your board.

Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle

Our CircuitPython starter guide has a great page on how to install libraries from the bundle.

Load the the following libraries into the lib folder on your CIRCUITPY drive:

  • adafruit_lc709203f.mpy
  • adafruit_bus_device

Before continuing make sure your board's lib folder or root filesystem has the adafruit_lc709203f.mpy file and adafruit_bus_device folder copied over.

Next connect to the board's serial console so you are ready to see the example output.

Python Installation of LC709203F Library

You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3.

Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!

Once that's done, from your command line run the following command:

pip3 install adafruit-circuitpython-lc709203f

If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

The LC709203 uses clock stretching, so on older Pi's (Pi 1, 2, 3, Zero) you will need to slow down the I2C clock to 10KHz https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/i2c-clock-stretching

CircuitPython & Python Usage

To demonstrate the usage of the sensor we'll initialize it and read the battery voltage and percentage from the board's Python REPL.

Run the following code to import the necessary modules and initialize the I2C connection with the sensor:

import board
from adafruit_lc709203f import LC709203F

sensor = LC709203F(board.I2C())

Now you're ready to use the following properties:

  • ic_version - Read-only chip version
  • cell_voltage - Floating point voltage
  • cell_percent - Percentage of cell capacity
  • power_mode - Current power mode (operating or sleeping)
  • pack_size - current battery pack size

For example to print the board version, and battery voltage and percentage values:

print("IC version:", hex(sensor.ic_version))
print("Battery voltage: %0.3f Volts" % (sensor.cell_voltage))
print("Battery percentage: %0.1f %%" % (sensor.cell_percent))

That's all there is to using the LC709203F LiPoly / LiIon Fuel Gauge and Battery Monitor with CircuitPython!

Full Example Code

# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense

import time
import board
from adafruit_lc709203f import LC709203F

print("LC709203F simple test")
print("Make sure LiPoly battery is plugged into the board!")

sensor = LC709203F(board.I2C())

print("IC version:", hex(sensor.ic_version))
while True:
    print(
        "Battery: %0.3f Volts / %0.1f %%" % (sensor.cell_voltage, sensor.cell_percent)
    )
    time.sleep(1)

This guide was first published on Sep 09, 2020. It was last updated on Sep 09, 2020.

This page (Python & CircuitPython) was last updated on Oct 23, 2021.

Text editor powered by tinymce.