Page 1 of 1

I2C giving TypeError: object with buffer protocol required

Posted: Mon Oct 25, 2021 12:46 am
by rajeee
Hi,
I am trying to get I2C to work on esp32-s2 device (feather-s2), but I keep getting the " TypeError: object with buffer protocol required". I don't even know, what this error means?

Code: Select all

 >>>i2c = machine.I2C(0, freq=100_100)
 >>> i2c.scan()
[105]
>>> data = bytearray(b'\x05\x00\xf6')
>>> response = i2c.writeto(105, 0x0010, data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object with buffer protocol required
>>> response = i2c.writeto(105, 0x0010, b'\x05\x00\xf6')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object with buffer protocol required
 
Could anyone help me debug this? What does the error even mean?

Thanks!

Re: I2C giving TypeError: object with buffer protocol required

Posted: Mon Oct 25, 2021 5:49 am
by Roberthh
The call arguments are wrong. You have to use:

response = i2c.writeto(105, data)

Bytes strings or bytearrays ar for instance objects with a buffer protocol (they are buffers), a number like 0x0010 is not.

Re: I2C giving TypeError: object with buffer protocol required

Posted: Mon Oct 25, 2021 7:12 am
by pythoncoder
And you can convert an integer to a bytes object with (for example) int.to_bytes(0x0010, 2, 'little').