Skip to content

Commit ef11ef1

Browse files
committed
Add extended configuration parameters and flags
- Updated to version 1.0.2 - Added ability to configure DesingEnergy, TaperRate and TerminationVoltage for more accurate gauging - Added ability to monitor ITPOR flag so configuration can be applied only when needed - Added ability to monitor discharge and full charge flags - Added example sketch demonstrating all of the above
1 parent 5ca0dbe commit ef11ef1

File tree

4 files changed

+229
-1
lines changed

4 files changed

+229
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include <SparkFunBQ27441.h>
2+
3+
// Set BATTERY_CAPACITY to the design capacity of your battery in mAh.
4+
const uint16_t BATTERY_CAPACITY = 2850;
5+
6+
//lowest operational voltage in mV
7+
const uint16_t TERMINATE_VOLTAGE = 3000;
8+
9+
//current at which charger stops charging battery in mA
10+
//in case of Sparkfun Battery Babysitter board:
11+
// 100mA charge current --> 12mA
12+
// 500mA charge current --> 60mA
13+
const uint16_t TAPER_CURRENT = 60;
14+
15+
void setup()
16+
{
17+
Serial.begin(115200);
18+
19+
if (!lipo.begin()) // begin() will return true if communication is successful
20+
{
21+
// If communication fails, print an error message and loop forever.
22+
Serial.println("Error: Unable to communicate with BQ27441.");
23+
Serial.println(" Check wiring and try again.");
24+
Serial.println(" (Battery must be plugged into Battery Babysitter!)");
25+
while (1)
26+
;
27+
}
28+
Serial.println("Connected to BQ27441!");
29+
30+
if (lipo.itporFlag()) //write config parameters only if needed
31+
{
32+
Serial.println("Writing gague config");
33+
34+
lipo.enterConfig(); // To configure the values below, you must be in config mode
35+
lipo.setCapacity(BATTERY_CAPACITY); // Set the battery capacity
36+
37+
/*
38+
Design Energy should be set to be Design Capacity × 3.7 if using the bq27441-G1A or Design
39+
Capacity × 3.8 if using the bq27441-G1B
40+
*/
41+
lipo.setDesignEnergy(BATTERY_CAPACITY * 3.7f);
42+
43+
/*
44+
Terminate Voltage should be set to the minimum operating voltage of your system. This is the target
45+
where the gauge typically reports 0% capacity
46+
*/
47+
lipo.setTerminateVoltage(TERMINATE_VOLTAGE);
48+
49+
/*
50+
Taper Rate = Design Capacity / (0.1 * Taper Current)
51+
*/
52+
lipo.setTaperRate(10 * BATTERY_CAPACITY / TAPER_CURRENT);
53+
54+
lipo.exitConfig(); // Exit config mode to save changes
55+
}
56+
else
57+
{
58+
Serial.println("Using existing gague config");
59+
}
60+
}
61+
62+
void printBatteryStats()
63+
{
64+
// Read battery stats from the BQ27441-G1A
65+
unsigned int soc = lipo.soc(); // Read state-of-charge (%)
66+
unsigned int volts = lipo.voltage(); // Read battery voltage (mV)
67+
int current = lipo.current(AVG); // Read average current (mA)
68+
unsigned int fullCapacity = lipo.capacity(FULL); // Read full capacity (mAh)
69+
unsigned int capacity = lipo.capacity(REMAIN); // Read remaining capacity (mAh)
70+
int power = lipo.power(); // Read average power draw (mW)
71+
int health = lipo.soh(); // Read state-of-health (%)
72+
73+
// Assemble a string to print
74+
String toPrint = "[" + String(millis() / 1000) + "] ";
75+
toPrint += String(soc) + "% | ";
76+
toPrint += String(volts) + " mV | ";
77+
toPrint += String(current) + " mA | ";
78+
toPrint += String(capacity) + " / ";
79+
toPrint += String(fullCapacity) + " mAh | ";
80+
toPrint += String(power) + " mW | ";
81+
toPrint += String(health) + "%";
82+
83+
//fast charging allowed
84+
if (lipo.chgFlag())
85+
toPrint += " CHG";
86+
87+
//full charge detected
88+
if (lipo.fcFlag())
89+
toPrint += " FC";
90+
91+
//battery is discharging
92+
if (lipo.dsgFlag())
93+
toPrint += " DSG";
94+
95+
// Print the string
96+
Serial.println(toPrint);
97+
}
98+
99+
void loop()
100+
{
101+
printBatteryStats();
102+
delay(1000);
103+
}

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SparkFun BQ72441 LiPo Fuel Gauge Arduino Library
2-
version=1.0.1
2+
version=1.0.2
33
author=SparkFun Electronics
44
maintainer=SparkFun Electronics
55
sentence=An Arduino library for interfacing with the BQ72441-G1 LiPo Fuel Gauge

src/SparkFunBQ27441.cpp

+78
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,58 @@ bool BQ27441::setCapacity(uint16_t capacity)
5353
// Write to STATE subclass (82) of BQ27441 extended memory.
5454
// Offset 0x0A (10)
5555
// Design capacity is a 2-byte piece of data - MSB first
56+
// Unit: mAh
5657
uint8_t capMSB = capacity >> 8;
5758
uint8_t capLSB = capacity & 0x00FF;
5859
uint8_t capacityData[2] = {capMSB, capLSB};
5960
return writeExtendedData(BQ27441_ID_STATE, 10, capacityData, 2);
6061
}
6162

