Skip to content

Commit b179a8d

Browse files
author
Jim Lindblom
committed
Adding GPOUT control and two examples.
1 parent a1d8512 commit b179a8d

File tree

6 files changed

+405
-61
lines changed

6 files changed

+405
-61
lines changed
+20-34
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,7 @@
1-
/******************************************************************************
2-
BQ27441_Basic.ino
3-
Basic BQ27441 Example Sketch
4-
Jim Lindblom @ SparkFun Electronics
5-
May 9, 2016
6-
https://github.com/sparkfun/SparkFun_BQ27441_Arduino_Library
7-
8-
This example demonstrates how to initialize a BQ27441, set the capacity of
9-
your LiPo battery, and read state-of-charge (SoC), voltage, current, and
10-
remaining capacity.
11-
12-
Hardware Resources:
13-
- Arduino Development Board
14-
- SparkFun Battery Babysitter
15-
16-
Development environment specifics:
17-
Arduino 1.6.7
18-
SparkFun Battery Babysitter v1.0
19-
Arduino Uno (any 'duino should do)
20-
******************************************************************************/
211
#include <SparkFunBQ27441.h>
222

23-
const int BATTERY_CAPACITY = 850; // e.g. 850mAh battery
24-
3+
const unsigned int BATTERY_CAPACITY = 850; // e.g. 850mAh battery
4+
unsigned int fullCapacity;
255
void setupBQ27441(void)
266
{
277
if (!lipo.begin())
@@ -34,22 +14,28 @@ void setupBQ27441(void)
3414
Serial.println("Connected to BQ27441!");
3515

3616
lipo.setCapacity(BATTERY_CAPACITY);
17+
fullCapacity = lipo.capacity(FULL);
3718
Serial.print("Full charge capacity set to: ");
38-
Serial.println(String(lipo.fullChargeCapacity()) + " mAh");
19+
Serial.println(String(fullCapacity) + " mAh");
3920
}
4021

4122
void printBatteryStats()
4223
{
43-
uint16_t soc = lipo.soc();
44-
uint16_t vRaw = lipo.voltage();
45-
float volts = lipo.convertVoltage(vRaw);
46-
int16_t current = lipo.current();
47-
uint16_t capacity = lipo.capacity();
48-
49-
String toPrint = String(soc) + " % |\t";
50-
toPrint += String(volts) + " V |\t";
51-
toPrint += String(current) + " mA |\t";
52-
toPrint += String(capacity) + " mAh";
24+
unsigned int soc = lipo.soc();
25+
unsigned int volts = lipo.voltage();
26+
int current = lipo.current(AVG);
27+
unsigned int capacity = lipo.capacity(REMAIN);
28+
int power = lipo.power();
29+
int health = lipo.soh();
30+
31+
String toPrint = String(soc) + "% | ";
32+
toPrint += String(volts) + " mV | ";
33+
toPrint += String(current) + " mA | ";
34+
toPrint += String(capacity) + " / ";
35+
toPrint += String(fullCapacity) + " mAh | ";
36+
toPrint += String(power) + " mW | ";
37+
toPrint += String(health) + "%";
38+
5339
Serial.println(toPrint);
5440
}
5541

