Source: https://mokxi.com/libraries/wire
Updated: 2026-11-02

Libraries / Core
Arduino library · Core

# The Arduino Wire library: talking I2C to sensors and displays

Wire.h is Arduino’s I2C library, and nearly every sensor and display library sits on top of it. I2C uses two shared lines, SDA for data and SCL for the clock, and every chip on the bus has a 7-bit address. The Arduino is the controller: it starts each conversation, names the address, and either sends bytes or asks for some back.

Open the example in the editor See the calls

Built in on Mokxi: #include <Wire.h> and it builds, with nothing to install. Free, and no account needed.

## What Wire does

A write is three steps: beginTransmission(address) opens a message, write() adds bytes to a 32-byte buffer, and endTransmission() sends it all and returns a status (0 for success, 2 when nobody acknowledged the address). A read is requestFrom(address, count), then read() for each byte that arrived. Register reads usually send the register number first with endTransmission(false), which keeps the bus for a repeated start.

The pins are fixed by the board: A4 and A5 on an Uno or Nano, 20 and 21 on a Mega, 2 and 3 on a Leonardo, GPIO 21 and 22 on a classic ESP32 (and any two pins with Wire.begin(sda, scl) there). The bus needs pull-up resistors, which most breakout boards already carry.

Library Manager name | Wire

Author | Arduino

Include | #include <Wire.h>

Boards | every board Mokxi compiles for

In the simulator | Simulated: it runs against the part the way it does on the bench.

Parts it drives | Accelerometer and gyroscope, MPU6050, Character LCD with I2C backpack

## Install it

It builds on every board Mokxi compiles for.

In Mokxi it is built in: #include <Wire.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board it comes with the board package (Arduino), so the Arduino IDE has it as soon as the board is installed: there is nothing to add from the Library Manager.

ESP32-C3 · Arduino Uno · Arduino Nano · Arduino Mega 2560 · Arduino Leonardo · SparkFun Pro Micro · Arduino Micro · ESP32-C6 · ATtiny85 · Raspberry Pi Pico · Raspberry Pi Pico W · Seeed XIAO SAMD21 · STM32 · STM32 Nucleo-F411RE · STM32 Blue Pill · micro:bit V2 · Arduino Uno R4 · ESP32 DevKit · ESP32-S3-DevKitC-1 · ESP8266 NodeMCU · Arduino Pro Mini 3.3 V · Seeed XIAO ESP32-C3 · ESP32-C3 SuperMini · Seeed XIAO ESP32-C6 · ESP32-DevKitC V4 · LOLIN32 Lite · Wemos D1 R32 · Cheap Yellow Display · TTGO T-Display · Seeed XIAO ESP32-S3 · Arduino Nano ESP32 · Arduino Nano RP2040 Connect · Seeed XIAO RP2040 · Waveshare RP2040-Zero · Arduino Uno R4 WiFi

## The calls you will use

In the order a sketch meets them.

### Wire.begin()

Joins the bus as the controller on the board’s standard SDA and SCL pins. Call it once in setup().

### Wire.begin(21, 22)

The ESP32 form: SDA and SCL on the GPIOs you name.

### Wire.setClock(400000)

Sets the bus speed. 100 kHz is the default; 400 kHz is fast mode, which most sensors and OLEDs accept.

### Wire.beginTransmission(0x68)

Starts a message to the device at that address. Nothing is sent yet.

### Wire.write(0x3B)

Adds a byte (often a register number) to the message buffer.

### Wire.endTransmission()

Sends the message and returns 0 on success, 2 if the address was not acknowledged, 3 if a data byte was not. endTransmission(false) holds the bus for a repeated start.

### Wire.requestFrom(0x68, 6)

Reads up to 6 bytes from the device into the receive buffer and returns how many came.

### Wire.available()

How many received bytes are waiting. read() takes them one at a time.

## Example: ADXL345 accelerometer on I2C

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3

Open it in the editor, wired and ready to run · ADXL345 accelerometer on I2C: the project page

## Common errors and how to fix them

### 'Wire' was not declared in this scope

Add #include <Wire.h> at the top of the sketch. Some libraries include it for you, but a sketch that calls Wire itself has to include it too.

### endTransmission() returns 2 and the device never answers

The address is wrong or the device is not on the bus. Run an I2C scanner to see which addresses answer, then check SDA and SCL are on the board’s I2C pins (not swapped) and that the module has power and ground.

### The sketch freezes inside a Wire call

A missing pull-up or a shorted line can hold SDA or SCL low forever. Check the module’s pull-up resistors and wiring; on recent AVR cores, Wire.setWireTimeout() makes a stuck transfer give up instead of hanging.

### Two I2C modules work alone but not together

They share an address. Many modules have a solder jumper or an ADDR pin to move one of them; otherwise use a second bus or an I2C multiplexer such as the TCA9548A.

## Questions and answers

### Do I need pull-up resistors?

I2C needs them on SDA and SCL, usually 4.7k to 10k to the logic supply. Almost every breakout board already has them, so with one or two modules you rarely add your own.

## Projects that use Wire

Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.

MPU-6050 readings over I2C A GY-521 board with an MPU-6050 on A4 and A5: the sketch wakes the chip and prints its raw accelerometer, temperature and gyro readings with nothing but Wire. DS3231 real-time clock on I2C A DS3231 clock module on A4 and A5 keeps the date and time; RTClib reads it once a second and prints the date, the time and the day of the week. Laser ruler A VL53L0X time-of-flight sensor over I2C, read with Pololu’s library: millimeters to the target, and “out of range” past about 1.2 m. I2C LCD1602: find its address, then say hello A 16x2 LCD with an I2C backpack on A4 and A5. Barometer A BMP180 read with the Adafruit BMP085 library: temperature to a tenth of a degree, pressure to a pascal, and the altitude that pressure means. Real-time clock A DS1307 clock module on A4 and A5 keeps the date and time, and RTClib reads it back once a second.

## Keep going

### Learn

- I2C Explained: Addresses, Acknowledges and Pull-Ups
- I2C Scanner for Arduino and ESP32, and Fixes
- ESP32 I2C Pins: Custom Pins and Two I2C Buses

### Parts

- Accelerometer and gyroscope, MPU6050
- Character LCD with I2C backpack

### More core libraries

- SPI
- EEPROM
- FreeRTOS (Arduino_FreeRTOS)

Every built-in library · Open the editor

## Try Wire without installing anything

Open the example, change the sketch, and keep your version in a free account.

Start building free

Open the editor
