Example Code for Arduino-Read Battery Voltage, Remaining Power and Set Low Power Interrupt Alert

Last revision 2025/12/11

A guide offers example code and detailed instructions for using an Arduino to efficiently read battery voltage, calculate remaining power, and set up low power interrupt alerts. Users will learn to connect hardware components such as the DFRobot Gravity 3.7V Li Battery Fuel Gauge and configure the MAX17043 library within the Arduino IDE. The article includes sample code for establishing communication between the Arduino and the gauge, as well as steps to customize alert thresholds. By following the wiring diagram and setup procedures, users can enhance their project's power management capabilities, ensuring timely alerts when battery power dips below specified levels. This guide is essential for anyone looking to optimize their Arduino projects with precise battery monitoring and alert systems.

Hardware Preparation

Software Preparation

  • Arduino IDE Click to Download Arduino IDE
  • Search and install the 'DFRobot_MAX17043' library in the Arduino IDE library manager
  • Or download and install the .zip library in the Github

Wiring Diagram

Other Preparation Work

  1. Connect the module to the Arduino according to the connection diagram. The battery can be connected to the 2P terminal or JST-PH2.0 2P connector. These two connectors are connected internally in parallel. The I2C address is fixed to 0x36.
  2. Install DFRobot_MAX17043 library.
  3. Open Arduino IDE, upload the following sample code to the Arduino UNO.
  4. Open the serial monitor of the Arduino IDE and set the baud rate to 115200.

Sample Code

/*
 * file DFRobot_MAX17043.ino
 *
 * connect gauge I2C interface with your board (please reference board compatibility)
 *
 * Voltage, percentage will be printed via serial.
 * Use API to config alaram and sleep (please reference to the readme in lib)
 *
 * Copyright   [DFRobot](https://www.dfrobot.com), 2016
 * Copyright   GNU Lesser General Public License
 *
 * version  V1.0
 * date  2018-3-26
 */

#include "DFRobot_MAX17043.h"
#include "Wire.h"

#ifdef __AVR__
  #define ALR_PIN       2
#else
  #define ALR_PIN       D2
#endif

#define PRINT_INTERVAL        2000
#define BAT_PERCENTAGE        32

DFRobot_MAX17043        gauge;
uint8_t       intFlag = 0;

void interruptCallBack()
{
  intFlag = 1;
}

void setup()
{
  Serial.begin(115200);
  while(!Serial);
  Serial.println();
  Serial.println();
  pinMode(ALR_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(ALR_PIN), interruptCallBack, FALLING);  //default alert is 32%

  while(gauge.begin() != 0) {
    Serial.println("gauge begin faild!");
    delay(2000);
  }
  delay(2);
  Serial.println("gauge begin successful!");
  //gauge.setInterrupt(BAT_PERCENTAGE);  //use this to modify alert threshold as 1% - 32% (integer)
}

void loop()
{
  static unsigned long lastMillis = 0;
  if((millis() - lastMillis) > PRINT_INTERVAL) {
    lastMillis = millis();
    Serial.println();

    Serial.print("voltage: ");
    Serial.print(gauge.readVoltage());
    Serial.println(" mV");

    Serial.print("precentage: ");
    Serial.print(gauge.readPercentage());
    Serial.println(" %");
  }

  if(intFlag == 1) {
    intFlag = 0;
    gauge.clearInterrupt();
    Serial.println("Low power alert interrupt!");
    //put your battery low power alert interrupt service routine here
  }
}

Result

  • Arduino prints the current voltage (voltage), remaining power (percentage), and interrupt alert (if any) information every 2 seconds.
  • Battery low power interrupt alert ALR instructions:
    • The default value of battery low power interrupt alert threshold is 32%. That is, when the remaining power is lower than 32%, a falling edge interrupt is generated on the ALR pin. The threshold BAT_PERCENTAGE can be set to any integer between 1-32 (corresponding to 1%-32%, respectively). This threshold can be set by the function setInterrupt().
    • When the battery's initial remaining power is higher than BAT_PERCENTAGE, the ALR pin is set high. If it falls below BAT_PERCENTAGE (due to discharge) , ALR is pulled to low. The controller is triggered to print ”Low power alert interrupt!”, and clear interrupt through clearInterrupt(), which causes ALR back to high immediately.
    • When the battery's initial remaining power is below BAT_PERCENTAGE, ALR generates a interrupt at the beginning.
    • After the battery remaining power grows higher than BAT_PERCENTAGE (due to charge) , another interrupt will be generated when the power again falls below BAT_PERCENTAGE (due to discharge). If the clearInterrupt() is not called after the interrupt is occurred, ALR remains low regardless of the battery remaining power.

Was this article helpful?

TOP