The Wire library allows you to communicate with I2C devices, a feature that is present on all Arduino boards. I2C is a very common protocol, primarily used for reading and sending data to external I2C components. To learn more, visit the Arduino & I2C guide.
This library inherits from the Stream functions, making it consistent with other read/write libraries. Because of this, send() and receive() have been replaced with read() and write().
To use this library, include it at the top of your sketch:
#include <Wire.h>
Due to hardware design and architectural differences, the I2C pins are located in different places on different boards. The table below highlights the default pins, as well as additional I2C ports available on certain boards.
| Board | I2C Default | I2C0 | I2C1 | I2C2 | Notes |
|---|---|---|---|---|---|
| UNO R3, UNO R3 SMD, UNO Mini LE | A4(SDA), A5(SCL) | I2C also available on the SDA/SCL pins (digital header). | |||
| UNO R4 Minima, UNO R4 WiFi, Nano R4 | A4(SDA), A5(SCL) | Qwiic: D27(SDA), D26(SCL) | A4(SDA), A5(SCL) | Use Wire.begin() for I2C1, and Wire1.begin() for I2C0 (Qwiic). I2C1 also available on the SDA/SCL pins (digital header on UNO boards). | |
| UNO WiFi Rev2, Zero | 20(SDA), 21(SCL) | ||||
| Leonardo, Micro, Yún Rev2 | D2(SDA), D3(SCL) | ||||
| Nano boards | A4(SDA), A5(SCL) | ||||
| MKR boards | D11(SDA), D12(SCL) | ||||
| GIGA R1 WiFi | 20(SDA), 21(SCL) | D102(SDA1), D101(SCL1) | D9(SDA2), D8(SCL2) | Use Wire1.begin() for I2C1, and Wire2.begin() for I2C2. | |
| Due | 20(SDA), 21(SCL) | D70(SDA1), D71(SCL1) | Use Wire1.begin() for I2C1. | ||
| Mega 2560 Rev3 | D20(SDA), D21(SCL) |
Initialization
Controller transmitter
Controller receiver
Peripheral callbacks
Timeout configuration
I2C addressing: The Wire library uses 7-bit addresses throughout. If you have a datasheet or sample code that uses 8-bit addresses, you'll need to shift the value one bit to the right (drop the low bit), yielding an address between 0 and 127. Note that addresses 0 to 7 are reserved, so the first usable address is 8.
Pull-up resistors: A pull-up resistor is needed when connecting the SDA and SCL pins. Some boards (such as the Mega 2560) have pull-up resistors on pins 20 and 21 onboard. Please refer to the examples for more information.
Buffer size: The Wire library uses a 32-byte buffer. Any communication should be within this limit; bytes exceeding this in a single transmission will be dropped.
Timeouts: Recent versions of the Wire library can use timeouts to prevent lockups in the face of certain bus problems, but this is not enabled by default. It is recommended to always enable timeouts when using the Wire library. See setWireTimeout() for more details.