User avatar
przemof
Posts: 148
Joined: Mon Dec 30, 2013 6:43 pm

Fuel gauge (battery status) based on MAX17043 - help needed

Tue Nov 13, 2018 12:33 pm

I'm trying to read from a fuel gauge (battery status) based on MAX17043. I have it connected and it shows in the i2c bus. I did some searching and I found:
- there was a driver submitted to the kernel main line [1], but I don't see it there,
- there is overlay for that chip
- datasheet [2]
- connection and low level driver [3] (read/write registers)
- schematic drawing [4]
- arduino wiring/code [5]
- a kernel driver [6]

Image

Is there anything better that those above? I mean some kernel driver and overlay file to avoid messing with registers like I did here with bmp280 [7]

[1] http://lkml.iu.edu/hypermail/linux/kern ... 00105.html
[2] https://cdn.sparkfun.com/datasheets/Pro ... X17044.pdf
[3] http://www.thinkering.de/cms/?p=960
[4] https://cdn.sparkfun.com/datasheets/Pro ... ge-v11.pdf
[5] http://blog.codebender.cc/2014/02/13/lifuelgauge/
[6] https://github.com/Outurnate/i2c-battery
[7] viewtopic.php?f=32&t=225212

User avatar
przemof
Posts: 148
Joined: Mon Dec 30, 2013 6:43 pm

Re: Fuel gauge (battery status) based on MAX17043 - help needed

Tue Nov 13, 2018 4:48 pm

Looks like the board I have is broken. I can do anything as per datasheet, except it doesn't measure voltage, so the results are useless. Looks like the unit from China/ebay are almost all rubbish [1]. Is there any battery gauge that just works nicely with pi?

[1] https://forum.arduino.cc/index.php?topic=341825.0

User avatar
przemof
Posts: 148
Joined: Mon Dec 30, 2013 6:43 pm

Re: Fuel gauge (battery status) based on MAX17043 - help needed

Tue Nov 13, 2018 10:45 pm

Someone my be more lucky than me. The code:

Code: Select all

#!/usr/bin/python3

import smbus
import time


address = 0x32

VCELL_REG = 0x02
SOC_REG = 0x04
MODE_REG = 0x06
VERSION_REG = 0x08
CONFIG_REG = 0x0C
COMMAND_REG = 0xFE

fg = smbus.SMBus(1)


def reset():
    fg.write_byte_data(address, COMMAND_REG, 0x54)
    fg.write_byte_data(address, COMMAND_REG + 1, 0x00)


def quick_start():
    fg.write_byte_data(address, MODE_REG, 0x00)
    fg.write_byte_data(address, MODE_REG + 1, 0x40)


def get_alert():
    #MSB = fg.read_byte_data(address, CONFIG_REG)
    LSB = fg.read_byte_data(address, CONFIG_REG + 1)
    alert_status = (0x20 & LSB) >> 5
    alert_thd = 32 - (0x1F & LSB)
    print('alert status : {}'.format(alert_status))
    print('alert thd : {}'.format(alert_thd))


def get_config():
    MSB = fg.read_byte_data(address, CONFIG_REG)
    LSB = fg.read_byte_data(address, CONFIG_REG + 1)
    config = (MSB << 8) + LSB
    sleep_status = (0x80 & LSB) >> 7
    print('sleep : {}'.format(sleep_status))
    print('config : {}'.format(hex(config)))
   
   def get_version():
    MSB = fg.read_byte_data(address, VERSION_REG)
    LSB = fg.read_byte_data(address, VERSION_REG + 1)
    version = hex(0xFF * MSB + LSB)
    print('version : {}'.format(version))


def get_soc():
    MSB = fg.read_byte_data(address, SOC_REG)
    LSB = fg.read_byte_data(address, SOC_REG + 1)
    percentage = (0xFF * MSB + LSB) >> 8
    print('---')
    MSB = fg.read_byte_data(address, VCELL_REG)
    LSB = fg.read_byte_data(address, VCELL_REG + 1)
    voltage = (MSB << 8 + LSB) >> 4
    print('{} %, {} V'.format(percentage, voltage))


reset()
quick_start()
get_version()
get_config()
get_soc()
while True:
    get_soc()
    get_alert()
    time.sleep(1)
My results:

Code: Select all

[email protected]:~/fuel $ python3 test.py 
version : 0x3
sleep : 0
config : 0x971c
---
0 %, 0 V
---
0 %, 0 V
alert status : 0
alert thd : 4
---
109 %, 0 V
alert status : 0
alert thd : 4
---
108 %, 0 V
alert status : 0
alert thd : 4
---
108 %, 0 V
alert status : 0
alert thd : 4
---
107 %, 0 V
alert status : 0
alert thd : 4
---
107 %, 0 V
alert status : 0
alert thd : 4
[..]
Voltage stays at 0V. Percentago goes slowly down to 0%. Voltage mesured on the CELL pin on the chip was 3.56V, so the the chip is partially dead. Rest of the functions work as expected, including setting off alarm when the battery goes below the alarm threshold.

Return to “HATs and other add-ons”