LiPo Fuel Gauge (MAX1704X) Hookup Guide

Pages
Contributors: bboyho
Favorited Favorite 1

Introduction

The SparkFun LiPo Fuel Gauge - MAX17043 connects your LiPo battery to your project and uses a sophisticated algorithm to detect the relative state of charge and direct A/D measurement of battery voltage. In other words, it tells your microcontroller how much 'fuel' is left in the tank. The LiPo Fuel Gauge Breakout communicates with your project over I2C and an alert pin also tells you when the charge has dropped below a certain percentage.

SparkFun LiPo Fuel Gauge

SparkFun LiPo Fuel Gauge

TOL-20680
$12.50

Required Materials

To follow along with this tutorial, you will need the following materials at a minimum. You may not need everything though depending on what you have. Add it to your cart, read through the guide, and adjust the cart as necessary. Below is a wishlist of the parts that you need to get started.

Microcontroller

You will need a microcontroller with an I2C port when connecting to the LiPo Fuel Gauge. For the scope of this tutorial, we will be focusing on the Arduino Library for the LiPo Fuel Gauge.

Arduino Pro Mini 328 - 3.3V/8MHz

Arduino Pro Mini 328 - 3.3V/8MHz

DEV-11114
$10.95
54
SparkFun RedBoard Artemis Nano

SparkFun RedBoard Artemis Nano

DEV-15443
$16.50
6
SparkFun Qwiic Micro - SAMD21 Development Board

SparkFun Qwiic Micro - SAMD21 Development Board

DEV-15423
$22.50
5
SparkFun RedBoard Turbo - SAMD21 Development Board

SparkFun RedBoard Turbo - SAMD21 Development Board

DEV-14812
$19.95
8
Arduino Pro 328 - 3.3V/8MHz

Arduino Pro 328 - 3.3V/8MHz

DEV-10914
$18.50
1

Display

The example code can be used to print the voltage and state of charge of a single cell, LiPo battery using a serial monitor. For those that want to monitor a battery remotely, you can add a display to the setup. Below is the Qwiic Micro OLED breakout that can be used. You can also use a different display. However, you will need to adjust the code to display the readings properly.

SparkFun Micro OLED Breakout (Qwiic)

LCD-14532
7 Retired

Single Cell LiPo Battery

Of course, you will also need a single cell LiPo battery. Below are a few LiPo batteries to choose from in the SparkFun catalog.

Lithium Ion Battery - 1250mAh (IEC62133 Certified)

Lithium Ion Battery - 1250mAh (IEC62133 Certified)

PRT-18286
$10.95
2
Lithium Ion Battery - 850mAh

Lithium Ion Battery - 850mAh

PRT-13854
$10.95
2
Lithium Ion Battery - 400mAh

Lithium Ion Battery - 400mAh

PRT-13851
$5.50
11
Lithium Ion Battery - 6Ah

Lithium Ion Battery - 6Ah

PRT-13856
$32.50
7

Tools

Building a circuit using this breakout requires some assembly and soldering. You may already have a few of these items but if not, the tools and hardware below help with that assembly.

Hook-Up Wire - Assortment (Stranded, 22 AWG)

Hook-Up Wire - Assortment (Stranded, 22 AWG)

PRT-11375
$22.50
19

SparkFun Beginner Tool Kit

TOL-14681
Retired

Prototyping Accessories

Depending on your setup, you may want to use IC hooks for a temporary connection. However, you will want to solder header pins to connect devices to the plated through holes for a secure connection. Depending on your application, you could use straight headers or right angle headers. Of course, you could also solder wire as well.

Breadboard - Self-Adhesive (White)

Breadboard - Self-Adhesive (White)

PRT-12002
$5.50
48
Break Away Headers - Straight

Break Away Headers - Straight

PRT-00116
$1.75
20
JST Jumper 2 Wire Assembly

JST Jumper 2 Wire Assembly

PRT-09914
$1.05
3
IC Hook with Pigtail

IC Hook with Pigtail

CAB-09741
$5.50
10
Jumper Wires Premium 6" M/M Pack of 10

Jumper Wires Premium 6" M/M Pack of 10

PRT-08431
$4.50
2
Break Away Male Headers - Right Angle

Break Away Male Headers - Right Angle

PRT-00553
$2.25
5
Jumper Wire - JST Black Red

Jumper Wire - JST Black Red

PRT-08670
$1.05

For those that want to take advantage of the Qwiic enabled devices, you'll want to grab a Qwiic cable. Users can cut, strip, and solder half of a cable to easily connect the LiPo Fuel Gauge to a Qwiic-enabled microcontroller. For those that soldered male header pins to the board when prototyping, users can use a Qwiic cable with male pins or female sockets to connect without desoldering the header pins on the board. Note that this causes the board to have a higher height profile than soldering wires straight to the board.

