smôl Power Board LiPo Hookup Guide

Pages
Contributors: PaulZC
Favorited Favorite 0

Arduino Example: Battery Voltage

If you are using the smôl ESP32 Processor Board, you are going to want to install the CP210x USB Driver and Arduino Boards Package for the ESP32 first. Please see the smôl ESP32 Hookup Guide for more details.

The smôl Power Boards have their own Arduino Library to make communicating with the board as simple as calling a function. You can install the library through the Arduino IDE Library Manager by searching for SparkFun smol power. Alternatively, you can grab the library from GitHub or can download it as a zip file by clicking the button below:

You are also going to need to install the SparkFun MAX1704x Fuel Gauge Arduino Library. Again, you can install this library through the Arduino IDE Library Manager by searching for SparkFun MAX1704x. Alternatively, you can grab the library from GitHub or can download it as a zip file by clicking the button below:

The Power Board library contains a set of tried-and-tested examples which will work with both the smôl Power Board LiPo and the smôl Power Board AAA. There is only one line of code to change when switching from one to the other.

The following code is a simplified version of Example2_BatteryVoltage. Upload the code onto your processor board and open the Serial Monitor or a terminal emulator at 115200 baud to see the output.

language:c
#include <Wire.h>

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

smolPowerLiPo myPowerBoard;

void setup()
{
  Serial.begin(115200);
  while (!Serial)
    ; // Wait for the user to open the Serial console
  Serial.println(F("smôl Power Board example"));
  Serial.println();

  Wire.begin();

  if (myPowerBoard.begin() == false) // Begin communication with the power board using the default I2C address (0x50) and the Wire port
  {
    Serial.println(F("Could not communicate with the power board. Please check the I2C connections. Freezing..."));
    while (1)
      ;
  }

  float voltage = myPowerBoard.getBatteryVoltage(); // Read the battery voltage from the fuel gauge
  Serial.print(F("The battery voltage reads as: "));
  Serial.println(voltage);

  if (voltage == -99.0)
  {
    Serial.println(F("A voltage of -99.0V indicates an error."));
  }
}

void loop()
{
  //Nothing to do here
}