Skip to content

Explanation of the functions of the Wire library

Koepel edited this page Mar 24, 2018 · 24 revisions

Wire.begin()

The Wire.begin() starts the hardware and software.

Delay needed ? No.
If there are external pullup resistors, there is no need to add a delay after the Wire.begin(). The SDA and SCL are already high and will stay high.
Without external pullup resistors, a delay is probably not needed. The SDA and SCL signals could have noise, but the first START condition on the I2C bus is enough. I have not heard of a sensor yet, that could not capture the first start after floating bus signals.

Wire.beginTransmission(), Wire.write(), Wire.endTransmission()

These three functions work together. The Wire.beginTransmission() and Wire.endTransmission() should never be used on their own. To test if a sensor will acknowledge to its address, a Wire.write() is not needed. The maximum number of Wire.write() depends on the buffer size (inside the Wire library).

Wire.requestFrom()

This function reads data from a sensor. After it is finished, the received data is in a buffer (inside the Wire library). That data can be read with Wire.read(). The Wire.available() tells how many bytes are still in that buffer.