Skip to content

Commit

Permalink
Adds MAX17048 battery monitor support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob committed Jun 13, 2024
1 parent 293fd78 commit efc649a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
22 changes: 21 additions & 1 deletion src/batterymonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@ void BatteryMonitor::Setup()
m_Logger.error("MCP3021 not found on I2C bus");
}
#endif
#if BATTERY_MONITOR == BAT_MAX17048
if (address == 0)
{
m_Logger.error("MAX17048 not found on I2C bus");
}
#endif
}

void BatteryMonitor::Loop()
{
#if BATTERY_MONITOR == BAT_EXTERNAL || BATTERY_MONITOR == BAT_INTERNAL || BATTERY_MONITOR == BAT_MCP3021 || BATTERY_MONITOR == BAT_INTERNAL_MCP3021
#if BATTERY_MONITOR == BAT_EXTERNAL || BATTERY_MONITOR == BAT_INTERNAL || BATTERY_MONITOR == BAT_MCP3021 || BATTERY_MONITOR == BAT_INTERNAL_MCP3021 || BATTERY_MONITOR == BAT_MAX17048
auto now_ms = millis();
if (now_ms - last_battery_sample >= batterySampleRate)
{
Expand Down Expand Up @@ -99,6 +105,20 @@ void BatteryMonitor::Loop()
}
}
#endif
#if BATTERY_MONITOR == BAT_MAX17048
if (address > 0)
{
// Cell voltage register address 0x02
uint16_t data;
auto status = I2Cdev::readWord(address, 0x02, &data);
if (status == 0)
{
float v = data * (78.125 / 1000000);
voltage = (voltage > 0) ? min(voltage, v) : v;
m_Logger.info("MAX17048 voltage %f", voltage);
}
}
#endif
if (voltage > 0) //valid measurement
{
// Estimate battery level, 3.2V is 0%, 4.17V is 100% (1.0)
Expand Down
7 changes: 3 additions & 4 deletions src/batterymonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ class BatteryMonitor

private:
unsigned long last_battery_sample = 0;
#if BATTERY_MONITOR == BAT_MCP3021 || BATTERY_MONITOR == BAT_INTERNAL_MCP3021
// TODO: The Feather has a MAX17048 (or LC709203F on older ones)
// uint8_t address = 0x36;
uint8_t address = 0;
#if BATTERY_MONITOR == BAT_MCP3021 || BATTERY_MONITOR == BAT_INTERNAL_MCP3021 || BATTERY_MONITOR == BAT_MAX17048
// The Feather has a MAX17048 at 0x36 (LC709203F is used on older boards though)
uint8_t address = 0x36;
#endif
#if BATTERY_MONITOR == BAT_INTERNAL || BATTERY_MONITOR == BAT_INTERNAL_MCP3021
uint16_t voltage_3_3 = 3000;
Expand Down
1 change: 1 addition & 0 deletions src/consts.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#define BAT_INTERNAL 2
#define BAT_MCP3021 3
#define BAT_INTERNAL_MCP3021 4
#define BAT_MAX17048 5

#define LED_OFF 255

Expand Down
4 changes: 3 additions & 1 deletion src/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ IMU_DESC_ENTRY(IMU_BMP160, PRIMARY_IMU_ADDRESS_ONE, IMU_ROTATION, PIN_IMU_SCL, P
// BAT_EXTERNAL for ADC pin,
// BAT_INTERNAL for internal - can detect only low battery,
// BAT_MCP3021 for external ADC connected over I2C
#define BATTERY_MONITOR BAT_INTERNAL
// BAT_MAX17048 for the MAX17048 LiPo fuel gauge connected over I2C
// #define BATTERY_MONITOR BAT_MAX17048
#define BATTERY_MONITOR BAT_MAX17048

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

0 comments on commit efc649a

Please sign in to comment.