Description

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>

Default I2C Pins

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.

BoardI2C DefaultI2C0I2C1I2C2Notes
UNO R3, UNO R3 SMD, UNO Mini LEA4(SDA), A5(SCL)I2C also available on the SDA/SCL pins (digital header).
UNO R4 Minima, UNO R4 WiFi, Nano R4A4(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, Zero20(SDA), 21(SCL)
Leonardo, Micro, Yún Rev2D2(SDA), D3(SCL)
Nano boardsA4(SDA), A5(SCL)
MKR boardsD11(SDA), D12(SCL)
GIGA R1 WiFi20(SDA), 21(SCL)D102(SDA1), D101(SCL1)D9(SDA2), D8(SCL2)Use Wire1.begin() for I2C1, and Wire2.begin() for I2C2.
Due20(SDA), 21(SCL)D70(SDA1), D71(SCL1)Use Wire1.begin() for I2C1.
Mega 2560 Rev3D20(SDA), D21(SCL)

Functions

Initialization

Controller transmitter

Controller receiver

Peripheral callbacks

Timeout configuration

Notes and Warnings

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.