SparkFun Qwiic Cable Kit

SparkFun Qwiic Cable Kit

KIT-15081
$8.95
22
Flexible Qwiic Cable - 200mm

Flexible Qwiic Cable - 200mm

PRT-17258
$1.60
Flexible Qwiic Cable - Female Jumper (4-pin)

Flexible Qwiic Cable - Female Jumper (4-pin)

CAB-17261
$1.50
Flexible Qwiic Cable - Breadboard Jumper (4-pin)

Flexible Qwiic Cable - Breadboard Jumper (4-pin)

PRT-17912
$1.60

Recommended Reading

If you aren’t familiar with the following concepts, we also recommend checking out a few of these tutorials before continuing.

Battery Technologies

The basics behind the batteries used in portable electronic devices: LiPo, NiMH, coin cells, and alkaline.

Electric Power

An overview of electric power, the rate of energy transfer. We'll talk definition of power, watts, equations, and power ratings. 1.21 gigawatts of tutorial fun!

I2C

An introduction to I2C, one of the main embedded communications protocols in use today.

What is a Battery?

An overview of the inner workings of a battery and how it was invented.

LilyPad Basics: Powering Your Project

Learn the options for powering your LilyPad projects, LiPo battery safety and care, and how to calculate and consider power constraints on your projects.

Breakout Board (MAX17043) Hardware Overview

Revision Change: The LiPo Fuel Gauge - MAX17043 illustrated in this section highlights V1.2. The hardware is slightly different. In V1.2, the battery voltage is separate from VCC's pull-up resistors. When using V1.2, make sure to connect a regulated 3.3V from your microcontroller to provide voltage to the pull-up resistors.

In this section, we will highlight parts of the LiPo Fuel Gauge (MAX17043) breakout board. For users that have a built in fuel gauge (MAX17043/MAX17048) already on your Arduino microcontroller, you can skip this section. The row of 1x3 header pins are arranged in a way so that you can insert the board a standard breadboard.

Top View Bottom View
Top View Bottom View

Battery and Power Input

The board includes a 2-pin JST connector to mate with single cell LiPo batteries. We have also broke out the pins to PTHs labeled as + and −. These can be used to solder the LiPo battery wires directly to the board and to your system's VBATT pin. The input voltage range is between 2.5V to 4.5V. Note that the nominal voltage of a single cell LiPo Battery is around 3.7V. Fully charged, the voltage is at around 4.2V. This input also powers the IC and should be connected to your system's power input as well.

Battery Input Highlighted - Top View Battery Input Highlighted - Bottom View
Battery Input
Highlighted - Top View
Battery Input
Highlighted - Bottom View

Pull-Up Resistor's Voltage Input

VCC pin is connected to the I2C and alert pull-up resistors. This voltage input pin is different from the battery input pin. The maximum voltage that can be connected to this pin is 5.5V. This is typically 3.3V. If you decide to daisy chain the LiPo Fuel Gauge to Qwiic-enabled devices, we recommend using 3.3V.

Power Input Highlighted - Top View Power Input Highlighted - Bottom View
Power Input
Highlighted - Top View
Power Input
Highlighted - Bottom View

I2C Pins

The I2C pins are broken out to PTHs. The 7-bit, unshifted address of the MAX17043 is 0x36. The address becomes 0x6C for write and 0x6D for read. There are two 2.2kΩ pull-up resistors connected to the SDA and SCL lines. These lines are connected to the VCC pin.

I2C Pins Highlighted - Top View I2C Pins Highlighted - Bottom View
I2C Pins
Highlighted - Top View
I2C Pins
Highlighted - Bottom View

Alert Pin (ALT)

The ALT pin is the alert pin. The datasheet labels this as ALRT but we decided to label it as ALT due to the size of the board. This pin is active low indicating that there is a low state of charge. This pin can be connected to a microcontroller's interrupt pin. This pin can be left unconnected and the status can be viewed though I2C.

Alert Pin Highlighted - Top View Alert Pin Highlighted - Bottom View<
Alert Pin
Highlighted - Top View
Alert Pin
Highlighted - Bottom View

Quick-Start Input Pin (QST)

The QST pin is for quick-start input. The datasheet labels this pin as QSTRT but we decided to label it as QST due to the size of the board. This allows users to reset the device through hardware. By default, the pin is connected to ground through a built-in 2.2kΩ resistor as suggested by the datasheet. A rising edge on this pin will initiate a hardware reset. One possible application is connecting this pin to a microcontrollers reset pin should users decide to initiate a hardware reset. A reset can also be initiated through software as well.