@@ -63,4 +49,4 @@ void loop()
6349
{
6450
printBatteryStats();
6551
delay(1000);
66-
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <SparkFunBQ27441.h>
2+
3+
const unsigned int BATTERY_CAPACITY = 850; // e.g. 850mAh battery
4+
unsigned int fullCapacity;
5+
6+
void setupBQ27441(void)
7+
{
8+
if (!lipo.begin())
9+
{
10+
Serial.println("Error: Unable to communicate with BQ27441.");
11+
Serial.println(" Check wiring and try again.");
12+
Serial.println(" (Battery must be plugged into Battery Babysitter!)");
13+
while (1) ;
14+
}
15+
Serial.println("Connected to BQ27441!");
16+
17+
lipo.enterConfig();
18+
lipo.setGPOUTPolarity(HIGH); // active-high
19+
lipo.setGPOUTFunction(BAT_LOW); // BAT_LOW mode
20+
lipo.setSOC1Thresholds(20, 25);
21+
lipo.setSOCFThresholds(5, 10);
22+
lipo.exitConfig();
23+
24+
if (lipo.GPOUTPolarity())
25+
Serial.println("GPOUT set to active-HIGH");
26+
else
27+
Serial.println("GPOUT set to active-LOW");
28+
29+
if (lipo.GPOUTFunction())
30+
Serial.println("GPOUT function set to BAT_LOW");
31+
else
32+
Serial.println("GPOUT function set to SOC_INT");
33+
34+
Serial.println("SOC1 Set Threshold: " + String(lipo.SOC1SetThreshold()));
35+
Serial.println("SOC1 Clear Threshold: " + String(lipo.SOC1ClearThreshold()));
36+
Serial.println("SOCF Set Threshold: " + String(lipo.SOCFSetThreshold()));
37+
Serial.println("SOCF Clear Threshold: " + String(lipo.SOCFClearThreshold()));
38+
}
39+
40+
void printBatteryStats()
41+
{
42+
unsigned int soc = lipo.soc();
43+
unsigned int volts = lipo.voltage();
44+
int current = lipo.current(AVG);
45+
unsigned int capacity = lipo.capacity(REMAIN);
46+
int power = lipo.power();
47+
int health = lipo.soh();
48+
fullCapacity = lipo.capacity(FULL);
49+
50+
String toPrint = String(soc) + "% | ";
51+
toPrint += String(volts) + " mV | ";
52+
toPrint += String(current) + " mA | ";
53+
toPrint += String(capacity) + " / ";
54+
toPrint += String(fullCapacity) + " mAh | ";
55+
toPrint += String(power) + " mW | ";
56+
toPrint += String(health) + "%";
57+
58+
toPrint += " | 0x" + String(lipo.status(), HEX) + " | ";
59+
toPrint += "0x" + String(lipo.flags(), HEX) + " | ";
60+
61+
Serial.println(toPrint);
62+
}
63+
64+
void setup()
65+
{
66+
Serial.begin(115200);
67+
setupBQ27441();
68+
}
69+
70+
void loop()
71+
{
72+
printBatteryStats();
73+
delay(1000);
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include <SparkFunBQ27441.h>
2+
3+
const unsigned int BATTERY_CAPACITY = 850; // e.g. 850mAh battery
4+
5+
const int GPOUT_PIN = 2;
6+
7+
void setupBQ27441(void)
8+
{
9+
if (!lipo.begin())
10+
{
11+
Serial.println("Error: Unable to communicate with BQ27441.");
12+
Serial.println(" Check wiring and try again.");
13+
Serial.println(" (Battery must be plugged into Battery Babysitter!)");
14+
while (1) ;
15+
}
16+
Serial.println("Connected to BQ27441!");
17+
18+
lipo.enterConfig();
19+
lipo.setCapacity(BATTERY_CAPACITY);
20+
lipo.setGPOUTPolarity(LOW); // active-low
21+
lipo.setGPOUTFunction(SOC_INT); // SOC_INT mode
22+
lipo.setSOCIDelta(1);
23+
lipo.exitConfig();
24+
25+
if (lipo.GPOUTPolarity())
26+
Serial.println("GPOUT set to active-HIGH");
27+
else
28+
Serial.println("GPOUT set to active-LOW");
29+
30+
if (lipo.GPOUTFunction())
31+
Serial.println("GPOUT function set to BAT_LOW");
32+
else
33+
Serial.println("GPOUT function set to SOC_INT");
34+
35+
Serial.println("SOCI Delta: " + String(lipo.sociDelta()));
36+
Serial.println();
37+
38+
Serial.println("Testing GPOUT Pulse");
39+
lipo.pulseGPOUT();
40+
int timeout = 10000;
41+
while ((digitalRead(GPOUT_PIN)) && timeout--)
42+
delay(1);
43+
if (timeout > 0)
44+
{
45+
Serial.print("GPOUT test successful!");
46+
Serial.println("(" + String(10000 - timeout) + ")");
47+
Serial.print("GPOUT will pulse whenever the SoC ");
48+
Serial.println("value changes by SOCI delta.");
49+
Serial.print("Or when the battery changes from");
50+
Serial.println(" charging to discharging, or vice-versa.");
51+
Serial.println();
52+
}
53+
else
54+
{
55+
Serial.println("GPOUT didn't pulse.");
56+
Serial.print("Make sure it's connected to pin ");
57+
Serial.print(GPOUT_PIN);
58+
Serial.println(" and reset.");
59+
while (1) ;
60+
}
61+
}
62+
63+
void printBatteryStats()
64+
{
65+
unsigned int soc = lipo.soc();
66+
unsigned int volts = lipo.voltage();
67+
int current = lipo.current(AVG);
68+
unsigned int capacity = lipo.capacity(REMAIN);
69+
int power = lipo.power();
70+
int health = lipo.soh();
71+
unsigned int fullCapacity = lipo.capacity(FULL);
72+
73+
String toPrint = "[" + String(millis()/1000) + "] ";
74+
toPrint += String(soc) + "% | ";
75+
toPrint += String(volts) + " mV | ";
76+
toPrint += String(current) + " mA | ";
77+
toPrint += String(capacity) + " / ";
78+
toPrint += String(fullCapacity) + " mAh | ";
79+
toPrint += String(power) + " mW | ";
80+
toPrint += String(health) + "%";
81+
82+
toPrint += " | 0x" + String(lipo.status(), HEX) + " | ";
83+
toPrint += "0x" + String(lipo.flags(), HEX) + " | ";
84+
85+
Serial.println(toPrint);
86+
}
87+
88+
void setup()
89+
{
90+
Serial.begin(115200);
91+
pinMode(GPOUT_PIN, INPUT_PULLUP);
92+
93+
setupBQ27441();
94+
}
95+
96+
void loop()
97+
{
98+
if (digitalRead(GPOUT_PIN) == LOW)
99+
{
100+
printBatteryStats();
101+
}
102+
}

src/BQ27441_Definitions.h

+12-1
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,15 @@ Arduino Uno (any 'duino should do)
131131
#define BQ27441_ID_CALIB_DATA 104
132132
#define BQ27441_ID_CC_CAL 105
133133
#define BQ27441_ID_CURRENT 107
134-
#define BQ27441_ID_CODES 112
134+
#define BQ27441_ID_CODES 112
135+
136+
/////////////////////////////////////////
137+
// OpConfig Register - Bit Definitions //
138+
/////////////////////////////////////////
139+
#define BQ27441_OPCONFIG_BIE (1<<13)
140+
#define BQ27441_OPCONFIG_BI_PU_EN (1<<12)
141+
#define BQ27441_OPCONFIG_GPIOPOL (1<<11)
142+
#define BQ27441_OPCONFIG_SLEEP (1<<5)
143+
#define BQ27441_OPCONFIG_RMFCC (1<<4)
144+
#define BQ27441_OPCONFIG_BATLOWEN (1<<2)
145+
#define BQ27441_OPCONFIG_TEMPS (1<<0)

0 commit comments

Comments
 (0)