Detect errors in I2C readings

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
MauroDiam
Posts: 7
Joined: Thu Jul 23, 2020 1:38 pm

Detect errors in I2C readings

Post by MauroDiam » Fri Aug 12, 2022 7:09 pm

Hi guys, is there a way to detect than there were errors when the method

Code: Select all

readfrom_mem()
from the class machine.SoftI2C is used with a ESP32 board. Does that method raise an exception if that occurs?

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

Re: Detect errors in I2C readings

Post by Roberthh » Fri Aug 12, 2022 7:28 pm

The I2C transport mechanisms has no error checks for the transmitted data. So if the target device does not implement some kind of error detection mechanism like CRC, you could at least at least try to check the data for plausibility.

MauroDiam
Posts: 7
Joined: Thu Jul 23, 2020 1:38 pm

Re: Detect errors in I2C readings

Post by MauroDiam » Fri Aug 12, 2022 8:00 pm

Hi @Roberthh, i'm reading the I2C protocol and now i see what you mean, so i'll have to study deeper the sensor datasheet to verify if it implements some errors correction code. But i still have a doubt, what happened if a NACK bit is transmitted by the master (esp32) during the execution of the method readfrom_mem()?

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

Re: Detect errors in I2C readings

Post by Roberthh » Fri Aug 12, 2022 8:18 pm

If a NAK bit is transmitted then the python gets and I/O error exception.
Edit: It depends.
If NAK is received after the address bit, then an MP_NODEV error is flagged.
If NAK is received after sending a data byte, the transmission is stopped.

Post Reply