Sub topic 4
How I2C works: two wires and an address
Assume that you can upload an Arduino sketch, use setup() and loop(), and wire power, ground, and signal lines on a breadboard. You should also know how to install an Arduino library. This chapter explains the I2C bus, which later lets you connect an LCD backpack or an SSD1306 OLED with only two signal wires.
The I2C working model
I2C, usually pronounced “I-squared-C,” is a communication bus. A bus lets several devices share the same signal wires. One device acts as the controller, usually the Arduino. Other devices act as targets, such as a display, sensor, or memory chip.
I2C uses two signal lines:
SDAcarries data.SCLcarries the clock signal that controls the timing of data bits.
All devices on the bus also need a shared ground. Power is usually provided separately through VCC and GND.
The bus is shared, but each target has an address. The controller places an address on the bus before sending or requesting data. Only the target with that address responds.
Common 7-bit addresses include 0x27 for some LCD backpacks and 0x3C for many SSD1306 OLED modules. These are common values, not guarantees. A module may use a different address because of its hardware design or solder-jumper settings.
What happens during a transfer
I2C signals use an electrical design called open-drain, or open-collector. Devices actively pull a line low, but they do not actively drive it high. Pull-up resistors bring the lines back to a high voltage when no device is pulling them low.
A normal transfer follows this pattern:
- The controller sends a start condition.
- The controller sends the target's 7-bit address and a read/write bit.
- The addressed target sends an acknowledgment, called an ACK.
- The controller sends data or asks the target for data.
- The controller sends a stop condition.
The Arduino Wire library hides the individual clock pulses and conditions. You normally provide the 7-bit address and the bytes to transmit.
#include <Wire.h>
const byte targetAddress = 0x27;
void setup() {
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(targetAddress);
Wire.write(0x00); // Example data byte for the target
byte result = Wire.endTransmission();
if (result == 0) {
Serial.println(F("Target acknowledged the transfer."));
} else {
Serial.print(F("Transfer failed, code "));
Serial.println(result);
}
}
void loop() {}beginTransmission() selects the target address. write() places bytes in the outgoing buffer. endTransmission() sends the address and buffered bytes, then returns a status code. A result of 0 means that the target acknowledged the transfer.
The example assumes a device exists at 0x27. If no device has that address, the transfer fails. It does not prove that the target is an LCD, because I2C addresses identify devices but do not describe their function.
Wiring and electrical limits
On an Arduino Uno or Nano, the I2C pins are usually:
A4forSDAA5forSCL
On an Arduino Mega, SDA and SCL are separate pins near the top of the board. Use the labels printed on your board when possible.
| Connection | Purpose |
|---|---|
| `SDA` to `SDA` | Shared data line |
| `SCL` to `SCL` | Shared clock line |
| `GND` to `GND` | Common electrical reference |
| `VCC` to the module supply | Powers the target |
I2C modules normally need pull-up resistors on SDA and SCL. Many LCD backpacks and OLED breakout boards already include them. If several boards each include pull-ups, the resistors are connected in parallel. Too many pull-ups can make the bus draw excessive current when a line is low. Too few, or no pull-ups, can make the signals rise too slowly.
Check logic voltage before connecting a module. A 3.3 V target may not tolerate 5 V signals, while a 5 V Arduino may need a level shifter for safe communication. Also check whether the module's VCC pin accepts the voltage you plan to use.
Sharing the bus
Several targets can use the same SDA and SCL wires if each has a different address. The controller selects one target at a time. Two devices with the same address may respond together, causing corrupted data or preventing either device from working correctly. Address jumpers, solder bridges, or an I2C multiplexer can solve that conflict.
A later scanner sketch can test the bus and report which addresses acknowledge. Use that result when configuring an LCD backpack or OLED instead of guessing.
Exercise
Why can an LCD backpack and an OLED share SDA and SCL without receiving each other's data?
Show answer
Each target has a different I2C address. The Arduino sends an address first, so only the matching target acknowledges and handles the transfer.