Alert Pin Highlighted - Top View Alert Pin Highlighted - Bottom View<
Quick-Start Input Pin
Highlighted - Top View
Quick-Start Input Pin
Highlighted - Bottom View

Jumpers

By default, this 3-pad jumper is closed and located on the bottom of the board. The 2.2kΩ pull-up resistors are attached to the primary I2C bus; if multiple devices are connected to the bus with the pull-up resistors enabled, the parallel equivalent resistance will create too strong of a pull-up for the bus to operate correctly. As a general rule of thumb, disable all but one pair of pull-up resistors if multiple devices are connected to the bus.

Jumpers

Jumpers Highlighted - Bottom View

Board Dimensions

The board is 0.40" x 0.95". To make the board as small as possible, there are no mounting holes included on the board.

Board Dimensions

Hardware Hookup

Now that we're familiar with the LiPo Fuel Gauge Breakout, let's connect it to a microcontroller and monitor a single cell LiPo battery!

LiPo Fuel Gauge Breakout Connections

For a permanent connection, we recommend soldering wires (or headers) to the PTHs on the breakout. We chose to use a combination of header pins and wires when prototyping. Of course, you could also solder wires to the breakout board as well. For a temporary connection during prototyping, you can use IC hooks like these.

How to Solder: Through-Hole Soldering

September 19, 2013

This tutorial covers everything you need to know about through-hole soldering.

Working with Wire

February 8, 2013

How to strip, crimp, and work with wire.

We recommend soldering the header pins and wires on one side. After soldering two rows of 1x3 header pins and a two wire cable, your setup should look like the following image below. We decided to solder the straight header pins and wire all on the top side of the board. Depending on your application, you could solder the straight header pins on the bottom side as well. This will allow you to easily view the silkscreen if you decide to solder on the bottom. Make sure to wire the red wire to the PTH labeled as "+" and the black wire to the PTH labeled as "−".

alt text

For users that want to prototype on a breadboard, you could insert the breakout board in the middle of a breadboard. Thanks to the header pin's plastic spacers, the cable can fit between the PCB and the breadboard. The image below shows the breakout board inserted into a mini breadboard. The edge of the board is on the edge of the mini breadboard so that you can disconnect/connect a LiPo battery to the 2-pin JST connector.

alt text

Connecting the LiPo Fuel Gauge to a Microcontroller

Connect the I2C pins, GND, and Vcc from LiPo Fuel Gauge to your microcontroller. We recommend using 3.3V for Vcc. Insert the battery into the LiPo Fuel Gauge's 2-pin JST connector. Then connect the JST cable that was soldered to your microcontroller's voltage input. In this case, we connected Qwiic cable to the RedBoard Artemis Nano's Qwiic connector and the 2-wire JST cable to the 2-pin JST connector.

Fritzing Diagram connected to microcontroller

Connecting a LiPo Charge Circuit

Users can include a LiPo charge circuit to safely charge the LiPo battery without needing to remove the LiPo battery from the LiPo Fuel Gauge. Below is one example that uses the LiPo Charger Plus to charge a single cell LiPo battery while it is also connected to an Arduino Pro Mini 3.3V/8MHz.

alt text

As the note indicates in the image, make sure to choose one power source for your Arduino microcontroller to avoid conflicting voltages: either from the LiPo battery or a USB-to-serial converter.

The Fritzing diagram shows male header pins connected to all but the VCC pin on the serial header. When connecting the USB-to-serial converter, this allows users to upload code or view serial data through the Arduino Serial Monitor without needing to worry about conflicting voltages from the FTDI's 3.3V pin.

Connecting a Boost Circuit

Users can also include a boost circuit when users need a steady 5V input. Below is one example that uses the LiPo Charger/Booster 5V/1A to boost the voltage to 5V for the RedBoard Qwiic. Most microcontrollers usually run at 3.3V so you may not need to worry about boosting it for your Arduino. However, 5V could be used for addressable LEDs, servos, and motors.

alt text

As the note indicates in the image, make sure to choose one power source for your Arduino microcontroller to avoid conflicting voltages: either from the LiPo battery and charger/booster, or USB on the Arduino.

The Fritzing diagram does not show the wires disconnected from the LiPo charger/booster. However, this would be the better option to ensure that the battery is not connected to the RedBoard Qwiic's input power pins. Users could also disconnect the 5V pin from the RedBoard Qwiic's input power pin.

The other option would be to hack the USB cable and disconnect the 5V wire, which would be more of a hassle.

Connecting a Display

