Skip to content

Commit 88abeea

Browse files
author
Jim Lindblom
committed
Comments en masse. Library clean up.
1 parent b179a8d commit 88abeea

File tree

8 files changed

+882
-244
lines changed

8 files changed

+882
-244
lines changed

examples/BQ27441_Basic/BQ27441_Basic.ino

+40-11
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,62 @@
1+
/******************************************************************************
2+
BQ27441_Basic
3+
BQ27441 Library Basic Example
4+
Jim Lindblom @ SparkFun Electronics
5+
May 9, 2016
6+
https://github.com/sparkfun/SparkFun_BQ27441_Arduino_Library
7+
8+
Demonstrates how to set up the BQ27441 and read state-of-charge (soc),
9+
battery voltage, average current, remaining capacity, average power, and
10+
state-of-health (soh).
11+
12+
After uploading, open up the serial monitor to 115200 baud to view your
13+
battery's stats.
14+
15+
Hardware Resources:
16+
- Arduino Development Board
17+
- SparkFun Battery Babysitter
18+
19+
Development environment specifics:
20+
Arduino 1.6.7
21+
SparkFun Battery Babysitter v1.0
22+
Arduino Uno (any 'duino should do)
23+
******************************************************************************/
124
#include <SparkFunBQ27441.h>
225

26+
// Set BATTERY_CAPACITY to the design capacity of your battery.
327
const unsigned int BATTERY_CAPACITY = 850; // e.g. 850mAh battery
4-
unsigned int fullCapacity;
28+
529
void setupBQ27441(void)
630
{
7-
if (!lipo.begin())
31+
// Use lipo.begin() to initialize the BQ27441-G1A and confirm that it's
32+
// connected and communicating.
33+
if (!lipo.begin()) // begin() will return true if communication is successful
834
{
35+
// If communication fails, print an error message and loop forever.
936
Serial.println("Error: Unable to communicate with BQ27441.");
1037
Serial.println(" Check wiring and try again.");
1138
Serial.println(" (Battery must be plugged into Battery Babysitter!)");
1239
while (1) ;
1340
}
1441
Serial.println("Connected to BQ27441!");
1542

43+
// Uset lipo.setCapacity(BATTERY_CAPACITY) to set the design capacity
44+
// of your battery.
1645
lipo.setCapacity(BATTERY_CAPACITY);
17-
fullCapacity = lipo.capacity(FULL);
18-
Serial.print("Full charge capacity set to: ");
19-
Serial.println(String(fullCapacity) + " mAh");
2046
}
2147

