Coder Social home page Coder Social logo

max1704x's Introduction

MAX1704X

Arduino library for the MAX17043 and MAX17044 LiPo Battery Fuel Gauge. For an example of this device on a breakout board see the SparkFun LiPo Fuel Gauge.

This device is also available on breakout boards from Amazon. Be cautious when ordering these from unknown suppliers. I have read on various forums that some of these do not work properly. I recommend ordering from SparkFun.

Usage

The step is to include the library in your sketch. Include MAX17043.h if you are suing the single cell chip or MAX17044.h if you are using the two cell chip.

#include "MAX17043.h"

Next, in the setup() routine call begin on the Fuel Gauge. Initialize the Serial interface to display the device properties to the serial output.

void setup()
{
  // Initialize the serial interface.
  Serial.begin(115200);

  // Initialize the fuel gauge.
  FuelGauge.begin();
}

In the loop, use the properties to show the state of the battery and the fuel gauge.

void loop()
{
  // Get the voltage, battery percent
  // and other properties.
  Serial.print("Version:   "); Serial.println(FuelGauge.version());
  Serial.print("ADC:   "); Serial.println(FuelGauge.adc());
  Serial.print("Voltage:   "); Serial.print(FuelGauge.voltage()); Serial.println(" mV");
  Serial.print("Percent:   "); Serial.print(FuelGauge.percent()); Serial.println("%");
  Serial.print("Is Sleeping:   "); Serial.println(FuelGauge.isSleeping() ? "Yes" : "No");
  Serial.print("Alert: "); Serial.println(FuelGauge.alertIsActive() ? "Yes" : "No");
  Serial.print("Threshold: "); Serial.println(FuelGauge.getThreshold());
  Serial.print("Compensation:  0x"); Serial.println(FuelGauge.compensation(), HEX);
}

Sleep Mode

While in Sleep mode, all IC operations are halted and power drain of the IC is greatly reduced. After exiting Sleep mode, fuel-gauge operation continues from the point it was halted.

Sleep mode can be entered by calling the sleep() method to put the device into sleep mode via software and the wake() method to take it out of sleep mode. The isSleeping() method can be used to get the current state of device.

Entering Sleep mode does not clear the interrupt.

Entering Seep Mode

The sample code below demonstrates how to check if the device is in sleep mode and then puts it in sleep mode if it is not.

if (!FuelGauge.isSleeping())
{
  FuelGauge.sleep();

  if (FuelGauge.isSleeping())
  {
	Serial.println("Fuel Gauge put in sleep mode.");
  }
  else
  {
	Serial.println("Fuel Gauge failed to be put in sleep mode.");
  }
}
else
{
  Serial.println("Fuel Gauge is already in sleep mode.");
}

Exiting Seep Mode

The sample code below demonstrates how to check if the device is in sleep mode and then wakes it up if it is.

if (FuelGauge.isSleeping())
{
  FuelGauge.wake();

  if (!FuelGauge.isSleeping())
  {
	Serial.println("Fuel Gauge is now awake.");
  }
  else
  {
	Serial.println("Failed to wake Fuel Gauge.");
  }
}
else
{
  Serial.println("Fuel Gauge is already awake.");
}

Resetting the Device

Calling the reset() method causes the MAX17043/MAX17044 to completely reset as if power had been removed. The reset occurs when the last bit has been clocked in. The IC does not respond with an I2C ACK after this command sequence.

FuelGauge.reset();

Quick-Start

A quick-start allows the MAX17043/MAX17044 to restart fuel-gauge calculations in the same manner as initial power-up of the IC. For example, if an application’s power-up sequence is exceedingly noisy such that excess error is introduced into the IC’s “first guess” of SOC, the host can issue a quick-start to reduce the error. A quick-start is initiated by a rising edge on the QSTRT pin, or through software by calling the quickstart() method.

FuelGauge.quickstart();

Alert Threshold

The MAX17043/MAX17044 have an interrupt feature that alerts a host microprocessor whenever the cell's state of charge, as defined by the SOC register, falls below a predefined alert threshold set at address 0Dh of the CONFIG register. When an alert is triggered, the IC drives the ALRT pin to logic-low and sets the ALRT bit in the CONFIG register to logic 1. The ALRT pin remains logic-low until the host software writes the ALRT bit to logic 0 to clear the interrupt. Clearing the ALRT bit while SOC is below the alert threshold does not generate another interrupt. The SOC register must first rise above and then fall below the alert threshold value before another interrupt is generated. Note that the alert function is not disabled at IC powerup. If the first SOC calculation is below the threshold setting, an interrupt is generated. Entering Sleep mode does not clear the interrupt.

Check the Alert Status

The current status of the alert interrupt can be checked via software. This is done by calling the alertIsActive() method. See next section for an example of using this method.

Clear the Alert Status

The clearAlert() method can be called to clear the current alert bit in the config register.

if (FuelGauge.alertIsActive())
{
  FuelGauge.clearAlert();
}

Change the Alert Threshold

The alert threshold is a 5-bit value that sets the state of charge level where an interrupt is generated on the ALRT pin. The alert threshold has an LSb weight of 1% and can be programmed from 1% up to 32%. The power-up default value for ATHD is 4%.

The getThreshold() and setThresold() methods can be used to get the current threshold setting and to change the threshold setting.

// Decrease the threshold by 1
uint8_t threshold = FuelGauge.getThreshold();
FuelGauge.setThreshold(--threshold);

Portions of this document are taken directly from the Maxim data-sheet for the MAX17043/MAX17044. To see the full details the data sheet can be found at https://datasheets.maximintegrated.com/en/ds/MAX17043-MAX17044.pdf.

max1704x's People

Contributors

ewpa avatar porrey avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.