For users that are interested in viewing how much charge a single cell LiPo battery has a available without a computer, users can attach a display to your microcontroller. Below is one example that addes a Qwiic Micro OLED to the first setup. Since you can control the display through I2C, it can be daisy chained using the Qwiic connectors. If you decide to use a different display, you will need to write code to output the values on the display.

alt text

Software Installation

Note: This example assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino IDE, library, or board add-on, please review the following tutorials.


If you've never connected an CH340 device to your computer before, you may need to install drivers for the USB-to-serial converter. Check out our section on "How to Install CH340 Drivers" for help with the installation.

SparkFun MAX1704x Fuel Gauge Arduino Library

The SparkFun MAX1704x Fuel Gauge Arduino Library can be downloaded with the Arduino library manager by searching 'SparkFun MAX1704x Fuel Gauge' or you can grab the zip here from the GitHub repository to manually install.


SparkFun Qwiic OLED Arduino Library

For users using a Qwiic Micro OLED to display the readings, the SparkFun Qwiic OLED Arduino Library can be downloaded with the Arduino library manager by searching 'SparkFun Qwiic OLED' or you can grab the zip here from the GitHub repository to manually install.


Example 1: Simple Serial

In this example, we will be checking a single cell LiPo battery's voltage and the state of charge using the MAX17043. The output will be sent to the Serial Monitor.

Hardware Hookup

For this example we will use the following parts from the wishlist.

Solder and connect the circuit based on the following diagram as shown earlier. Instead of inserting the LiPo Fuel Gauge in a mini breadboard, you could connect the flexible Qwiic cable with female jumpers to the male break away headers that were soldered on the breakout board. For a more permanent connection, you could also cut the female jumpers, strip the Qwiic cable wires, and solder directly to the breakout board.

Fritzing Diagram connected to microcontroller

Upload Code

From the menu, select the following: File > Examples > SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library > Example1_Simple. If you have not already, select your Board (in this case the RedBoard Artemis Nano), and associated COM port (in this case, COM27). Then hit the upload button.

alt text

Open the Arduino Serial Monitor and set it to 115200 baud to view the serial output. You should see the voltage, battery percent, and alert flag. In this case, the single cell LiPo battery that was connected to the IC was almost fully charged and at about 4.20V. Since the battery was higher than the threshold that was set, the alert flag was not triggered and remained low.

alt text

Depending on how your battery is connected to your system, the reading can be a bit misleading. When there is a dedicated LiPo battery charging circuit actively charging the single cell LiPo battery and the MAX1704X initially reads the battery, the values can be higher. Try disconnecting the LiPo battery from the microcontroller's VBATT pin and hitting the reset button on your microcontroller to restart the code. The image below shows both wires disconnected from the RedBoard Artemis Nano's JST connector since we would be using a 2-pin JST jumper wire.

alt text

By reopening the Arduino Serial Monitor, you may see a different reading reflecting the current state of the single cell LiPo battery rather than the output voltage of the charge IC. The image below shows actual voltage and remaining charge. You may want to add a display and write additional code as an alternative to connecting to a computer's serial terminal.

alt text

Example 4: MAX17048 KitchenSink

In this example, we will be checking a single cell LiPo battery's voltage and the state of charge using the MAX17048. The output will be sent to the Serial Monitor.

Hardware Hookup

For this example we will use the following parts from the wishlist.

In this case, we did not need to solder anything! The IoT RedBoard - ESP32 has a built in LiPo Fuel Gauge (MAX17048). By simply connecting a LiPo battery to the 2-pin JST connector and uploading code, we should be good to go!

alt text

Upload Code

From the menu, select the following: File > Examples > SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library > Example4_MAX17048_KitchenSink. If you have not already, select your Board (in this case the SparkFun ESP32 IoT RedBoard), and associated COM port (in this case COM27). Then hit the upload button.

LiPo connected to IoT RedBoard

Open the Arduino Serial Monitor and set it to 115200 baud to view the serial output. You should see the voltage, battery percent, alert flag, and several more readings. In this case, the single cell LiPo battery that was connected to the IC was fully charged and at about 4.10V.

Serial Output

Click on image for closer view.

But wait! Remember the previous example? If you looked closely at the circuit of the SparkFun IoT RedBoard - ESP32 Development Board, there is also... you guessed it: a charge circuit built in. Try closing out the Arduino Serial Monitor, disconnecting the USB, and disconnecting the LiPo battery. Then reinsert the LiPo battery, connect the USB cable, and reopen the Arduino Serial Monitor. The IC will recalculate everything. In the image below, the voltage is a bit misleading since the charge IC is charging the LiPo battery and may not be the true representation of the LiPo battery's voltage. The remaining charge was closer to what was expected.

Serial Output

Click on image for closer view.

More Examples!!!

