I purchased a few of these LiPo fuel gauges and got one hooked up physically using I2C. I’m using a raspberry pi zero however, and am having trouble translating between the examples given for arduino and in circuit python and others on the hookup guide page into “normal” python on the pi. Also, please forgive me, I am also very new to python in general so I’m often making guesses on how to adapt example code and obvious differences to others between circuit python on an ESP32 and python on a pi are not easy for me to spot just yet.
I believe I am close, but because of my lack of understanding with the “registers” where data is stored in the fuel gauge, I’m at a dead end. I found an example in circuitpython that had this code after init and all that:
print(f"Battery voltage: {max17.cell_voltage:.2f} Volts")
print(f"Battery state : {max17.cell_percent:.1f} %")
After some experimentation, hex was as close as I could get to “working” like this:
cell_voltage = 0x2F
voltage = fg.read_byte_data(address, cell_voltage)
print("volt",voltage)
This “works” in that I get no errors, but I only get a value of 255 for that particular address. So I “walked” the addresses from 0 to 100 and found a few “interesting” addresses.
Below is what I currently have that is “working” in that I am getting values from the fuel gauge, but only one of these values seems to make any sense. The value in reference 5 appears to be the battery percentage as it steadily decreases when I have the pi running on battery. The value starts well over 100 though, and seems to fall quite rapidly at least at the end going from 20-0 in a matter of minutes so if this is a pure percent, I suspect I am missing a calibration step.
In the code below, the “registers” list contains the places I found values other than 255. The “registers2” list contains the places I found values that change basically each time I read them. And it seems to make no difference if I use the hex or decimal number when pulling these values.
In another forum post I found someone’s example code that seems to indicate that the voltage is stored in 1.25mV increments so the value given is modified like this: volt=x1/800. That math does not seem to clear any of the values up that I am getting (i.e. 167, 112, or 164). For that math to make sense I would need a value of around 2000-3000 and I’ve only seen a max of 255 so far. Another part of the example has this formula volt=x5/4096. Again, would need to start with a value in the 2000-3000 range.
Any help you can provide would be much appreciated. I feel like I have the percent value, but I’m missing a calibration step for that one and I’m missing the correct locations to probe for voltages along with how to translate those values to voltages.
I’m also questioning if the fuel gauge itself is causing a drain on the battery while I’m not reading from it and if so, I’ve seen some hibernation references so should I be putting the fuel gauge into hibernation mode between readings?
#!/usr/bin/python3
import smbus
import time
address = 0x36
fg = smbus.SMBus(1)
registers = [2,3,4,5,6,7,8,9,12,13,21,52,53,54,55,60,61,62,63]
registers2 = [2,3,5,21]
for x in range (0,4):
for x in registers2:
var = fg.read_byte_data(address,x)
print(x,var)
print('---')
print('--- ---')
time.sleep(2)
Hookup guide page:
https://learn.sparkfun.com/tutorials/li … okup-guide
Circuitpython guide from Adafruit for a different device with the same chip:
https://learn.adafruit.com/adafruit-max … cuitpython
The library for that similar Adafruit device: