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.
330const unsigned int BATTERY_CAPACITY = 850 ; // e.g. 850mAh battery
431
32+ // Arduino pin connected to BQ27441's GPOUT pin
533const int GPOUT_PIN = 2 ;
634
35+ // Set the integer percentage change that triggers an interrupt
36+ const int PERCENTAGE_INTERVAL = 1 ;
37+
738void 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
63110void 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
88135void setup ()
89136{
90- Serial.begin (115200 );
91- pinMode (GPOUT_PIN, INPUT_PULLUP);
92-
137+ Serial.begin (115200 );
93138 setupBQ27441 ();
94139}
95140
96141void 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