Looking for more examples? Try checking the other examples in the Arduino Library. Example 2 is basically the same as Example 1, however the code was written for Arduino microcontrollers that have a different non-standard Wire and Serial ports. Example 3 is also based on Example but are for users that are using the MAX17044 IC (MAX17044 is configured for a dual-cell 2S pack). You will see the same readings on the Arduino Serial Monitor for both examples.

Combined Example A: Simple Serial and Qwiic Micro OLED

In this example, we will be checking a single cell LiPo battery's voltage and the state of charge using the MAX17043. The output will be sent to the Serial Monitor and the Qwiic Micro OLED.

Hardware Hookup

For this example we will use the following parts from the wishlist.

Solder and connect the circuit based on the following diagram as shown earlier. Instead of inserting the LiPo Fuel Gauge in a mini breadboard, you could connect the flexible Qwiic cable with female jumpers to the male break away headers that were soldered on the breakout board. For a more permanent connection, you could also cut the female jumpers, strip the Qwiic cable wires, and solder directly to the breakout board. Insert the Qwiic Micro OLED between the LiPo Fuel Gauge and the RedBoard Artemis Nano.

alt text

After soldering and connecting the boards together, your setup should look similar to the following.

alt text

Depending on the microcontroller that you have and setup, you may want to disconnect the LiPo battery when uploading code and monitoring the LiPo battery through the Arduino Serial Monitor. The values may be misleading due the built in charge circuit that is on the RedBoard Artemis Nano.

alt text

Upload Code

Copy and paste following code into your Arduino IDE. If you have not already, select your Board (in this case the RedBoard Artemis Nano), and associated COM port (in this case, COM27). Then hit the upload button.

language:c
/******************************************************************************
  Combined Simple Serial and Qwiic Micro OLED Example
  Modified By: Ho Yun "Bobby" Chan
  SparkFun Electronics
  Date: February 10, 2023
  License: MIT. See license file for more information but you can
  basically do whatever you want with this code.

  This is a combined example of Paul Clark's MAX17043 Fuel Guage
  simple serial example and Kirk Benell's Qwiic OLED Hello
  example. The example reads a single cell LiPo battery's voltage
  and state-of-charge (SOC) using the MAX1704X. The voltage,
  percent remaining (i.e. the SOC), and alert flag are displayed
  as an output on the Qwiic Micro OLED.

  By opening the Arduino Serial Monitor (115200 baud), the example
  will also print the gauge's voltage, state-of-charge (SOC)
  readings, alert status to Serial.

  Feel like supporting open source hardware?
  Buy a board from SparkFun!

  LiPo Fuel Gauge - MAX17043: https://www.sparkfun.com/products/20680
  Qwiic Micro OLED: https://www.sparkfun.com/products/14532

  Distributed as-is; no warranty is given.
******************************************************************************/

#include <Wire.h> // Needed for I2C

//////////LIPO FUEL GAUGE//////////
#include <SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library.h> // Click here to get the library: http://librarymanager/All#SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library

SFE_MAX1704X lipo; // Defaults to the MAX17043

//SFE_MAX1704X lipo(MAX1704X_MAX17043); // Create a MAX17043
//SFE_MAX1704X lipo(MAX1704X_MAX17044); // Create a MAX17044
//SFE_MAX1704X lipo(MAX1704X_MAX17048); // Create a MAX17048
//SFE_MAX1704X lipo(MAX1704X_MAX17049); // Create a MAX17049

double voltage = 0; // Variable to keep track of LiPo voltage
double soc = 0; // Variable to keep track of LiPo state-of-charge (SOC)
bool alert; // Variable to keep track of whether alert has been triggered



//////////QWIIC MICRO OLED//////////
#include <SparkFun_Qwiic_OLED.h> //http://librarymanager/All#SparkFun_Qwiic_Graphic_OLED

// The Qwiic OLED Library supports three different types of SparkFun boards. The demo uses the following
// defines to determine which device is being used. Uncomment the device being used for this demo.

QwiicMicroOLED myOLED;
// QwiicTransparentOLED myOLED;
// QwiicNarrowOLED myOLED;

// Fonts
#include <res/qw_fnt_5x7.h>
//#include <res/qw_fnt_8x16.h>, not used
//#include <res/qw_fnt_31x48.h>, not used
//#include <res/qw_fnt_7segment.h>, not used
//#include <res/qw_fnt_largenum.h>, not used



