LiPo Fuel Gauge (MAX1704X) Hookup Guide

Pages
Contributors: bboyho
Favorited Favorite 0

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