It's easy to use the MAX17048 with Python or CircuitPython, and the Adafruit_CircuitPython_MAX1704x module. This module allows you to easily write Python code that reads the values from the MAX17048's battery monitoring and alert functions. 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

First, wire up a MAX17048 to your board exactly as shown below. Here's an example of wiring a Feather M4 to the sensor with I2C using one of the handy STEMMA QT connectors:

STEMMA

  • Board 3V to sensor VIN (red wire)
  • Board GND to sensor GND (black wire)
  • Board SCL to sensor SCL (yellow wire)
  • Board SDA to sensor SDA (blue wire)

Battery

  • 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.

You can also use standard 0.100" pitch headers to wire it up on a breadboard:

STEMMA

  • Board 3V to sensor VIN (red wire)
  • Board GND to sensor GND (black wire)
  • Board SCL to sensor SCL (yellow wire)
  • Board SDA to sensor SDA (blue wire)

Battery

  • 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, below shows 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 using I2C and a STEMMA QT connector:

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

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

Finally here is an example of how to wire up a Raspberry Pi to the sensor using a solderless breadboard:

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

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

Python Installation of MAX1704X 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-max1704x

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

CircuitPython Usage

To use with CircuitPython, you need to first install the MAX1704X library, and its dependencies, 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 the following folders and file:

  • adafruit_bus_device/
  • adafruit_register/
  • adafruit_max1704x.mpy
CIRCUITPY

Python Usage

Once you have the library pip3 installed on your computer, copy or download the following example to your computer, and run the following, replacing code.py with whatever you named the file:

python3 code.py

Example Code - Simple Test

# SPDX-FileCopyrightText: Copyright (c) 2022 ladyada for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense

import time
import board
import adafruit_max1704x

i2c = board.I2C()  # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
max17 = adafruit_max1704x.MAX17048(i2c)

print(
    "Found MAX1704x with chip version",
    hex(max17.chip_version),
    "and id",
    hex(max17.chip_id),
)

# Quick starting allows an instant 'auto-calibration' of the battery. However, its a bad idea
# to do this right when the battery is first plugged in or if there's a lot of load on the battery
# so uncomment only if you're sure you want to 'reset' the chips charge calculator.
# print("Quick starting")
# max17.quick_start = True

while True:
    print(f"Battery voltage: {max17.cell_voltage:.2f} Volts")
    print(f"Battery state  : {max17.cell_percent:.1f} %")
    print("")
    time.sleep(1)

If running CircuitPython: Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!

If running Python: The console output will appear wherever you are running Python.

In the max1704x_simpletest.py example, the MAX17048 is instantiated on I2C. The chip version and chip ID is printed to the REPL. 

In the loop, the battery's voltage and charge percentage is printed to the REPL every second.

Example Code - Advanced Test

# SPDX-FileCopyrightText: Copyright (c) 2022 ladyada for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense

import time
import board
import adafruit_max1704x

i2c = board.I2C()  # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
max17 = adafruit_max1704x.MAX17048(i2c)

print(
    "Found MAX1704x with chip version",
    hex(max17.chip_version),
    "and id",
    hex(max17.chip_id),
)

# Quick starting allows an instant 'auto-calibration' of the battery. However, its a bad idea
# to do this right when the battery is first plugged in or if there's a lot of load on the battery
# so uncomment only if you're sure you want to 'reset' the chips charge calculator.
# print("Quick starting")
max17.quick_start = True

# The reset voltage is what the chip considers 'battery has been removed and replaced'
# The default is 3.0 Volts but you can change it here:
# max17.reset_voltage = 2.5
print("MAX1704x reset voltage = %0.1f V" % max17.reset_voltage)

# The analog comparator is used to detect the rest voltage, if you don't think the battery
# will ever be removed this can reduce current usage (see datasheet on VRESET.Dis)
print("Analog comparator is ", end="")
if max17.comparator_disabled:
    print("disabled")
else:
    print("enabled")

# Hibernation mode reduces how often the ADC is read, for power reduction. There is an automatic
# enter/exit mode but you can also customize the activity threshold both as voltage and charge rate
# max17.activity_threshold = 0.15
print("MAX1704x activity threshold = %0.2f V" % max17.activity_threshold)

# max17.hibernation_threshold = 5
print("MAX1704x hibernation threshold = %0.2f %%" % max17.hibernation_threshold)

# You can also 'force' hibernation mode!
# max17.hibernate()
# ...or force it to wake up!
# max17.wake()

# The alert pin can be used to detect when the voltage of the battery goes below or
# above a voltage, you can also query the alert in the loop.
max17.voltage_alert_min = 3.5
print("Voltage alert minimum = %0.2f V" % max17.voltage_alert_min)
max17.voltage_alert_max = 4.1
print("Voltage alert maximum = %0.2f V" % max17.voltage_alert_max)

print("")
while True:
    print(f"Battery voltage: {max17.cell_voltage:.2f} Volts")
    print(f"Battery state  : {max17.cell_percent:.1f} %")

    # we can check if we're hibernating or not
    if max17.hibernating:
        print("Hibernating!")

    if max17.active_alert:
        print("Alert!")
        if max17.reset_alert:
            print("  Reset indicator")
            max17.reset_alert = False  # clear the alert

        if max17.voltage_high_alert:
            print("  Voltage high")
            max17.voltage_high_alert = False  # clear the alert

        if max17.voltage_low_alert:
            print("  Voltage low")
            max17.voltage_low_alert = False  # clear the alert

        if max17.voltage_reset_alert:
            print("  Voltage reset")
            max17.voltage_reset_alert = False  # clear the alert

        if max17.SOC_low_alert:
            print("  Charge low")
            max17.SOC_low_alert = False  # clear the alert

        if max17.SOC_change_alert:
            print("  Charge changed")
            max17.SOC_change_alert = False  # clear the alert
    print("")
    time.sleep(1)

In the max1704x_advanced.py example, the MAX17048 is instantiated on I2C. The chip version and chip ID is printed to the REPL along with thresholds for the alerts that are available in the library.

In the loop, the battery's voltage, charge percentage and any active alerts are printed to the REPL every second.

Voltage High Alert:

Voltage Low Alert:

Hibernating Alert:

This guide was first published on Sep 07, 2022. It was last updated on Jul 18, 2024.

This page (Python & CircuitPython) was last updated on Jul 18, 2024.

Text editor powered by tinymce.