void setup() {
  Serial.begin(115200); // Start serial, to output debug data
  //while (!Serial)
  //  ; //Wait for user to open terminal
  Serial.println(F("Combined MAX17043 Simple Serial Example & Qwiic OLED Example"));

  Wire.begin();

  lipo.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial

  // Set up the MAX17043 LiPo fuel gauge:
  if (lipo.begin() == false) // Connect to the MAX17043 using the default wire port
  {
    Serial.println(F("MAX17043 not detected. Please check wiring. Freezing."));
    while (1)
      ;
  }

  // Initalize the OLED device and related graphics system
  if (myOLED.begin() == false)
  {
    Serial.println("Device begin failed. Freezing...");
    while (true)
      ;
  }

  // Quick start restarts the MAX17043 in hopes of getting a more accurate
  // guess for the SOC.
  lipo.quickStart();

  // We can set an interrupt to alert when the battery SoC gets too low.
  // We can alert at anywhere between 1% - 32%:
  lipo.setThreshold(20); // Set alert threshold to 20%.
}// end setup()


void loop() {

  // lipo.getVoltage() returns a voltage value (e.g. 3.93)
  voltage = lipo.getVoltage();

  // lipo.getSOC() returns the estimated state of charge (e.g. 79%)
  soc = lipo.getSOC();

  // lipo.getAlert() clears the alert flag
  // Output: 0 on success, positive integer on fail.
  lipo.clearAlert();
  // lipo.getAlert() returns a 0 or 1 (0=alert not triggered)
  alert = lipo.getAlert();

  myOLED.erase(); //clear display

  //set font type, we'll use a character size of 5x7
  myOLED.setFont(&QW_FONT_5X7);
  //myOLED.setFont(&QW_FONT_8X16);  //not used
  //myOLED.setFont(&QW_FONT_31X48); //not used
  //myOLED.setFont(&QW_FONT_LARGENUM); //not used
  //myOLED.setFont(&QW_FONT_7SEGMENT); //not used

  //Print Voltage
  myOLED.setCursor(0, 0);
  myOLED.print(voltage, 2);
  myOLED.print(F("V"));

  //Print Battery %
  myOLED.setCursor(0, 10);
  myOLED.print(soc, 2);
  myOLED.print(F("%"));

  //Print Alert Status
  myOLED.setCursor(0, 20);
  myOLED.print(F("VBAT:")); //alert pin
  if (alert == HIGH) {
    myOLED.print("LOW"); //Flag was raised, battery is low!!!
  }
  else {

    myOLED.print(F("OK")); //Battery charge is good. 8)
  }

  // There's nothing on the screen yet - Now send the graphics to the device
  myOLED.display();


  // Print the variables to Serial Terminal:
  Serial.print(F("Voltage: "));
  Serial.print(voltage);  // Print the battery voltage
  Serial.println(F(" V"));

  Serial.print(F("Percentage: "));
  Serial.print(soc); // Print the battery state of charge
  Serial.println(F(" %"));

  Serial.print(F("Alert: "));
  Serial.println(alert);
  Serial.println();

  delay(500);
}//end loop()

Disconnect the USB cable from your RedBoard Artemis Nano. Hit the reset button.

alt text

Looking close at the display, you should see the voltage, remaining charge, the alert flag indicating if the battery is low, and a battery meter icon. These values may be different depending on how much charge the LiPo battery has available.

alt text

Combined Example B: Simple Serial, Qwiic Micro OLED, Battery Icon

This example is pretty much the same as the previous combined example. However, we will add an additional battery meter icon.

Hardware Hookup

For this example we will use the same parts as the previous combined example's wishlist.

alt text

Upload Code

Copy and paste following code into your Arduino IDE.

language:c
/******************************************************************************
  Combined Simple Serial and Qwiic Micro OLED Example
  Modified By: Ho Yun "Bobby" Chan
  SparkFun Electronics
  Date: February 10, 2023
  License: MIT. See license file for more information but you can
  basically do whatever you want with this code.

  This is a combined example of Paul Clark's MAX17043 Fuel Guage
  simple serial example, Kirk Benell's Qwiic OLED Hello example,
  and Nathan Seidle's RTK Display Test Sketch. The example reads a
  single cell LiPo battery's voltage and state-of-charge (SOC) 
  using the MAX1704X. The voltage, percent remaining (i.e. the 
  SOC), and alert flag are displayed as an output on the Qwiic
  Micro OLED. A graphic of a LiPo battery's remaining charge is
  displayed on the Qwiic Micro OLED as well.

  By opening the Arduino Serial Monitor (115200 baud), the example
  will also print the gauge's voltage, state-of-charge (SOC)
  readings, alert status to Serial.

  Feel like supporting open source hardware?
  Buy a board from SparkFun!

  LiPo Fuel Gauge - MAX17043 : https://www.sparkfun.com/products/20680
  Qwiic Micro OLED: https://www.sparkfun.com/products/14532

  Distributed as-is; no warranty is given.
******************************************************************************/

