-
Notifications
You must be signed in to change notification settings - Fork 5
Explanation of the functions of the Wire library
The Wire.begin() starts the hardware and software.
What about a delay after a Wire.begin() ?
A delay is probaby not needed.
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 because they are floating before the Wire.begin(), 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 condition after floating bus signals.
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() in the middle is not needed.
The maximum number of bytes that can be written depends on the buffer size (inside the Wire library).
Read more about the buffer size: Buffer size of the Wire library.
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.
When there was a problem during the I2C bus activity, the received bytes up to that point are not reliable. With the Arduino Wire library it is not even possible to know if there are any reliable bytes before the bus error did happen. It is therefor better to first check for errors and if there are no errors, then read all the bytes.
Checking for errors might not be needed, see the Check for bus errors ? page.