Tuesday 10 December 2019

Arduino Wire.requestFrom() Description & Explanation

I will talk about the "Wire.requestFrom()" from the Arduino Wire library for AVR microcontrollers (for example the Arduino Uno).

The Wire.requestFrom() is only called by an Arduino which is the Master on the I2C bus. The Wire.requestFrom() does a complete I2C transaction, with START, STOP, reading databytes, everything.

  • First it sends a START
  • Then it puts the I2C address on the bus with the "read" bit in the lowest bit.
  • It checks the acknowledge from the Slave, if there is a acknowledge, then continue with reading data bytes.
  • It reads a byte and give an acknowledge to the Slave for the next byte. It puts the read byte in a buffer (inside the Wire library).
  • After the last byte is read, it does not send a acknowledge. Sending "no acknowledge" is the same as a NACK.
  • It sends a STOP.

That's it. After calling the Wire.requestFrom(), the read data is in a buffer inside the Wire library. The activity on the I2C bus has finished.

You can read those bytes from that buffer with Wire.read(), and Wire.available() tells how many bytes are still in that buffer.

The Arduino reference about the Wire library caused a lot of confusion in the past, so I made an alternative explanation.

Is this enough detail, or do you want to know about that the Wire library is interrupt-driven, but the Wire.requestFrom() is still blocking ?