#include <Wire.h> // Needed for I2C


//////////LIPO FUEL GAUGE//////////
#include <SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library.h> // Click here to get the library: http://librarymanager/All#SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library

SFE_MAX1704X lipo; // Defaults to the MAX17043

//SFE_MAX1704X lipo(MAX1704X_MAX17043); // Create a MAX17043
//SFE_MAX1704X lipo(MAX1704X_MAX17044); // Create a MAX17044
//SFE_MAX1704X lipo(MAX1704X_MAX17048); // Create a MAX17048
//SFE_MAX1704X lipo(MAX1704X_MAX17049); // Create a MAX17049

double voltage = 0; // Variable to keep track of LiPo voltage
double soc = 0; // Variable to keep track of LiPo state-of-charge (SOC)
bool alert; // Variable to keep track of whether alert has been triggered



//////////QWIIC MICRO OLED//////////
#include <SparkFun_Qwiic_OLED.h> //http://librarymanager/All#SparkFun_Qwiic_Graphic_OLED
#include "icons.h"

// The Qwiic OLED Library supports three different types of SparkFun boards. The demo uses the following
// defines to determine which device is being used. Uncomment the device being used for this demo.
QwiicMicroOLED myOLED;
// QwiicTransparentOLED myOLED;
// QwiicNarrowOLED myOLED;

// Fonts
#include <res/qw_fnt_5x7.h>
//#include <res/qw_fnt_8x16.h>, not used
//#include <res/qw_fnt_31x48.h>, not used
//#include <res/qw_fnt_7segment.h>, not used
//#include <res/qw_fnt_largenum.h>, not used



void setup() {
  Serial.begin(115200); // Start serial, to output debug data
  //while (!Serial)
  //  ; //Wait for user to open terminal
  Serial.println(F("Combined MAX17043 Example & Qwiic OLED Example"));

  Wire.begin();

  lipo.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial

  // Set up the MAX17043 LiPo fuel gauge:
  if (lipo.begin() == false) // Connect to the MAX17043 using the default wire port
  {
    Serial.println(F("MAX17043 not detected. Please check wiring. Freezing."));
    while (1)
      ;
  }

  // Initalize the OLED device and related graphics system
  if (myOLED.begin() == false)
  {
    Serial.println(F("Device begin failed. Freezing..."));
    while (true)
      ;
  }

  // Quick start restarts the MAX17043 in hopes of getting a more accurate
  // guess for the SOC.
  lipo.quickStart();

  // We can set an interrupt to alert when the battery SoC gets too low.
  // We can alert at anywhere between 1% - 32%:
  lipo.setThreshold(20); // Set alert threshold to 20%.
}

void loop() {

  // lipo.getVoltage() returns a voltage value (e.g. 3.93)
  voltage = lipo.getVoltage();

  // lipo.getSOC() returns the estimated state of charge (e.g. 79%)
  soc = lipo.getSOC();

  // lipo.getAlert() clears the alert flag
  // Output: 0 on success, positive integer on fail.
  lipo.clearAlert();
  // lipo.getAlert() returns a 0 or 1 (0=alert not triggered)
  alert = lipo.getAlert();

  myOLED.erase(); //clear display

  //set font type, we'll use a character size of 5x7
  myOLED.setFont(&QW_FONT_5X7);
  //myOLED.setFont(&QW_FONT_8X16); //not used
  //myOLED.setFont(&QW_FONT_31X48); //not used
  //myOLED.setFont(&QW_FONT_LARGENUM); //not used
  //myOLED.setFont(&QW_FONT_7SEGMENT); //not used

  // "Print" Voltage
  myOLED.setCursor(0, 0);
  myOLED.print(voltage, 2);
  myOLED.print(F("V"));

  // "Print" Battery %
  myOLED.setCursor(0, 10);
  myOLED.print(soc, 2);
  myOLED.print(F("%"));

  // "Print" Alert Status
  myOLED.setCursor(0, 20);
  myOLED.print(F("VBAT:")); //alert pin
  if (alert == HIGH) {
    myOLED.print(F("LOW")); //Flag was raised, battery is low!!!
  }
  else {

    myOLED.print(F("OK")); //Battery charge is good. 8)
  }

  if (soc >= 50.00) {
    //Battery Level 50-100%
    displayBitmap(0, 30, Battery_2_Width, Battery_2_Height, Battery_3);
  }
  else if (20.00 <= soc < 50.00) {
    //Battery Level 20-50%
    displayBitmap(0, 30, Battery_2_Width, Battery_2_Height, Battery_2);
  }
  else if (10.00 <= soc < 20.00) {
    //Battery Level 10%-20%
    displayBitmap(0, 30, Battery_2_Width, Battery_2_Height, Battery_1);
  }
  else {
    //Battery Level <10%
    displayBitmap(0, 30, Battery_2_Width, Battery_2_Height, Battery_0);
  }


  // There's nothing on the screen yet - Now send the graphics to the device
  myOLED.display();


  // Print the variables to Serial Terminal:
  Serial.print(F("Voltage: "));
  Serial.print(voltage);  // Print the battery voltage
  Serial.println(" V");

  Serial.print(F("Percentage: "));
  Serial.print(soc); // Print the battery state of charge
  Serial.println(" %");

  Serial.print(F("Alert: "));
  Serial.println(alert);
  Serial.println();

  delay(500);
}



