Skip to content

Commit 35ae073

Browse files
committed
Added the full_scale and MAX17044 example.
1 parent 77109c8 commit 35ae073

File tree

6 files changed

+102
-8
lines changed

6 files changed

+102
-8
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ MAX17044 is configured for a dual-cell 2S pack.
88

99
This Arduino library provides support for both devices.
1010

11+
## Thanks
12+
13+
Parts of this library were based on / inspired by [Daniel Porrey's max1704x library](https://github.com/porrey/max1704x). Thank you Daniel.
14+
1115
## Repository Contents
1216

1317
- **/examples** - Example sketches for the library (.ino). Run these from the Arduino IDE.

examples/Example1_Simple/Example1_Simple.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void setup()
3434
Serial.begin(115200); // Start serial, to output debug data
3535
while (!Serial)
3636
; //Wait for user to open terminal
37-
Serial.println(F("MAX17043 / MAX17044 Example"));
37+
Serial.println(F("MAX17043 Example"));
3838

3939
Wire.begin();
4040

@@ -43,7 +43,7 @@ void setup()
4343
// Set up the MAX17043 LiPo fuel gauge:
4444
if (lipo.begin() == false) // Connect to the MAX17043 using the default wire port
4545
{
46-
Serial.println(F("MAX17043 / MAX17044 not detected. Please check wiring. Freezing."));
46+
Serial.println(F("MAX17043 not detected. Please check wiring. Freezing."));
4747
while (1)
4848
;
4949
}

examples/Example2_AlternatePorts/Example2_AlternatePorts.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void setup()
3838
mySerial.begin(115200); // Start serial, to output debug data
3939
while (!mySerial)
4040
; //Wait for user to open terminal
41-
mySerial.println(F("MAX17043 / MAX17044 Example"));
41+
mySerial.println(F("MAX17043 Example"));
4242

4343
myWire.begin();
4444

@@ -47,7 +47,7 @@ void setup()
4747
// Set up the MAX17043 LiPo fuel gauge:
4848
if (lipo.begin(myWire) == false) // Connect to the MAX17043 using non-standard wire port
4949
{
50-
mySerial.println(F("MAX17043 / MAX17044 not detected. Please check wiring. Freezing."));
50+
mySerial.println(F("MAX17043 not detected. Please check wiring. Freezing."));
5151
while (1)
5252
;
5353
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/******************************************************************************
2+
Example3_MAX17044
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 how to talk to the MAX17044 using the SparkFun MAX17043 Arduino library.
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(10); // The MAX17044 has a full-scale voltage of 10V. Specify it here. (int)
27+
28+
double voltage = 0; // Variable to keep track of LiPo voltage
29+
double soc = 0; // Variable to keep track of LiPo state-of-charge (SOC)
30+
bool alert; // Variable to keep track of whether alert has been triggered
31+
32+
void setup()
33+
{
34+
Serial.begin(115200); // Start serial, to output debug data
35+
while (!Serial)
36+
; //Wait for user to open terminal
37+
Serial.println(F("MAX17044 Example"));
38+
39+
Wire.begin();
40+
41+
lipo.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial
42+
43+
// Set up the MAX17044 LiPo fuel gauge:
44+
if (lipo.begin() == false) // Connect to the MAX17044 using the default wire port
45+
{
46+
Serial.println(F("MAX17044 not detected. Please check wiring. Freezing."));
47+
while (1)
48+
;
49+
}
50+
51+
// Quick start restarts the MAX17044 in hopes of getting a more accurate
52+
// guess for the SOC.
53+
lipo.quickStart();
54+
55+
// We can set an interrupt to alert when the battery SoC gets too low.
56+
// We can alert at anywhere between 1% - 32%:
57+
lipo.setThreshold(20); // Set alert threshold to 20%.
58+
}
59+
60+
void loop()
61+
{
62+
// lipo.getVoltage() returns a voltage value (e.g. 7.86)
63+
voltage = lipo.getVoltage();
64+
// lipo.getSOC() returns the estimated state of charge (e.g. 79%)
65+
soc = lipo.getSOC();
66+
// lipo.getAlert() returns a 0 or 1 (0=alert not triggered)
67+
alert = lipo.getAlert();
68+
69+
// Print the variables:
70+
Serial.print("Voltage: ");
71+
Serial.print(voltage); // Print the battery voltage
72+
Serial.println(" V");
73+
74+
Serial.print("Alert: ");
75+
Serial.println(alert);
76+
77+
Serial.print("Percentage: ");
78+
Serial.print(soc); // Print the battery state of charge
79+
Serial.println(" %");
80+
Serial.println();
81+
82+
delay(500);
83+
}

src/SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ Distributed as-is; no warranty is given.
2020
******************************************************************************/
2121
#include "SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library.h"
2222

23-
SFE_MAX1704X::SFE_MAX1704X()
23+
SFE_MAX1704X::SFE_MAX1704X(int full_scale)
2424
{
2525
// Constructor
26+
_full_scale = full_scale;
2627
}
2728

2829
boolean SFE_MAX1704X::begin(TwoWire &wirePort)
@@ -75,10 +76,14 @@ float SFE_MAX1704X::getVoltage()
7576
{
7677
uint16_t vCell;
7778
vCell = read16(MAX17043_VCELL);
78-
// vCell is a 12-bit register where each bit represents 1.25mV
79+
// vCell is a 12-bit register where each bit represents:
80+
// 1.25mV on the MAX17043
81+
// 2.5mV on the MAX17044
7982
vCell = (vCell) >> 4;
8083

81-
return ((float) vCell / 800.0);
84+
float divider = 4000 / _full_scale;
85+
86+
return (((float)vCell) / divider);
8287
}
8388

8489
float SFE_MAX1704X::getSOC()

src/SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Distributed as-is; no warranty is given.
7777
class SFE_MAX1704X
7878
{
7979
public:
80-
SFE_MAX1704X();
80+
SFE_MAX1704X(int full_scale = 5); // Default to a full-scale of 5V for the MAX17043. Select 10 for the MAX17044.
8181

8282
// begin() - Initializes the MAX17043.
8383
boolean begin(TwoWire &wirePort = Wire); //Returns true if module is detected
@@ -171,6 +171,8 @@ class SFE_MAX1704X
171171
Stream *_debugPort; //The stream to send debug messages to if enabled. Usually Serial.
172172
boolean _printDebug = false; //Flag to print debugging variables
173173

174+
int _full_scale = 5; // Default to a full-scale of 5V for the MAX17043. Select 10 for the MAX17044.
175+
174176
// write16([data], [address]) - Write 16 bits to the requested address. After
175177
// writing the address to be written, two sequential 8-bit writes will occur.
176178
// the msb is written first, then lsb.

0 commit comments

Comments
 (0)