Arduino and the I2C/TWI protocol
I2C/TWI is a low-speed, two-wire serial communication protocol designed for connecting integrated circuits—such as displays, sensors, etc.—to a microcontroller.
Difficulty: Beginner
Created: December 2022
Technically reviewed: August 2026
Reference platform: Arduino Uno family / general Arduino API
In the previous lesson, we explored serial communication between the Arduino and a computer. We will now learn about a communication method that allows an Arduino to connect directly to various sensors, displays, real-time clock modules and other integrated circuits.
This is I²C, also known as TWI.
I²C is particularly useful when we want to connect multiple devices to the same Arduino without using separate data lines for every device.
What is I²C?
I²C is a short-distance, synchronous serial communication bus. It basically requires two signal lines for data transfer:
- SDA – the data line;
- SCL – the clock line.
A shared ground connection is also required, of course. Therefore, when connecting two separate circuits, we must establish at least the SDA, SCL and GND connections.
One or more controllers and multiple target devices can operate on an I²C bus. In this lesson, the Arduino will be the controller.
Older documentation often uses the terms master and slave for these roles. In modern documentation, controller and target are becoming more common.
I²C and TWI
In Arduino and AVR microcontroller documentation, we often encounter the term TWI, meaning Two-Wire Interface, alongside I²C.
For a beginner using Arduino, I²C and TWI can essentially be associated with the same compatible two-wire communication solution. We use the Wire library for both in Arduino programs.
How are the devices connected?
Every participant on the I²C bus connects to the same SDA and SCL lines.
Arduino SDA ─────┬──── sensor SDA
├──── display SDA
└──── clock module SDA
Arduino SCL ─────┬──── sensor SCL
├──── display SCL
└──── clock module SCL
Arduino GND ─────┴──── common GND
The devices are therefore not connected in a chain in the sense that data must pass through each of them. Electrically, every participant is connected to the same two signal lines.
SDA and SCL
Addresses, data and acknowledgement bits are transmitted over the SDA line.
The SCL line provides the timing for data transfer. Under normal conditions, the active controller generates the clock. Some target devices, however, may temporarily hold the SCL line LOW when they need more time. This is called clock stretching.
Arbitration may also occur in systems containing multiple controllers, but its detailed operation is beyond the scope of this beginner lesson.
Why are pull-up resistors required?
I²C participants can typically pull the SDA and SCL lines LOW. Pull-up resistors connected to the supply voltage establish the HIGH logic level.
VCC VCC
│ │
[R] [R]
│ │
SDA SCL
│ │
└──── I²C devices ────┘
Without pull-up resistors, the HIGH level of the lines would not be properly defined, which could make communication unreliable or prevent it from working.
Many ready-made I²C modules already contain pull-up resistors. This is convenient, but when several modules are connected, their resistors may be connected in parallel. As a result, the equivalent resistance may become too low.
There is no universal resistor value suitable for every circuit. The required value is influenced by factors including:
- the supply and signal voltage;
- the bus capacitance;
- the length of the wires;
- the communication speed;
- the number of connected devices and modules;
- pull-up resistors already fitted to the modules.
For short, simple breadboard circuits, we often encounter values of approximately 4.7 kΩ, but this must not be treated as a rule that applies to every system.
Voltage levels
The HIGH level of an I²C bus is primarily determined by the voltage to which the pull-up resistors are connected.
Before connecting a 5 V Arduino to a module that can tolerate only 3.3 V, we must verify:
- the supply voltage required by the module;
- the signal levels tolerated by its SDA and SCL inputs;
- the voltage to which the module’s pull-up resistors are connected;
- whether a bidirectional I²C level shifter is required.
It is not enough to check only the module’s supply voltage. A module may, for example, accept a 5 V supply while its I²C circuitry uses 3.3 V signal levels.

