kplindegaard / smbus2

A drop-in replacement for smbus-cffi/smbus-python in pure Python
MIT License
243 stars 68 forks source link

smbus2 reading incorrect value from I2C register #115

Open pmi123 opened 2 months ago

pmi123 commented 2 months ago

I have a Pi Zero W reading registers from an AdaFruit MAX17042 battery fuel gauge https://www.adafruit.com/product/5580 using the I2C bus with a single cell LiPo battery and a 68 Ohm resistor and red LED as a load. AdaFruit provides some C code to interface to the board https://github.com/adafruit/Adafruit_CircuitPython_MAX1704x/tree/main and I can read the voltage from the register on the chip using the I2C bus, and it appears to be accurate. When I try to read the same register using smbus2 I get a different value.

The value read from the Adafruit code is 513D: adafruit-1 The value read from the smbus2 code is 5113: smbus2-1

I am using data = self.i2cbus.read_word_data(self.i2caddress, register) to read the registers on the MAX17048 chip. The python files for the code I am using are attached as .txt.files. i2ctest.txt MAX17048.txt

If I use smbus instead of smbus2 to read the MAX17042's registers, I get the correct result. I have attached the updated MAX17048.py code to use smbus instead of smbus2. MAX17048-smbus.txt

kplindegaard commented 2 months ago

Hi @pmi123. smbus2 is not a clever library. It is just a wrapper on top of the Linux kernel's i2c bus interface with the objective to simplify i2c and SMBus communication in Python. It doesn't do any sort of data conversion or adjustments, just pipes through the commands and responses as is.

I am not familiar with the device you have, but aren't the differences in the received word values very small? 513D vs 5113 are not a lot if the whole word range is used. Thus leaving room for measurement error/noise, I mean....?