Skip to content

Tricks that are (almost) allowed

Koepel edited this page Mar 25, 2018 · 12 revisions

Reading the data in the loop() in a Slave with empty onReceive handler.

When an Arduino is set as I2C Slave, the interrupt function for receiving data is set with Wire.onReceive(). That interrupt function (the handler function) can be empty and the checking for received data can be done in the loop(). Thus assuming that the data is still available in the buffer after the interrupt function (the handler function) has finished.

This might work, although when new data is received it will replace the old data and in the loop() a mix of old and new data is processed.

The official Arduino Wire libraries clear the buffer when receiving data. That means the old data stays in the buffer can can indeed be used outside the interrupt function. This is however not described in the documentation, so there is no guarantee that this will always work.

The main concern is the compatibility with other Wire libraries.

In my opinion it is a trick, which is allowed at this moment with the official Arduino Wire libraries. As long as the documentation does not explicitly say that it is allowed, it might be better to avoid it if possible.

Clone this wiki locally