//Wrapper to avoid needing to pass width/height data twice
void displayBitmap(uint8_t x, uint8_t y, uint8_t imageWidth, uint8_t imageHeight, uint8_t *imageData) {
  myOLED.bitmap(x, y, x + imageWidth, y + imageHeight, imageData, imageWidth, imageHeight);
}

To keep track of the icons that we create, we are going to create a header file with the *.ino. This is useful when writing code for big projects that involve a lot of components (e.g. RTK Express, RTK Express Plus, RTK Facet, RTK Facet L-Band, etc.). Click on the icon to create a new tab. We will name this icons.h.

alt text

Copy and paste the following code into tab.

language:c
uint8_t Battery_3 [] = {
0xFF, 0x01, 0xFD, 0xFD, 0xFD, 0x01, 0x01, 0xFD, 0xFD, 0xFD, 0x01, 0x01, 0xFD, 0xFD, 0xFD, 0x01,
0x0F, 0x08, 0xF8, 0x0F, 0x08, 0x0B, 0x0B, 0x0B, 0x08, 0x08, 0x0B, 0x0B, 0x0B, 0x08, 0x08, 0x0B,
0x0B, 0x0B, 0x08, 0x0F, 0x01, 0x01, 
};
int Battery_3_Height = 12;
int Battery_3_Width = 19;

uint8_t Battery_2 [] = {
0xFF, 0x01, 0xFD, 0xFD, 0xFD, 0x01, 0x01, 0xFD, 0xFD, 0xFD, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x0F, 0x08, 0xF8, 0x0F, 0x08, 0x0B, 0x0B, 0x0B, 0x08, 0x08, 0x0B, 0x0B, 0x0B, 0x08, 0x08, 0x08,
0x08, 0x08, 0x08, 0x0F, 0x01, 0x01, 
};
int Battery_2_Height = 12;
int Battery_2_Width = 19;

uint8_t Battery_1 [] = {
0xFF, 0x01, 0xFD, 0xFD, 0xFD, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x0F, 0x08, 0xF8, 0x0F, 0x08, 0x0B, 0x0B, 0x0B, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
0x08, 0x08, 0x08, 0x0F, 0x01, 0x01, 
};
int Battery_1_Height = 12;
int Battery_1_Width = 19;

uint8_t Battery_0 [] = {
0xFF, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x0F, 0x08, 0xF8, 0x0F, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
0x08, 0x08, 0x08, 0x0F, 0x01, 0x01, 
};
int Battery_0_Height = 12;
int Battery_0_Width = 19;

If you have not already, select your Board (in this case the RedBoard Artemis Nano), and associated COM port (in this case, COM27). Then hit the upload button. Disconnect the USB cable from your RedBoard Artemis Nano. Hit the reset button.

alt text

Looking close at the display, you should see the voltage, remaining charge, the alert flag indicating if the battery is low, and a battery meter icon. These values may be different depending on how much charge the LiPo battery has available!

alt text

Troubleshooting

Resources and Going Further

Now that you've successfully got your LiPo Fuel Gauge (MAX17043) up and running, it's time to incorporate it into your own project! For more information, check out the resources below:

Need some inspiration for your next project? Check out some of these related tutorials. Looking for an alternative solution to using a display? Try using an RGB LED to display the remaining charge.

Photon Battery Shield Hookup Guide

The Photon Battery Shield has everything your Photon needs to run off, charge, and monitor a LiPo battery. Read through this hookup guide to get started using it.

Battery Babysitter Hookup Guide

An introduction and getting started guide for the Battery Babysitter - a flexible LiPo battery charger and monitor.

Wireless Joystick Hookup Guide

A hookup guide for the SparkFun Wireless Joystick Kit.

SparkFun RTK Facet Hookup Guide

Setup the RTK Facet in minutes to begin gathering millimeter level geospatial coordinates.

Or check out some of these blog posts for ideas using LiPo fuel gauges.