I2C read/write error handling

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
thejoker
Posts: 11
Joined: Sat Aug 04, 2018 2:14 pm

I2C read/write error handling

Post by thejoker » Tue Sep 18, 2018 8:27 pm

Dear reader, I currently get the following error message semi-regularly when using the I2C communication protocol:

OSError: [Errno 5] EIO.

Now I've looked up what it means and apparently it's an Input/Output error (I/O error). I found a thread of a few months ago where someone had a similar problem, and it was solved by adding a small delay after the writing operation. However, I have added up to 100ms delay without succesfully mitigating the error. The error happens semi-regularly after calling the read_byte function inside a for loop, but it never happens when I read it with putty.

Code: Select all

def read_byte(addr, nr_of_bytes, i2cobj):
    bytes_read = bytearray(nr_of_bytes)
    bytes_read[:] = i2cobj.mem_read(nr_of_bytes, MPU_address, addr)
    return bytes(bytes_read)
Any suggestions on how to solve this problem? It also occurs if I don't initialize the bytearray and directly return the i2cobj.mem_read(...) value .

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: I2C read/write error handling

Post by jickster » Wed Sep 19, 2018 2:49 am

What do you mean “read it with putty”?

How do you read i2c with putty?


Sent from my iPhone using Tapatalk Pro

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

Re: I2C read/write error handling

Post by pythoncoder » Wed Sep 19, 2018 7:56 am

@jickster I think he means issuing the command manually via the terminal, rather than in a program loop.

@thejoker I don't think the precise form of your function will affect the outcome. You don't mention what hardware you have at the other end of the I2C interface but I'd suspect a marginal hardware condition: such faults can produce quite random symptoms and you can spend a long time chasing a nonexistent coding issue. Something on the lines of baudrate too high, wires too long or a power supply issue.

For what it's worth the simplest form of your function would be:

Code: Select all

def read_byte(addr, nr_of_bytes, i2cobj):
    return i2cobj.mem_read(nr_of_bytes, MPU_address, addr)
A worthwhile simplification but alas it won't fix your problem.
Peter Hinch
Index to my micropython libraries.

thejoker
Posts: 11
Joined: Sat Aug 04, 2018 2:14 pm

Re: I2C read/write error handling

Post by thejoker » Wed Sep 19, 2018 4:08 pm

@Peter: Correct, putty is the terminal I use to issue commands to the board.

On-topic though, I solved the problem! It was hardware related, and had to do with parasitic capacitance. Fortunately not micropython related.

Thank you for helping me shift my focus from the micropython board to my hardware setup.

Post Reply