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
+ ******************************************************************************/
1
27
#include < SparkFunBQ27441.h>
2
28
29
+ // Set BATTERY_CAPACITY to the design capacity of your battery.
3
30
const unsigned int BATTERY_CAPACITY = 850 ; // e.g. 850mAh battery
4
31
32
+ // Arduino pin connected to BQ27441's GPOUT pin
5
33
const int GPOUT_PIN = 2 ;
6
34
35
+ // Set the integer percentage change that triggers an interrupt
36
+ const int PERCENTAGE_INTERVAL = 1 ;
37
+
7
38
void setupBQ27441 (void )
8
39
{
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
10
45
{
46
+ // If communication fails, print an error message and loop forever.
11
47
Serial.println (" Error: Unable to communicate with BQ27441." );
12
48
Serial.println (" Check wiring and try again." );
13
49
Serial.println (" (Battery must be plugged into Battery Babysitter!)" );
14
50
while (1 ) ;
15
51
}
16
52
Serial.println (" Connected to BQ27441!" );
17
53
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
24
63
64
+ // Use lipo.GPOUTPolarity to read from the chip and confirm the changes
25
65
if (lipo.GPOUTPolarity ())
26
66
Serial.println (" GPOUT set to active-HIGH" );
27
67
else
28
68
Serial.println (" GPOUT set to active-LOW" );
29
69
70
+ // Use lipo.GPOUTFunction to confirm the functionality of GPOUT
30
71
if (lipo.GPOUTFunction ())
31
72
Serial.println (" GPOUT function set to BAT_LOW" );
32
73
else
33
74
Serial.println (" GPOUT function set to SOC_INT" );
34
-
75
+
76
+ // Read lipo.sociDelta() to confirm the integer change value
35
77
Serial.println (" SOCI Delta: " + String (lipo.sociDelta ()));
36
78
Serial.println ();
37
79
80
+ // Use lipo.pulseGPOUT() to trigger a pulse on GPOUT. This only works
81
+ // in SOC_INT mode.
38
82
Serial.println (" Testing GPOUT Pulse" );
39
83
lipo.pulseGPOUT ();
40
- int timeout = 10000 ;
84
+ int timeout = 10000 ; // The pulse can take a while to occur. Set max to 10s
41
85
while ((digitalRead (GPOUT_PIN)) && timeout--)
42
- delay (1 );
86
+ delay (1 ); // Wait for GPOUT to go high, or timeout to occur
43
87
if (timeout > 0 )
44
88
{
89
+ // If GPOUT pulsed, print success message.
45
90
Serial.print (" GPOUT test successful!" );
46
91
Serial.println (" (" + String (10000 - timeout) + " )" );
47
92
Serial.print (" GPOUT will pulse whenever the SoC " );
@@ -52,6 +97,8 @@ void setupBQ27441(void)
52
97
}
53
98
else
54
99
{
100
+ // If GPOUT didn't pulse, something went wrong. Print error message
101
+ // and loop forever.
55
102
Serial.println (" GPOUT didn't pulse." );
56
103
Serial.print (" Make sure it's connected to pin " );
57
104
Serial.print (GPOUT_PIN);
@@ -62,14 +109,16 @@ void setupBQ27441(void)
62
109
63
110
void printBatteryStats ()
64
111
{
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 (%)
72
120
121
+ // Assemble a string to print
73
122
String toPrint = " [" + String (millis ()/1000 ) + " ] " ;
74
123
toPrint += String (soc) + " % | " ;
75
124
toPrint += String (volts) + " mV | " ;
@@ -78,25 +127,23 @@ void printBatteryStats()
78
127
toPrint += String (fullCapacity) + " mAh | " ;
79
128
toPrint += String (power) + " mW | " ;
80
129
toPrint += String (health) + " %" ;
81
-
82
- toPrint += " | 0x" + String (lipo.status (), HEX) + " | " ;
83
- toPrint += " 0x" + String (lipo.flags (), HEX) + " | " ;
84
130
131
+ // Print the string
85
132
Serial.println (toPrint);
86
133
}
87
134
88
135
void setup ()
89
136
{
90
- Serial.begin (115200 );
91
- pinMode (GPOUT_PIN, INPUT_PULLUP);
92
-
137
+ Serial.begin (115200 );
93
138
setupBQ27441 ();
94
139
}
95
140
96
141
void loop ()
97
142
{
143
+ // The interrupt is set to active-low. If the GPOUT pin goes low...
98
144
if (digitalRead (GPOUT_PIN) == LOW)
99
145
{
146
+ // ...SOC_INT occurred. Print battery stats.
100
147
printBatteryStats ();
101
148
}
102
149
}
0 commit comments