I2C giving TypeError: object with buffer protocol required

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
rajeee
Posts: 1
Joined: Mon Oct 25, 2021 12:34 am

I2C giving TypeError: object with buffer protocol required

Post by rajeee » Mon Oct 25, 2021 12:46 am

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!

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: I2C giving TypeError: object with buffer protocol required

Post by Roberthh » Mon Oct 25, 2021 5:49 am

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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: I2C giving TypeError: object with buffer protocol required

Post by pythoncoder » Mon Oct 25, 2021 7:12 am

And you can convert an integer to a bytes object with (for example) int.to_bytes(0x0010, 2, 'little').
Peter Hinch
Index to my micropython libraries.

Post Reply