I²C connectors on Arduino Uno boards
The primary I²C bus of the Arduino Uno R3 and Uno R4 is available at the following points:
| Signal | Uno R3 | Uno R4 Minima | Uno R4 WiFi |
|---|---|---|---|
| SDA | A4 / SDA | A4 / SDA | A4 / SDA |
| SCL | A5 / SCL | A5 / SCL | A5 / SCL |
The separately labelled SDA and SCL connection points found on Uno R3 and R4 boards belong to the primary bus. They do not represent an additional independent I²C bus.
The Qwiic connector on the Uno R4 WiFi uses a separate 3.3 V I²C bus. It is available in the program as Wire1. In this beginner lesson, we use the default Wire bus.
The I²C address
Every target device must have an address so that the controller can select the device with which it wants to communicate.
A typical 7-bit address is, for example:
0x3C
The 0x prefix indicates that the number is written in hexadecimal notation.
Not every value in the normal 7-bit address range can be used as a general device address because the I²C standard reserves certain addresses for special purposes. A general device scanner should therefore examine the following range:
0x08–0x77
Two target devices with the same address on the same bus generally cannot be distinguished from each other. On many modules, the address can be changed using solder bridges or configuration pins.
If this is not possible, a separate bus, an I²C multiplexer or another hardware solution may be required.
The 7-bit address and the R/W bit
During an I²C transfer, the 7-bit device address is followed by a direction bit. This indicates whether the controller wants to write or read.
Suppose the device’s 7-bit address is:
0x3C
The address byte transmitted on the bus may be:
0x78 – write
0x79 – read
The Arduino Wire library, however, must be given the 7-bit address:
const uint8_t deviceAddress = 0x3C;
Wire.beginTransmission(deviceAddress);
We must not pass the left-shifted value 0x78 or 0x79. The Wire library handles the write or read direction.
START, STOP and repeated START
The controller begins an I²C transaction with a START condition.
This may be followed by:
- the target device address;
- the write or read direction;
- one or more data bytes;
- an acknowledgement or negative acknowledgement;
- finally, the STOP condition.
For some devices, a write operation and a read operation must be linked without releasing the bus. In this case, instead of generating a STOP condition, the controller creates another START condition, called a repeated START.
ACK and NACK
After each transferred byte, the receiving participant indicates whether it accepted the byte.
- ACK: the byte was received and acknowledged;
- NACK: no acknowledgement was received.
A NACK may occur, for example, when:
- no device is present at the specified address;
- the device is not powered correctly;
- the wiring is incorrect;
- the voltage level is unsuitable;
- the device is temporarily unable to respond;
- the program uses an incorrect address.
The Wire library
We use the Wire library for I²C communication on Arduino:
#include <Wire.h>
We start the default bus operating as a controller with:
Wire.begin();
This is usually placed in the setup() function:
#include <Wire.h>
void setup()
{
Wire.begin();
}
void loop()
{
}
The parameterless form of Wire.begin() is the appropriate starting point for beginner controller programs.
Sending data
A simple write operation consists of three main steps:
Wire.beginTransmission(deviceAddress);
Wire.write(value);
uint8_t error = Wire.endTransmission();
beginTransmission() specifies the target device’s address and prepares the transmit buffer.
write() places the data to be sent into this buffer.
endTransmission() performs the physical transfer and then returns its result.
A reusable helper function:
bool sendByte(uint8_t deviceAddress, uint8_t value)
{
Wire.beginTransmission(deviceAddress);
Wire.write(value);
uint8_t error = Wire.endTransmission();
return error == 0;
}
Usage:
const uint8_t deviceAddress = 0x3C;
if (sendByte(deviceAddress, 42))
{
Serial.println("Data sent successfully.");
}
else
{
Serial.println("Failed to send data.");
}
Do not run this example with an arbitrary device unless you know its communication protocol. The meaning of a data byte differs from one device to another, and some values may modify settings.
The result of endTransmission()
The return value of endTransmission() should always be examined.
Wire.beginTransmission(deviceAddress);
Wire.write(value);
uint8_t error = Wire.endTransmission();
switch (error)
{
case 0:
Serial.println("Data sent successfully.");
break;
case 1:
Serial.println("The data did not fit in the transmit buffer.");
break;
case 2:
Serial.println("The address was not acknowledged.");
break;
case 3:
Serial.println("A data byte was not acknowledged.");
break;
case 4:
Serial.println("Another I2C error occurred.");
break;
case 5:
Serial.println("The transfer stopped because of a timeout.");
break;
default:
Serial.print("Unknown Wire error code: ");
Serial.println(error);
break;
}
The result code for a successful operation is 0.
Different Arduino cores may introduce additional error codes. Portable programs should therefore treat unknown non-zero values as errors in addition to the documented codes.
Requesting data
When reading data, the controller specifies:
- the target device address;
- the number of requested bytes.
Requesting one byte:
size_t receivedCount =
Wire.requestFrom(deviceAddress, static_cast<uint8_t>(1));
if (receivedCount == 1 && Wire.available() > 0)
{
int receivedValue = Wire.read();
Serial.print("Received value: ");
Serial.println(receivedValue);
}
else
{
Serial.println("The expected data was not received.");
}
requestFrom() starts the read transaction and returns the number of bytes placed in the receive buffer.
Wire.available() reports how many bytes are available to read.
Wire.read() removes one byte from the receive buffer. Its return type is int, so we should read only after verifying that data is actually available.
Selecting a register and reading data
Many I²C devices use internal registers. A common sequence is:
- send the number of the register we want to read;
- do not release the bus completely;
- switch to reading using a repeated START;
- request the register’s contents.
The basic pattern is:
const uint8_t deviceAddress = 0x3C;
const uint8_t registerAddress = 0x01;
Wire.beginTransmission(deviceAddress);
Wire.write(registerAddress);
uint8_t error = Wire.endTransmission(false);
if (error == 0)
{
size_t receivedCount =
Wire.requestFrom(deviceAddress, static_cast<uint8_t>(1));
if (receivedCount == 1 && Wire.available() > 0)
{
int registerValue = Wire.read();
Serial.print("Register value: ");
Serial.println(registerValue);
}
}
endTransmission(false) tells the library not to send a STOP condition. The following read operation can therefore be connected to the preceding write using a repeated START.
This is only a general pattern. The actual register addresses, data format and sequence of operations must always be determined from the device’s data sheet.
I²C address scanner
If we do not know a module’s address, an address scanner can test which normal 7-bit addresses respond.
#include <Wire.h>
const uint8_t firstAddress = 0x08;
const uint8_t lastAddress = 0x77;
void setup()
{
Serial.begin(9600);
Wire.begin();
Serial.println("I2C address scanner started.");
}
void loop()
{
uint8_t foundDevices = 0;
Serial.println();
Serial.println("Scanning...");
for (uint8_t address = firstAddress;
address <= lastAddress;
address++)
{
Wire.beginTransmission(address);
uint8_t error = Wire.endTransmission();
if (error == 0)
{
Serial.print("Device found at 0x");
if (address < 0x10)
{
Serial.print('0');
}
Serial.println(address, HEX);
foundDevices++;
}
else if (error != 2)
{
Serial.print("Wire error at address 0x");
if (address < 0x10)
{
Serial.print('0');
}
Serial.print(address, HEX);
Serial.print(". Error code: ");
Serial.println(error);
}
}
if (foundDevices == 0)
{
Serial.println("No responding I2C device found.");
}
else
{
Serial.print("Number of devices found: ");
Serial.println(foundDevices);
}
delay(5000);
}
Open the Serial Monitor at 9600 baud.
The program:
- scans the
0x08–0x77range; - initiates an empty write transaction at every address;
- prints the address when an acknowledgement is received;
- repeats the scan after five seconds.
The address scanner is a useful diagnostic tool, but it does not prove that a module is fully operational. A device may acknowledge its address even when its sensing or configuration functions are not working correctly.
Connect a scanner to a special-purpose or already operating system only when you know that the empty write attempts will not interfere with its devices.
Devices with identical addresses
If two target devices on the same bus use the same address, both may respond to the same transaction.
Possible solutions include:
- changing the address of one device;
- using address-selection solder bridges;
- placing the devices on separate I²C buses;
- using an I²C multiplexer;
- using enable or shutdown pins if supported by the device.
Always select the solution based on the relevant module’s data sheet.
Speed
I²C defines several speed modes. In Arduino projects, we often encounter:
- 100 kHz Standard-mode;
- 400 kHz Fast-mode.
The bus clock can be configured using setClock():
Wire.setClock(400000);
Use this only when:
- the Arduino core supports it;
- every connected device supports the selected speed;
- the wiring and pull-up resistors are suitable for that speed.
A higher configured clock rate does not guarantee that the complete system will operate reliably at that speed.
Wire length and bus capacitance
I²C was primarily designed for short connections within a device or on a circuit board.
There is no maximum cable length that applies to every system. In addition to wire length, operation is influenced by:
- the capacitance of the wires and connectors;
- the pull-up resistors;
- the clock rate;
- electrical noise;
- grounding;
- signal-level conversion;
- the number of devices.
On a breadboard, start with short wires. If the system operates reliably only at a lower clock rate, first examine the wiring, pull-up resistors and signal levels.
The Wire buffer
Data passed to Wire.write() is placed in a temporary buffer.
The buffer size is not identical on every Arduino platform:
- the classic AVR-based Uno R3 implementation typically uses a 32-byte buffer;
- the Renesas-based Uno R4 implementation may use a different, larger buffer;
- other boards and Arduino cores may apply different limits.
For portable programs:
- avoid unnecessarily large single transactions;
- check the documentation for the platform being used;
- inspect the result of
endTransmission(); - divide data into smaller parts when necessary and when the target device’s protocol allows it.
Controller and target modes
In this lesson, the Arduino operated as a controller:
Wire.begin();
An Arduino can also operate as a target device. In this case, it receives an address and can respond to incoming data or a controller’s read request using event handlers.
The relevant operations include:
Wire.begin(targetAddress);
Wire.onReceive(receiveHandler);
Wire.onRequest(requestHandler);
Target mode introduces event-handling, timing and platform-dependent limitations, so it will be discussed in detail in a later advanced lesson.
Common errors
No common ground
In addition to SDA and SCL, separately powered circuits need a common reference, meaning a GND connection.
SDA and SCL swapped
Check the labels on the board and module. Do not rely solely on wire colours.
Incorrect address
A module’s documentation may list several addresses, or its address may be changed using solder bridges. Use an address scanner.
Confusing 7-bit and 8-bit addresses
If a data sheet gives separate write and read addresses, check whether they already include the R/W bit. The Wire library generally requires the 7-bit device address.
Incorrect voltage level
Check the voltage to which the pull-up resistors are connected and verify that every device can tolerate it.
Too many pull-up resistors
The built-in pull-up resistors of several modules may be connected in parallel, producing an equivalent resistance that is too low.
Wires too long or clock rate too high
Shorten the wires, improve the ground arrangement and try a lower clock rate.
Error code not checked
The program must not automatically assume that a transfer succeeded. Examine the result of endTransmission() and the number of bytes actually received.
Summary
In this lesson, we learned that:
- I²C/TWI is a synchronous communication bus using two signal lines;
- SDA is the data line and SCL is the clock line;
- both signal lines require suitable pull-ups;
- several target devices can share the same bus;
- target devices are selected using their 7-bit addresses;
- the
Wirefunctions receive the 7-bit address; Wire.begin()initializes the default bus;- for writing, we use
beginTransmission(),write()andendTransmission(); - for reading, we use
requestFrom(),available()andread(); endTransmission(false)allows transactions to be linked using a repeated START;- the default I²C bus of the Uno R3 and R4 is available on A4/SDA and A5/SCL;
- the Uno R4 WiFi also has a separate
Wire1bus; - resistor values, wire length, speed, buffer size and device count cannot be handled using a single universal rule.
Exercises
1. Run the address scanner
Connect a known I²C module and run the address scanner. Record the detected address in hexadecimal form.
2. Try an incorrect address
Change the address in a data-sending example to a value at which no device is present. Examine the error code returned by endTransmission().
3. Convert an address
A data sheet specifies 0x78 as the write address and 0x79 as the read address. Determine the 7-bit address that must be passed to the Wire functions.
4. Check a circuit
Before connecting a 5 V Arduino to a 3.3 V I²C module, find:
- the module’s permitted supply voltage;
- the permitted signal level on SDA and SCL;
- the position and voltage of the pull-up resistors;
- whether a bidirectional level shifter is required.