2248
void printBatteryStats()
2349
{
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();
50+
// Read battery stats from the BQ27441-G1A
51+
unsigned int soc = lipo.soc(); // Read state-of-charge (%)
52+
unsigned int volts = lipo.voltage(); // Read battery voltage (mV)
53+
int current = lipo.current(AVG); // Read average current (mA)
54+
unsigned int fullCapacity = lipo.capacity(FULL); // Read full capacity (mAh)
55+
unsigned int capacity = lipo.capacity(REMAIN); // Read remaining capacity (mAh)
56+
int power = lipo.power(); // Read average power draw (mW)
57+
int health = lipo.soh(); // Read state-of-health (%)
3058

59+
// Now print out those values:
3160
String toPrint = String(soc) + "% | ";
3261
toPrint += String(volts) + " mV | ";
3362
toPrint += String(current) + " mA | ";
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,81 @@
1+
/******************************************************************************
2+
BQ27441_GPOUT_BAT_LOW
3+
BQ27441 Library GPOUT Example - Battery low Mode
4+
Jim Lindblom @ SparkFun Electronics
5+
May 9, 2016
6+
https://github.com/sparkfun/SparkFun_BQ27441_Arduino_Library
7+
8+
Demonstrates how to use the BQ27441's BAT_LOW function on GPOUT. In this mode
9+
GPOUT will become active whenever the battery goes below a set threshold.
10+
11+
In addition to I2C, connect GPOUT to pin 2 of your Arduino.
12+
13+
After uploading, open up the serial monitor to 115200 baud to view your
14+
battery's stats. They should only print when the percentage goes up or down
15+
by 1%.
16+
17+
Hardware Resources:
18+
- Arduino Development Board
19+
- SparkFun Battery Babysitter
20+
21+
Development environment specifics:
22+
Arduino 1.6.7
23+
SparkFun Battery Babysitter v1.0
24+
Arduino Uno (any 'duino should do)
25+
******************************************************************************/
126
#include <SparkFunBQ27441.h>
227

28+
// Set BATTERY_CAPACITY to the design capacity of your battery.
329
const unsigned int BATTERY_CAPACITY = 850; // e.g. 850mAh battery
4-
unsigned int fullCapacity;
30+
31+
const byte SOCI_SET = 15; // Interrupt set threshold at 20%
32+
const byte SOCI_CLR = 20; // Interrupt clear threshold at 25%
33+
const byte SOCF_SET = 5; // Final threshold set at 5%
34+
const byte SOCF_CLR = 10; // Final threshold clear at 10%
35+
36+
// Arduino pin connected to BQ27441's GPOUT pin
37+
const int GPOUT_PIN = 2;
538

639
void setupBQ27441(void)
740
{
8-
if (!lipo.begin())
41+
pinMode(GPOUT_PIN, INPUT_PULLUP); // Set the GPOUT pin as an input w/ pullup
42+
43+
// Use lipo.begin() to initialize the BQ27441-G1A and confirm that it's
44+
// connected and communicating.
45+
if (!lipo.begin()) // begin() will return true if communication is successful
946
{
47+
// If communication fails, print an error message and loop forever.
1048
Serial.println("Error: Unable to communicate with BQ27441.");
1149
Serial.println(" Check wiring and try again.");
1250
Serial.println(" (Battery must be plugged into Battery Babysitter!)");
1351
while (1) ;
1452
}
1553
Serial.println("Connected to BQ27441!");
1654

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);
55+
// In this example, we'll manually enter and exit config mode. By controlling
56+
// config mode manually, you can set the chip up faster -- completing all of
57+
// the set up in a single config mode sweep.
58+
lipo.enterConfig(); // To configure the values below, you must be in config mode
59+
lipo.setCapacity(BATTERY_CAPACITY); // Set the battery capacity
60+
lipo.setGPOUTPolarity(LOW); // Set GPOUT to active-high
61+
lipo.setGPOUTFunction(BAT_LOW); // Set GPOUT to BAT_LOW mode
62+
lipo.setSOC1Thresholds(SOCI_SET, SOCI_CLR); // Set SOCI set and clear thresholds
63+
lipo.setSOCFThresholds(SOCF_SET, SOCF_CLR); // Set SOCF set and clear thresholds
2264
lipo.exitConfig();
2365

66+
// Use lipo.GPOUTPolarity to read from the chip and confirm the changes
2467
if (lipo.GPOUTPolarity())
2568
Serial.println("GPOUT set to active-HIGH");
2669
else
2770
Serial.println("GPOUT set to active-LOW");
2871

72+
// Use lipo.GPOUTFunction to confirm the functionality of GPOUT
2973
if (lipo.GPOUTFunction())
3074
Serial.println("GPOUT function set to BAT_LOW");
3175
else
3276
Serial.println("GPOUT function set to SOC_INT");
3377

78+
// Read the set and clear thresholds to make sure they were set correctly
3479
Serial.println("SOC1 Set Threshold: " + String(lipo.SOC1SetThreshold()));
3580
Serial.println("SOC1 Clear Threshold: " + String(lipo.SOC1ClearThreshold()));
3681
Serial.println("SOCF Set Threshold: " + String(lipo.SOCFSetThreshold()));
@@ -39,25 +84,26 @@ void setupBQ27441(void)
3984

4085
void printBatteryStats()
4186
{
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);
87+
// Read battery stats from the BQ27441-G1A
88+
unsigned int soc = lipo.soc(); // Read state-of-charge (%)
89+
unsigned int volts = lipo.voltage(); // Read battery voltage (mV)
90+
int current = lipo.current(AVG); // Read average current (mA)
91+
unsigned int fullCapacity = lipo.capacity(FULL); // Read full capacity (mAh)
92+
unsigned int capacity = lipo.capacity(REMAIN); // Read remaining capacity (mAh)
93+
int power = lipo.power(); // Read average power draw (mW)
94+
int health = lipo.soh(); // Read state-of-health (%)
4995

96+
// Assemble a string to print
5097
String toPrint = String(soc) + "% | ";
5198
toPrint += String(volts) + " mV | ";
5299
toPrint += String(current) + " mA | ";
53100
toPrint += String(capacity) + " / ";
54101
toPrint += String(fullCapacity) + " mAh | ";
55102
toPrint += String(power) + " mW | ";
56103
toPrint += String(health) + "%";
57-
58-
toPrint += " | 0x" + String(lipo.status(), HEX) + " | ";
59-
toPrint += "0x" + String(lipo.flags(), HEX) + " | ";
104+
toPrint += " | 0x" + String(lipo.flags(), HEX);
60105

106+
// Print the string
61107
Serial.println(toPrint);
62108
}
63109

