Skip to content

Instantly share code, notes, and snippets.

@anecdata
Created December 4, 2021 20:41
Show Gist options
  • Save anecdata/63204130b01d0dafaa299c6fa166c778 to your computer and use it in GitHub Desktop.
Save anecdata/63204130b01d0dafaa299c6fa166c778 to your computer and use it in GitHub Desktop.
CircuitPython Battery Fuel Gauge: LC709203F [+ SH1107]
import time
import board
import supervisor
import displayio
import adafruit_displayio_sh1107
from adafruit_lc709203f import LC709203F, PackSize
print("LC709203F Battery Fuel Gauge")
i2c = board.I2C()
try:
displayio.release_displays()
display_bus = displayio.I2CDisplay(i2c, device_address=0x3c)
display = adafruit_displayio_sh1107.SH1107(display_bus, width=128, height=64)
display.brightness = 0.25 # 0.0-1.0
except RuntimeError as e:
print('OLED:', e)
while True:
try:
sensor = LC709203F(i2c)
sensor.pack_size = PackSize.MAH400
print(f"IC version: {hex(sensor.ic_version)}")
break
except ValueError as e:
print(f"{supervisor.ticks_ms():9d} No LC709203F or no battery ({e})")
time.sleep(1)
while True:
print(f"{supervisor.ticks_ms():9d}", end=" ")
try:
print(" ".join((f"Battery: {sensor.cell_voltage:0.3f}v",
f"{sensor.cell_percent:0.1f}%",
f"pack={sensor.pack_size}",
f"pwr={sensor.power_mode}",
f"prof={sensor.battery_profile}",)))
except (RuntimeError, OSError) as e:
print(e)
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment