Recently released a Library for direct interface with blues Mojo

Hey everyone, I’ve recently released a library that interfaces directly with Blues Mojo. You can check it out on GitHub: GitHub - Pius171/LTC2959-library: A library for communicating with LTC2959 Ultra-Low Power Battery Gas Gauge

Any contribution to the library will be highly appreciated. I have tested it with both STM32 and ESP32, but it should work with an Arduino-supported board, as it uses the standard wire library for I2C in arduino.

5 Likes

Thanks for sharing this @Pius4109!

Works great @Pius4109 !

One thing: It seems like like the variable containing mAh is rolling over or getting out of range when going into negatives. “Charge” goes from zero to a few millions when its supposed to go negative.

I will look into it. I thought I already solved that issue. Thanks

I’ve resolved the issue — thanks @TobiasR for helping spot it!

The ACR (Accumulated Charge Register) is a 32-bit register with a full range of:

0x00000000 (0) → 0xFFFFFFFF (4,294,967,295)

Originally, I was resetting the register to zero (0x00000000). However, during charging (when the current is negative), the ACR decreases. This quickly pushes the value below zero, which wraps around due to the unsigned nature of the 32-bit register — causing an underflow and leading to incorrect (very large) mAh readings.

To solve this, I now reset the ACR to mid-scale (0x80000000). This shifts the effective range to:

−2,147,483,648 to +2,147,483,647  (signed 32-bit range)

Then, in my readCharge_mAh() function, I subtract the mid-scale value before converting to mAh:

int32_t delta = (int32_t)(rawACR - 0x80000000);
float mAh = delta * 0.000533;

This way, the charge count can grow positively when discharging and negatively when charging, thus preventing underflow.

Great you found the cause. Looking forward to pull an updated library.

Another thing I just encountered: I’m not sure the counter is giving the right mAh. Look at the example bellow:

18:01:46:515 -> Hello World (Setup)
18:01:46:599 -> Initializing LTC2959...
18:01:46:601 -> LTC2959 initialized!
18:01:48:604 -> Voltage: 4.087 V | Current: 32.659 mA | Temp: 25.64 C | Max V: 4.224 V | Min V: 4.082 V | Charge: 0.089 mAh
18:02:48:605 -> Voltage: 4.084 V | Current: 32.587 mA | Temp: 25.66 C | Max V: 4.224 V | Min V: 4.082 V | Charge: 2.773 mAh
18:03:48:609 -> Voltage: 4.084 V | Current: 33.016 mA | Temp: 25.73 C | Max V: 4.224 V | Min V: 4.082 V | Charge: 5.482 mAh

If we assume the the instant measurement of 33 mA is about what is being drawn on average the mAh consumption during one minute should have been approx 0.55 mAh. But the value in the example above is ~2.8 mAh during one minute.

I’m measuring the STM32 Swan itself.

Any thoughts on this?

Edit: Added a inline mA measure multi meter which confirms that its pulling approx 30 mA continuously.

I think I found the cause:
The MoJo implementation has a 250 Ohm resistor instead of a 50 ohm, which is the “standard” in the datasheet of the LTC2959. There is nowhere to program the LTC2959 with the resistor value, as far as i know. But it can be corrected in the code when processing the values:

LTC2959\src\LTC2959.cpp:

// For Rsense = 0.250 Ω:
const float qLSB_nAh = 533.0f * (50.0e-3f / 0.25f); // ≈106.6 nAh
float LTC2959::readCharge_mAh()
{
uint32_t raw = readRegister32(0x03);
return raw * (qLSB_nAh / 1.0e6f);
}

Sorry I am just seeing this, I will look into it on Monday. I also saw Pull request on github.