63+
// Configures the design energy of the connected battery.
64+
bool BQ27441::setDesignEnergy(uint16_t energy)
65+
{
66+
// Write to STATE subclass (82) of BQ27441 extended memory.
67+
// Offset 0x0C (12)
68+
// Design energy is a 2-byte piece of data - MSB first
69+
// Unit: mWh
70+
uint8_t enMSB = energy >> 8;
71+
uint8_t enLSB = energy & 0x00FF;
72+
uint8_t energyData[2] = {enMSB, enLSB};
73+
return writeExtendedData(BQ27441_ID_STATE, 12, energyData, 2);
74+
}
75+
76+
// Configures the terminate voltage.
77+
bool BQ27441::setTerminateVoltage(uint16_t voltage)
78+
{
79+
// Write to STATE subclass (82) of BQ27441 extended memory.
80+
// Offset 0x0F (16)
81+
// Termiante voltage is a 2-byte piece of data - MSB first
82+
// Unit: mV
83+
// Min 2500, Max 3700
84+
if(voltage<2500) voltage=2500;
85+
if(voltage>3700) voltage=3700;
86+
87+
uint8_t tvMSB = voltage >> 8;
88+
uint8_t tvLSB = voltage & 0x00FF;
89+
uint8_t tvData[2] = {tvMSB, tvLSB};
90+
return writeExtendedData(BQ27441_ID_STATE, 16, tvData, 2);
91+
}
92+
93+
// Configures taper rate of connected battery.
94+
bool BQ27441::setTaperRate(uint16_t rate)
95+
{
96+
// Write to STATE subclass (82) of BQ27441 extended memory.
97+
// Offset 0x1B (27)
98+
// Termiante voltage is a 2-byte piece of data - MSB first
99+
// Unit: 0.1h
100+
// Max 2000
101+
if(rate>2000) rate=2000;
102+
uint8_t trMSB = rate >> 8;
103+
uint8_t trLSB = rate & 0x00FF;
104+
uint8_t trData[2] = {trMSB, trLSB};
105+
return writeExtendedData(BQ27441_ID_STATE, 27, trData, 2);
106+
}
107+
62108
/*****************************************************************************
63109
********************** Battery Characteristics Functions ********************
64110
*****************************************************************************/
@@ -296,6 +342,38 @@ bool BQ27441::socfFlag(void)
296342

297343
}
298344

345+
// Check if the ITPOR flag is set
346+
bool BQ27441::itporFlag(void)
347+
{
348+
uint16_t flagState = flags();
349+
350+
return flagState & BQ27441_FLAG_ITPOR;
351+
}
352+
353+
// Check if the FC flag is set
354+
bool BQ27441::fcFlag(void)
355+
{
356+
uint16_t flagState = flags();
357+
358+
return flagState & BQ27441_FLAG_FC;
359+
}
360+
361+
// Check if the CHG flag is set
362+
bool BQ27441::chgFlag(void)
363+
{
364+
uint16_t flagState = flags();
365+
366+
return flagState & BQ27441_FLAG_CHG;
367+
}
368+
369+
// Check if the DSG flag is set
370+
bool BQ27441::dsgFlag(void)
371+
{
372+
uint16_t flagState = flags();
373+
374+
return flagState & BQ27441_FLAG_DSG;
375+
}
376+
299377
// Get the SOC_INT interval delta
300378
uint8_t BQ27441::sociDelta(void)
301379
{

src/SparkFunBQ27441.h

+47
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,24 @@ class BQ27441 {
9696
*/
9797
bool setCapacity(uint16_t capacity);
9898

99+
/**
100+
Configures the design energy of the connected battery.
101+
102+
@param energy of battery (unsigned 16-bit value)
103+
@return true if energy successfully set.
104+
*/
105+
bool setDesignEnergy(uint16_t energy);
106+
107+
/**
108+
Configures terminate voltage (lowest operational voltage of battery powered circuit)
109+
110+
@param voltage of battery (unsigned 16-bit value)
111+
@return true if energy successfully set.
112+
*/
113+
bool setTerminateVoltage(uint16_t voltage);
114+
115+
bool setTaperRate(uint16_t rate);
116+
99117
/////////////////////////////
100118
// Battery Characteristics //
101119
/////////////////////////////
@@ -244,6 +262,35 @@ class BQ27441 {
244262
*/
245263
bool socfFlag(void);
246264

265+
/**
266+
Check if the ITPOR flag is set in flags()
267+
268+
@return true if flag is set
269+
*/
270+
bool itporFlag(void);
271+
272+
/**
273+
Check if the FC flag is set in flags()
274+
275+
@return true if flag is set
276+
*/
277+
bool fcFlag(void);
278+
279+
/**
280+
Check if the CHG flag is set in flags()
281+
282+
@return true if flag is set
283+
*/
284+
bool chgFlag(void);
285+
286+
/**
287+
Check if the DSG flag is set in flags()
288+
289+
@return true if flag is set
290+
*/
291+
bool dsgFlag(void);
292+
293+
247294
/**
248295
Get the SOC_INT interval delta
249296

0 commit comments

Comments
 (0)