Skip to content

Commit 77109c8

Browse files
committed
Added Example2. Added more debug messages.
1 parent 058999c commit 77109c8

File tree

4 files changed

+127
-4
lines changed

4 files changed

+127
-4
lines changed

examples/Example1_Simple/Example1_Simple.ino

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ This code is released under the MIT license.
1919
Distributed as-is; no warranty is given.
2020
******************************************************************************/
2121

22+
#include <Wire.h> // Needed for I2C
23+
2224
#include <SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library.h> // Click here to get the library: http://librarymanager/All#SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library
2325

2426
SFE_MAX1704X lipo;
@@ -30,9 +32,21 @@ bool alert; // Variable to keep track of whether alert has been triggered
3032
void setup()
3133
{
3234
Serial.begin(115200); // Start serial, to output debug data
35+
while (!Serial)
36+
; //Wait for user to open terminal
37+
Serial.println(F("MAX17043 / MAX17044 Example"));
38+
39+
Wire.begin();
40+
41+
lipo.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial
3342

34-
// Set up the MAX17043 LiPo fuel gauge:
35-
lipo.begin(); // Initialize the MAX17043 LiPo fuel gauge
43+
// Set up the MAX17043 LiPo fuel gauge:
44+
if (lipo.begin() == false) // Connect to the MAX17043 using the default wire port
45+
{
46+
Serial.println(F("MAX17043 / MAX17044 not detected. Please check wiring. Freezing."));
47+
while (1)
48+
;
49+
}
3650

3751
// Quick start restarts the MAX17043 in hopes of getting a more accurate
3852
// guess for the SOC.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/******************************************************************************
2+
Example2_AlternatePorts
3+
By: Paul Clark
4+
Date: October 23rd 2020
5+
6+
Based extensively on:
7+
MAX17043_Simple_Serial.cpp
8+
SparkFun MAX17043 Example Code
9+
Jim Lindblom @ SparkFun Electronics
10+
Original Creation Date: June 22, 2015
11+
12+
This file demonstrates the simple API of the SparkFun MAX17043 Arduino library using non-standard Wire and Serial ports.
13+
14+
This example will print the gauge's voltage and state-of-charge (SOC) readings
15+
to serial (115200 baud)
16+
17+
This code is released under the MIT license.
18+
19+
Distributed as-is; no warranty is given.
20+
******************************************************************************/
21+
22+
#include <Wire.h> // Needed for I2C
23+
24+
#include <SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library.h> // Click here to get the library: http://librarymanager/All#SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library
25+
26+
SFE_MAX1704X lipo;
27+
28+
// Define our non-standard ports:
29+
#define mySerial Serial1
30+
TwoWire myWire(0);
31+
32+
double voltage = 0; // Variable to keep track of LiPo voltage
33+
double soc = 0; // Variable to keep track of LiPo state-of-charge (SOC)
34+
bool alert; // Variable to keep track of whether alert has been triggered
35+
36+
void setup()
37+
{
38+
mySerial.begin(115200); // Start serial, to output debug data
39+
while (!mySerial)
40+
; //Wait for user to open terminal
41+
mySerial.println(F("MAX17043 / MAX17044 Example"));
42+
43+
myWire.begin();
44+
45+
lipo.enableDebugging(mySerial); // Uncomment this line to enable helpful debug messages on non-standard serial
46+
47+
// Set up the MAX17043 LiPo fuel gauge:
48+
if (lipo.begin(myWire) == false) // Connect to the MAX17043 using non-standard wire port
49+
{
50+
mySerial.println(F("MAX17043 / MAX17044 not detected. Please check wiring. Freezing."));
51+
while (1)
52+
;
53+
}
54+
55+
// Quick start restarts the MAX17043 in hopes of getting a more accurate
56+
// guess for the SOC.
57+
lipo.quickStart();
58+
59+
// We can set an interrupt to alert when the battery SoC gets too low.
60+
// We can alert at anywhere between 1% - 32%:
61+
lipo.setThreshold(20); // Set alert threshold to 20%.
62+
}
63+
64+
void loop()
65+
{
66+
// lipo.getVoltage() returns a voltage value (e.g. 3.93)
67+
voltage = lipo.getVoltage();
68+
// lipo.getSOC() returns the estimated state of charge (e.g. 79%)
69+
soc = lipo.getSOC();
70+
// lipo.getAlert() returns a 0 or 1 (0=alert not triggered)
71+
alert = lipo.getAlert();
72+
73+
// Print the variables:
74+
mySerial.print("Voltage: ");
75+
mySerial.print(voltage); // Print the battery voltage
76+
mySerial.println(" V");
77+
78+
mySerial.print("Alert: ");
79+
mySerial.println(alert);
80+
81+
mySerial.print("Percentage: ");
82+
mySerial.print(soc); // Print the battery state of charge
83+
mySerial.println(" %");
84+
mySerial.println();
85+
86+
delay(500);
87+
}

src/SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,13 @@ uint8_t SFE_MAX1704X::sleep()
159159
// Read config reg, so we don't modify any other values:
160160
uint16_t configReg = read16(MAX17043_CONFIG);
161161
if (configReg & (1<<7))
162-
return 5; // Already sleeping, do nothing but return an error
162+
{
163+
if (_printDebug == true)
164+
{
165+
_debugPort->println(F("sleep: MAX17043 is already sleeping!"));
166+
}
167+
return MAX17043_GENERIC_ERROR; // Already sleeping, do nothing but return an error
168+
}
163169
configReg |= (1<<7); // Set sleep bit
164170

165171
return write16(configReg, MAX17043_CONFIG);
@@ -170,7 +176,13 @@ uint8_t SFE_MAX1704X::wake()
170176
// Read config reg, so we don't modify any other values:
171177
uint16_t configReg = read16(MAX17043_CONFIG);
172178
if (!(configReg & (1<<7)))
173-
return 5; // Already awake, do nothing but return an error
179+
{
180+
if (_printDebug == true)
181+
{
182+
_debugPort->println(F("sleep: MAX17043 is already awake!"));
183+
}
184+
return MAX17043_GENERIC_ERROR; // Already sleeping, do nothing but return an error
185+
}
174186
configReg &= ~(1<<7); // Clear sleep bit
175187

176188
return write16(configReg, MAX17043_CONFIG);

src/SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ Distributed as-is; no warranty is given.
6464
////////////////////////////////
6565
#define MAX17043_ADDRESS 0x36 // Unshifted I2C address. Becomes 0x6C for write and 0x6D for read.
6666

67+
// Generic error:
68+
// Wire.endTransmission will return:
69+
// 0:success
70+
// 1:data too long to fit in transmit buffer
71+
// 2:received NACK on transmit of address
72+
// 3:received NACK on transmit of data
73+
// 4:other error
74+
// So, let's use "5" as a generic error value
75+
#define MAX17043_GENERIC_ERROR 5
76+
6777
class SFE_MAX1704X
6878
{
6979
public:

0 commit comments

Comments
 (0)