@@ -70,5 +116,15 @@ void setup()
70116
void loop()
71117
{
72118
printBatteryStats();
119+
120+
// If the GPOUT interrupt is active (low)
121+
if (!digitalRead(GPOUT_PIN))
122+
{
123+
// Check which of the flags triggered the interrupt
124+
if (lipo.socfFlag())
125+
Serial.println("<!-- WARNING: Battery Dangerously low -->");
126+
else if (lipo.socFlag())
127+
Serial.println("<!-- WARNING: Battery Low -->");
128+
}
73129
delay(1000);
74-
}
130+
}
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,92 @@
1+
/******************************************************************************
2+
BQ27441_GPOUT_SOC_INT
3+
BQ27441 Library GPOUT Example - State-of-charge interval mode
4+
Jim Lindblom @ SparkFun Electronics
5+
May 9, 2016
6+
https://github.com/sparkfun/SparkFun_BQ27441_Arduino_Library
7+
8+
Demonstrates how to use the BQ27441's SOC_INT function on GPOUT. In this mode
9+
GPOUT will pulse every time the state-of-charge (soc) goes up or down by a
10+
set percentage integer.
11+
12+
In addition to I2C, connect GPOUT to pin 2 of your Arduino.
13+
14+
After uploading, open up the serial monitor to 115200 baud to view your
15+
battery's stats. They should only print when the percentage goes up or down
16+
by 1%.
17+
18+
Hardware Resources:
19+
- Arduino Development Board
20+
- SparkFun Battery Babysitter
21+
22+
Development environment specifics:
23+
Arduino 1.6.7
24+
SparkFun Battery Babysitter v1.0
25+
Arduino Uno (any 'duino should do)
26+
******************************************************************************/
127
#include <SparkFunBQ27441.h>
228

29+
// Set BATTERY_CAPACITY to the design capacity of your battery.
330
const unsigned int BATTERY_CAPACITY = 850; // e.g. 850mAh battery
431

32+
// Arduino pin connected to BQ27441's GPOUT pin
533
const int GPOUT_PIN = 2;
634

