0

I'm trying to connect my energy measurement IC with Raspberry Pi 3B using the I2C protocol. I'm using sudo i2cdetect -y 1 to detect the connected devices and it is correctly showing the address(7-bits) of the connected IC. The IC contains a various registers of 8,16,24 and 32-bits. The register addresses are 16-bits long and I'm using the following code to access a register 0x021C(24-bits) and write registers 0xFE(8-bit) and 0x0120(16-bit)

Python Code

from smbus2 import SMBus

bus = SMBus(1)

address = 0x38

bus.write_byte_data(address,0xFE,0xAD)  #Writing single byte of data
bus.write_i2c_block_data(address,0x120,[0x00,0x30])  #Writing multiple bytes of data to the register
block = bus.read_i2c_block_data(address,0x21C,3)  #Reading multiple bytes of data from the register
print(block)

bus.close()

The output should be [0x8D,0xXX,0xXX] but the code is producing output as [0,0,0]

  • Question-1: Is giving 16-bit address allowed in this Library or not?
  • Question-2: If allowed then why the code is not working?

Any help would be much appreciated

1
  • You seem to be using "read block". I vaguely remember that requires "bus stretching".
    – tlfong01
    Jul 8, 2022 at 2:46

1 Answer 1

0

All I2C parameters are 8-bit.

To send a 16-bit value you must send two 8-bit values.

To send a 24-bit value you must send three 8-bit values.

You must also send the values in the order expected by the device. This may be least significant value first or most significant value first.

3
  • I've tried what you are mentioning but the functions only accept only integer values for the register address. read_block_data(i2c_addr, register, force=None) Read a block of up to 32-bytes from a given register. Parameters: i2c_addr (int) – i2c address register (int) – Start register force (Boolean) – Returns: List of bytes Return type: list Jul 7, 2022 at 20:09
  • I suggest you experiment with pigs. Only use i2co, i2crd, and i2cwd.
    – joan
    Jul 7, 2022 at 20:21
  • Take a look at adafruit blink library, similar to smbus but less protocol specific: in particular look at something like writeto_then_readfrom(address, buffer_out, buffer_in, *, out_start=0, out_end=None, in_start=0, in_end=None, stop=False), it might enable you to do what you are attempting Dec 4, 2022 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.