Skip to content

Commit efc649a

Browse files
author
Rob
committed
Adds MAX17048 battery monitor support.
1 parent 293fd78 commit efc649a

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

src/batterymonitor.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,17 @@ void BatteryMonitor::Setup()
4343
m_Logger.error("MCP3021 not found on I2C bus");
4444
}
4545
#endif
46+
#if BATTERY_MONITOR == BAT_MAX17048
47+
if (address == 0)
48+
{
49+
m_Logger.error("MAX17048 not found on I2C bus");
50+
}
51+
#endif
4652
}
4753

4854
void BatteryMonitor::Loop()
4955
{
50-
#if BATTERY_MONITOR == BAT_EXTERNAL || BATTERY_MONITOR == BAT_INTERNAL || BATTERY_MONITOR == BAT_MCP3021 || BATTERY_MONITOR == BAT_INTERNAL_MCP3021
56+
#if BATTERY_MONITOR == BAT_EXTERNAL || BATTERY_MONITOR == BAT_INTERNAL || BATTERY_MONITOR == BAT_MCP3021 || BATTERY_MONITOR == BAT_INTERNAL_MCP3021 || BATTERY_MONITOR == BAT_MAX17048
5157
auto now_ms = millis();
5258
if (now_ms - last_battery_sample >= batterySampleRate)
5359
{
@@ -99,6 +105,20 @@ void BatteryMonitor::Loop()
99105
}
100106
}
101107
#endif
108+
#if BATTERY_MONITOR == BAT_MAX17048
109+
if (address > 0)
110+
{
111+
// Cell voltage register address 0x02
112+
uint16_t data;
113+
auto status = I2Cdev::readWord(address, 0x02, &data);
114+
if (status == 0)
115+
{
116+
float v = data * (78.125 / 1000000);
117+
voltage = (voltage > 0) ? min(voltage, v) : v;
118+
m_Logger.info("MAX17048 voltage %f", voltage);
119+
}
120+
}
121+
#endif
102122
if (voltage > 0) //valid measurement
103123
{
104124
// Estimate battery level, 3.2V is 0%, 4.17V is 100% (1.0)

src/batterymonitor.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,9 @@ class BatteryMonitor
7474

7575
private:
7676
unsigned long last_battery_sample = 0;
77-
#if BATTERY_MONITOR == BAT_MCP3021 || BATTERY_MONITOR == BAT_INTERNAL_MCP3021
78-
// TODO: The Feather has a MAX17048 (or LC709203F on older ones)
79-
// uint8_t address = 0x36;
80-
uint8_t address = 0;
77+
#if BATTERY_MONITOR == BAT_MCP3021 || BATTERY_MONITOR == BAT_INTERNAL_MCP3021 || BATTERY_MONITOR == BAT_MAX17048
78+
// The Feather has a MAX17048 at 0x36 (LC709203F is used on older boards though)
79+
uint8_t address = 0x36;
8180
#endif
8281
#if BATTERY_MONITOR == BAT_INTERNAL || BATTERY_MONITOR == BAT_INTERNAL_MCP3021
8382
uint16_t voltage_3_3 = 3000;

src/consts.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#define BAT_INTERNAL 2
6363
#define BAT_MCP3021 3
6464
#define BAT_INTERNAL_MCP3021 4
65+
#define BAT_MAX17048 5
6566

6667
#define LED_OFF 255
6768

src/defines.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ IMU_DESC_ENTRY(IMU_BMP160, PRIMARY_IMU_ADDRESS_ONE, IMU_ROTATION, PIN_IMU_SCL, P
5656
// BAT_EXTERNAL for ADC pin,
5757
// BAT_INTERNAL for internal - can detect only low battery,
5858
// BAT_MCP3021 for external ADC connected over I2C
59-
#define BATTERY_MONITOR BAT_INTERNAL
59+
// BAT_MAX17048 for the MAX17048 LiPo fuel gauge connected over I2C
60+
// #define BATTERY_MONITOR BAT_MAX17048
61+
#define BATTERY_MONITOR BAT_MAX17048
6062

6163
// BAT_EXTERNAL definition override
6264
// D1 Mini boards with ESP8266 have internal resistors. For these boards you only have to adjust BATTERY_SHIELD_RESISTANCE.

0 commit comments

Comments
 (0)