35+
// Set the integer percentage change that triggers an interrupt
36+
const int PERCENTAGE_INTERVAL = 1;
37+
738
void setupBQ27441(void)
839
{
9-
if (!lipo.begin())
40+
pinMode(GPOUT_PIN, INPUT_PULLUP); // Set the GPOUT pin as an input w/ pullup
41+
42+
// Use lipo.begin() to initialize the BQ27441-G1A and confirm that it's
43+
// connected and communicating.
44+
if (!lipo.begin()) // begin() will return true if communication is successful
1045
{
46+
// If communication fails, print an error message and loop forever.
1147
Serial.println("Error: Unable to communicate with BQ27441.");
1248
Serial.println(" Check wiring and try again.");
1349
Serial.println(" (Battery must be plugged into Battery Babysitter!)");
1450
while (1) ;
1551
}
1652
Serial.println("Connected to BQ27441!");
1753

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();
54+
// In this example, we'll manually enter and exit config mode. By controlling
55+
// config mode manually, you can set the chip up faster -- completing all of
56+
// the set up in a single config mode sweep.
57+
lipo.enterConfig(); // To configure the values below, you must be in config mode
58+
lipo.setCapacity(BATTERY_CAPACITY); // Set the battery capacity
59+
lipo.setGPOUTPolarity(LOW); // Set GPOUT to active-low
60+
lipo.setGPOUTFunction(SOC_INT); // Set GPOUT to SOC_INT mode
61+
lipo.setSOCIDelta(PERCENTAGE_INTERVAL); // Set percentage change integer
62+
lipo.exitConfig(); // Exit config mode to save changes
2463

64+
// Use lipo.GPOUTPolarity to read from the chip and confirm the changes
2565
if (lipo.GPOUTPolarity())
2666
Serial.println("GPOUT set to active-HIGH");
2767
else
2868
Serial.println("GPOUT set to active-LOW");
2969

70+
// Use lipo.GPOUTFunction to confirm the functionality of GPOUT
3071
if (lipo.GPOUTFunction())
3172
Serial.println("GPOUT function set to BAT_LOW");
3273
else
3374
Serial.println("GPOUT function set to SOC_INT");
34-
75+
76+
// Read lipo.sociDelta() to confirm the integer change value
3577
Serial.println("SOCI Delta: " + String(lipo.sociDelta()));
3678
Serial.println();
3779

80+
// Use lipo.pulseGPOUT() to trigger a pulse on GPOUT. This only works
81+
// in SOC_INT mode.
3882
Serial.println("Testing GPOUT Pulse");
3983
lipo.pulseGPOUT();
40-
int timeout = 10000;
84+
int timeout = 10000; // The pulse can take a while to occur. Set max to 10s
4185
while ((digitalRead(GPOUT_PIN)) && timeout--)
42-
delay(1);
86+
delay(1); // Wait for GPOUT to go high, or timeout to occur
4387
if (timeout > 0)
4488
{
89+
// If GPOUT pulsed, print success message.
4590
Serial.print("GPOUT test successful!");
4691
Serial.println("(" + String(10000 - timeout) + ")");
4792
Serial.print("GPOUT will pulse whenever the SoC ");
@@ -52,6 +97,8 @@ void setupBQ27441(void)
5297
}
5398
else
5499
{
100+
// If GPOUT didn't pulse, something went wrong. Print error message
101+
// and loop forever.
55102
Serial.println("GPOUT didn't pulse.");
56103
Serial.print("Make sure it's connected to pin ");
57104
Serial.print(GPOUT_PIN);
@@ -62,14 +109,16 @@ void setupBQ27441(void)
62109

63110
void printBatteryStats()
64111
{
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);
112+
// Read battery stats from the BQ27441-G1A
113+
unsigned int soc = lipo.soc(); // Read state-of-charge (%)
114+
unsigned int volts = lipo.voltage(); // Read battery voltage (mV)
115+
int current = lipo.current(AVG); // Read average current (mA)
116+
unsigned int fullCapacity = lipo.capacity(FULL); // Read full capacity (mAh)
117+
unsigned int capacity = lipo.capacity(REMAIN); // Read remaining capacity (mAh)
118+
int power = lipo.power(); // Read average power draw (mW)
119+
int health = lipo.soh(); // Read state-of-health (%)
72120

121+
// Assemble a string to print
73122
String toPrint = "[" + String(millis()/1000) + "] ";
74123
toPrint += String(soc) + "% | ";
75124
toPrint += String(volts) + " mV | ";
@@ -78,25 +127,23 @@ void printBatteryStats()
78127
toPrint += String(fullCapacity) + " mAh | ";
79128
toPrint += String(power) + " mW | ";
80129
toPrint += String(health) + "%";
81-
82-
toPrint += " | 0x" + String(lipo.status(), HEX) + " | ";
83-
toPrint += "0x" + String(lipo.flags(), HEX) + " | ";
84130

131+
// Print the string
85132
Serial.println(toPrint);
86133
}
87134

88135
void setup()
89136
{
90-
Serial.begin(115200);
91-
pinMode(GPOUT_PIN, INPUT_PULLUP);
92-
137+
Serial.begin(115200);
93138
setupBQ27441();
94139
}
95140

96141
void loop()
97142
{
143+
// The interrupt is set to active-low. If the GPOUT pin goes low...
98144
if (digitalRead(GPOUT_PIN) == LOW)
99145
{
146+
// ...SOC_INT occurred. Print battery stats.
100147
printBatteryStats();
101148
}
102149
}

0 commit comments

Comments
 (0)