Wire

Description

This library allows you to communicate with I2C devices, a feature that is present on all Arduino boards. I2C is a very common protocol, primarly used for reading/sending data to/from external I2C components. To learn more, visit this article for Arduino & I2C.

Due to the hardware design and various architectural differences, the I2C pins are located in different places. The pin map just below highlights the default pins, as well as additional ports available on certain boards.

BoardI2C DefaultI2C1I2C2Notes
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 WiFiA4(SDA), A5(SCL)Qwiic: D27(SDA), D26(SCL)I2C also available on the SDA / SCL pins (digital header).
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)

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()
.

Recent versions of the Wire library can use timeouts to prevent a lockup in the face of certain problems on the bus, but this is not enabled by default (yet) in current versions. It is recommended to always enable these timeouts when using the Wire library. See the Wire.setWireTimeout function for more details.

Note: There are both 7 and 8-bit versions of I2C addresses. 7 bits identify the device, and the eighth bit determines if it’s being written to or read from. The Wire library uses 7 bit addresses throughout. If you have a datasheet or sample code that uses 8-bit address, you’ll want to drop the low bit (i.e. shift the value one bit to the right), yielding an address between 0 and 127. However the addresses from 0 to 7 are not used because are reserved so the first address that can be used is 8. Please note that a pull-up resistor is needed when connecting SDA/SCL pins. Please refer to the examples for more information. Mega 2560 board has pull-up resistors on pins 20 and 21 onboard.

The Wire library implementation uses a 32 byte buffer, therefore any communication should be within this limit. Exceeding bytes in a single transmission will just be dropped.

To use this library:

#include <Wire.h>

Functions

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.