arthurfbs
Posts: 2
Joined: Fri Jan 19, 2018 8:53 pm

How to read I2C values with python smbus from BQ27441 (battery fuel gauge)

Fri Jan 19, 2018 9:05 pm

Hi,

I am trying to read values of current, voltage, temperature and state of charge from the BQ27441 (datasheet http://www.ti.com/lit/ug/sluuac9a/sluuac9a.pdf) through I2C in Raspberry Pi. I'm doing a code in python using smbus, but I don't know exactly which commands should I use for that. If I try to read the voltage value, for example, I don't know which address showed in the datasheet should I use. Is it 0x04 or 0x05? Anybody could give me please an example python code for that?
Attachments
Untitled.jpg
Untitled.jpg (241 KiB) Viewed 2849 times

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: How to read I2C values with python smbus from BQ27441 (battery fuel gauge)

Mon Jan 22, 2018 6:18 pm

I think you're getting mixed up between the address of the i2c device and the command codes to send. Have a look here https://learn.adafruit.com/adafruits-ra ... guring-i2c and scroll down to the part about "Testing I2C", that will show you how to find the address of your device. Use that address to connect then send both 0x04 and 0x05 as the control codes.

Dave.
Apple say... Monkey do !!

shadetree01010100
Posts: 29
Joined: Thu Oct 12, 2017 9:19 pm
Location: CO, USA
Contact: Website

Re: How to read I2C values with python smbus from BQ27441 (battery fuel gauge)

Mon Jan 22, 2018 10:08 pm

in the linked datasheet, 3.1 Data Memory Parameter Update Example, tells us that the device has two bus addresses, 0xAA and 0xAB. Note that your pi is not going to work with address 0xAB, the actual address is 0x55 (addresses above 0x77 must be shifted one bit to the right). So using SMBus you want to use 0x55 as your address, and as davef21370 said send two commands according to your attachment (such as 0x04, 0x05) to have the value returned to you.

arthurfbs
Posts: 2
Joined: Fri Jan 19, 2018 8:53 pm

Re: How to read I2C values with python smbus from BQ27441 (battery fuel gauge)

Tue Jan 23, 2018 10:17 pm

Thank you for the answers! The device address is 0x55. What I had to do is read 2 bytes, starting from register 0x04 (so it will read 0x04 and 0x05 register) to get the voltage value.

Now I'm trying to set the Nominal Capacity in the fuel gauge BQ27441. I am following the steps written on page 14 of the datasheet (http://www.ti.com/lit/ug/sluuac9a/sluuac9a.pdf). I wrote a python code as you can see below, trying to set the Design Capacity to 1200mAh, but when I try to write on the address 0x60, I've got the following error message

"bus.write_byte_data(address,0x60,0x1f)

IOError: [Errno 121] Remote I/O error"


I am able to read 0x60; just when I try to write on it that I have this problem and I've got that message. Does anyone know what might be causing this error?

Code: Select all

import smbus
import time
import struct

bus=smbus.SMBus(1)

address = 0x55

e=bus.read_i2c_block_data(address,0x0a,2)
(full_cap,)=struct.unpack('H', bytearray(e)[0:2])
print "Full Available Capacity is",full_cap, 'mA'

#Trying to write the nominal capacity
bus.write_byte_data(address,0x00,0x00)
bus.write_byte_data(address,0x01,0x80)
bus.write_byte_data(address,0x00,0x00)
bus.write_byte_data(address,0x01,0x80)

bus.write_byte_data(address,0x00,0x13)
bus.write_byte_data(address,0x01,0x00)

bus.write_byte_data(address,0x61,0x00)
bus.write_byte_data(address,0x3e,0x52)
bus.write_byte_data(address,0x3f,0x00)

bus.write_byte_data(address,0x4a,0x04) #writing new capacity
bus.write_byte_data(address,0x4b,0xb0) #writing new capacity

time.sleep(1)

bus.write_byte_data(address,0x60,0x1f) #trying to write on BlockDataChecksum() 

time.sleep(1)

bus.write_byte_data(address,0x00,0x42)
bus.write_byte_data(address,0x01,0x00)

bus.write_byte_data(address,0x00,0x20)
bus.write_byte_data(address,0x01,0x00)

f=bus.read_i2c_block_data(address,0x3c,2) #address for design capacity
(des_cap,)=struct.unpack('H', bytearray(f)[0:2])
print(des_cap)

shadetree01010100
Posts: 29
Joined: Thu Oct 12, 2017 9:19 pm
Location: CO, USA
Contact: Website

Re: How to read I2C values with python smbus from BQ27441 (battery fuel gauge)

Tue Jan 23, 2018 11:36 pm

it looks like writing is working fine, but it's not liking what you're trying to write into 0x60

Code: Select all

BlockDataChecksum(): 0x60
UNSEALED Access: This byte contains the checksum on the 32 bytes of block data read or written. The
least-significant byte of the sum of the data bytes written must be complemented ([255 – x], for x the leastsignificant
byte) before being written to 0x60. For a block write, the correct complemented checksum must
be written before the BlockData() will be transferred to RAM.
SEALED Access: This command is not available in SEALED mode.
Two guesses off the top of my head:
1) is it in UNSEALED mode? (I assume yes since the preceding write_byte_data calls work)
2) is the value you're writing correct?

It might help to see the whole error/traceback, there should be more info attached to that IOError.

Return to “Python”

Who is online

Users browsing this forum: onlinegill and 20 guests