0

I would like to write 16 bits of data to an I2C bus. Considering that the data to be transferred is not in a list, I am using the pi.i2c_write_device() function, however I get the following error:

    in i2c_write_device
    if len(data):
TypeError: object of type 'int' has no len()

I am passing in a 16 bit value, hence the error. Here is the code I am working with:

import pigpio
import time

##########################################
##  I2C Initilization
##########################################

channel = 1

i2c_addr = 0b0001110  

pi=pigpio.pi()
j = pi.i2c_open(channel, i2c_addr) # open device at address i2c_addr on bus 1

###########################################
transfer_data = 2096
data = transfer_data << 4        #(16 bits needed for DAC)
pi.i2c_write_device(j, data)

sleep(3)
pi.i2c_close(j)

I appreciate any help with figuring this out. Please let me know if you have any questions, thank you!

1 Answer 1

0

The parameter data expects an array of bytes.

i2c_write_device gives the following examples.

pi.i2c_write_device(h, b"\x12\x34\xA8")

pi.i2c_write_device(h, b"help")

pi.i2c_write_device(h, 'help')

pi.i2c_write_device(h, [23, 56, 231])

Perhaps try something like

val = 2096 << 4

pi.i2c_write_device(h, [val&255, (val>>8)&255])
1
  • thank you for the clear explanation!! @joan
    – user49706
    Commented Oct 2, 2022 at 16:58

Your Answer

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

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