Sub topic 4
How I2C works: two wires and an address
This chapter explains the I2C bus well enough that you can reason about it when a display does not respond. It assumes you can upload a sketch, wire a breadboard with power, ground, and signal wires, and install a library from the Library Manager. It also assumes you have read the chapter on powering and wiring a display safely, so you already know about shared ground and logic levels.
The next chapter uses this model to find a display's address with a scanner sketch. Later chapters use it to drive an LCD backpack and an SSD1306 OLED.
Why displays use I2C
A 16x2 character LCD in 4-bit parallel mode needs six Arduino pins for signals, plus power, ground, and contrast wiring. An OLED has more pixels to update, and a parallel bus would use even more pins.
I2C (Inter-Integrated Circuit, said "I-squared-C" or "I-two-C") is a serial bus that needs only two signal wires, however many devices share it. A serial bus sends data one bit at a time over a small number of wires. The two I2C signal wires are:
- SDA (serial data): carries the data bits in both directions.
- SCL (serial clock): carries the clock pulses that tell every device when to read or change a bit on SDA.
Both lines are shared by every device on the bus. Each device also needs power and ground. A typical I2C display module therefore has four pins: GND, VCC, SDA, and SCL.
Controller and targets
Every I2C bus has at least one controller, the device that starts every transfer and generates the clock on SCL. Older documents call it the "master." The other devices are targets (older: "slaves"), which listen and respond only when the controller addresses them.
In nearly all Arduino display projects, the Arduino is the only controller. The display, and any sensors you add later, are targets. The Arduino Wire library can also make a board act as a target, but you do not need that here.
Addresses
All targets share the same two wires, so the controller needs a way to say who it is talking to. Each target has a 7-bit address, a number from 0 to 127. At the start of every transfer, the controller sends the address of the device it wants. Targets whose address does not match ignore the rest of the transfer.
Some points about addresses:
- Addresses 0x00 to 0x07 and 0x78 to 0x7F are reserved for special purposes. This is why the scanner sketch in the next chapter loops from 1 to 126, and why real devices use addresses in the range 0x08 to 0x77.
- The address is usually fixed by the chip's design, with one or more pins or solder jumpers that let you choose among a few values.
- Two devices with the same address on the same bus will conflict. Both try to answer, and the data becomes corrupted or unreadable.
- Addresses are usually written in hexadecimal with a
0xprefix, for example0x27.
Common addresses for display hardware:
| Device | Typical address | How to change it |
|---|---|---|
| LCD backpack with PCF8574 chip | 0x27 | Solder jumpers A0 to A2 (range 0x20 to 0x27) |
| LCD backpack with PCF8574A chip | 0x3F | Solder jumpers A0 to A2 (range 0x38 to 0x3F) |
| SSD1306 OLED, 128x64 or 128x32 | 0x3C | Often a solder pad or resistor position selects 0x3D |
These are typical values, not guarantees. Clone modules vary, and the number printed on the board is sometimes wrong. The scanner in the next chapter reports what the hardware actually answers to.
The 7-bit versus 8-bit address trap
On the wire, the controller sends one byte after the start of a transfer. The top 7 bits are the address, and the lowest bit says whether the controller wants to write to the target (0) or read from it (1). Some datasheets and some vendors quote that whole byte as the "address." An OLED listed as 0x78 in a datasheet is the 8-bit write form of the 7-bit address 0x3C, because 0x3C shifted left by one bit is 0x78.
The Arduino Wire library always uses the 7-bit form. If a datasheet gives an address above 0x77, shift it right by one bit before using it in a sketch.
What happens during a transfer
A write to a target follows a fixed sequence:
- The controller creates a start condition: it pulls SDA low while SCL is high. Every other device on the bus notices that a transfer is starting.
- The controller sends the 7-bit address and the read/write bit, clocking out one bit per SCL pulse.
- The addressed target pulls SDA low for one clock pulse. This is the ACK (acknowledge). If SDA stays high, that is a NACK (not acknowledge), which usually means no device answered.
- The controller sends data bytes. After each byte, the target sends an ACK.
- The controller creates a stop condition: it releases SDA to go high while SCL is high. The bus is free again.
The sequence diagram below shows a write of one data byte.
The ACK in step 3 is what the scanner sketch uses. It tries each address and checks whether anything acknowledged. You will see this directly in the next chapter.
Open-drain lines and pull-up resistors
I2C devices never drive SDA or SCL high. They only pull the line low, or let go of it. This is called open-drain (or open-collector) output. A pull-up resistor connected from each line to the supply voltage pulls the line high whenever nobody is holding it low.
This design lets several devices share one wire without a short circuit. If one device could drive a line high while another drove it low, current would flow between them and could damage them. With open-drain outputs, the worst case is that several devices pull the line low together, which is harmless.
Pull-up resistors matter in practice:
- Most display modules include pull-up resistors on the board, often 4.7 kΩ or 10 kΩ. Many Arduino boards add their own weak internal pull-ups when
Wire.begin()runs, but these are weak, around 20 to 50 kΩ. - When you connect several modules, their on-board pull-ups are in parallel, so the combined resistance drops. Too many modules can pull the combined value so low that devices struggle to pull the line down, which causes errors. If you chain several boards, check whether you can remove or disable some pull-ups.
- If the pull-ups are missing, the lines float and the bus fails or behaves erratically. A bare chip on a breadboard needs external pull-ups, with 4.7 kΩ a common starting value at 5 V.
Speed
The clock frequency sets how fast bits move. Arduino's Wire library defaults to 100 kHz, called standard mode. Many devices also support 400 kHz fast mode, which you can request with Wire.setClock(400000) after Wire.begin().
A faster clock helps with an OLED, where a full frame is about 1 KB of data. At 100 kHz a full-screen update takes roughly 100 ms in the best case, because each byte needs nine clock pulses including its ACK. At 400 kHz the same update takes a quarter of that time. For a 16x2 LCD, the data volume is tiny and 100 kHz is plenty.
Higher speeds put stricter demands on wiring. Long wires add capacitance that slows the rising edge of the signal, and the pull-up resistors then cannot restore the high level quickly enough. If a display works at 100 kHz but fails at 400 kHz, shorten the wires or lower the pull-up resistance within the limits above.
Wiring I2C on common boards
The Wire library uses fixed hardware pins on each board. You do not choose them in the sketch.
| Board | SDA | SCL |
|---|---|---|
| Arduino Uno, Nano | A4 | A5 |
| Arduino Mega 2560 | 20 | 21 |
| Arduino Leonardo | 2 | 3 |
| Arduino Uno R4 | A4 (also on the SDA header pin) | A5 (also on the SCL header pin) |
Newer Uno-style boards also have dedicated SDA and SCL pins near the AREF pin. These connect to the same lines as A4 and A5 on the Uno.
Connect:
- The display's GND to an Arduino GND pin.
- The display's VCC to 5 V or 3.3 V, whichever the module is rated for.
- The display's SDA to the board's SDA pin.
- The display's SCL to the board's SCL pin.
Do not cross SDA and SCL. Swapping them is the most common wiring mistake, and the symptom is that the scanner finds nothing.
Logic levels
The module's supply voltage and the board's logic level must be compatible. A 5 V Arduino with 3.3 V-only targets can damage those devices, because the pull-ups may pull SDA and SCL up to 5 V. Many display modules include a voltage regulator and tolerate 5 V on VCC, but check your module's documentation. If a device is strictly 3.3 V, power the pull-ups from 3.3 V or use a level shifter designed for I2C.
The Wire library in a sketch
You rarely write raw I2C code for a display because libraries do it for you. Knowing the three basic calls makes library behavior and error codes easier to read.
#include <Wire.h>
const byte DISPLAY_ADDRESS = 0x3C; // example 7-bit address
void setup() {
Serial.begin(9600);
Wire.begin(); // join the bus as the controller
Wire.beginTransmission(DISPLAY_ADDRESS); // start building a transfer
Wire.write(0x00); // example data bytes
Wire.write(0xAE);
byte result = Wire.endTransmission(); // send it and get a status
Serial.print("endTransmission returned ");
Serial.println(result);
}
void loop() {}The sketch queues data between beginTransmission() and endTransmission(), and endTransmission() sends the whole transfer and returns a status code. The bytes shown, 0x00 then 0xAE, are the SSD1306 "control byte" and "display off" command, which are harmless and make a sensible test if an SSD1306 is at that address. For other devices the bytes mean something different, so change them if you use this on other hardware.
The return value of endTransmission() tells you what happened:
| Code | Meaning |
|---|---|
| 0 | Success. The target acknowledged. |
| 1 | Data too long for the buffer (the `Wire` buffer is 32 bytes on AVR boards). |
| 2 | NACK on the address. No device answered at that address. |
| 3 | NACK on a data byte. The device exists but refused the data. |
| 4 | Other error, such as a bus problem. |
| 5 | Timeout (on some boards and library versions). |
Code 2 is the one you will see most often. It means the address is wrong, the wiring is wrong, or the device has no power.
Edge cases and failure modes
- Address conflicts. Two modules with the same address cannot share a bus. Change one address with its jumpers, or put them on separate buses. Two OLEDs, for example, need different addresses.
- Bus stuck low. If a device is reset in the middle of a transfer, it may hold SDA low and the bus will hang. Power-cycling the whole circuit clears it. Some
Wirelibrary versions have a timeout to avoid hanging forever, but older AVR versions can block indefinitely. - Missing ground. A display that has power from a different supply but no common ground with the Arduino produces random or no responses.
- Long wires. I2C is designed for short connections on a board or between nearby boards. Beyond about half a meter of ordinary jumper wire, expect problems, and reduce the clock speed first.
- Buffer limit. AVR boards limit one
Wiretransfer to 32 bytes. Display libraries split their data into chunks to stay within this. That is why a library written for an ARM board may behave differently on an Uno.
Summary
I2C connects an Arduino to a display with two shared signal wires. The Arduino is the controller and starts every transfer. Each target has a 7-bit address, and a transfer begins with that address and a read/write bit. The target confirms with an ACK. Open-drain outputs and pull-up resistors keep the shared lines safe. The clock runs at 100 kHz by default and can usually be raised to 400 kHz. The endTransmission() return code reports whether a device acknowledged, which is the idea behind the scanner in the next chapter.
Exercise
A datasheet says an OLED "slave address" is 0x78, but your sketch uses Adafruit_SSD1306 and the display never responds at 0x78. What is the likely cause, what address should you try, and what return code from Wire.endTransmission() would you expect when you probe the wrong address?
Show answer
The datasheet quotes the 8-bit form, which includes the read/write bit. The Arduino Wire library expects the 7-bit address, so shift 0x78 right by one bit to get 0x3C (some modules use 0x3D). Probing the wrong address returns 2, meaning NACK on the address because no device answered.
Exercise
You connect three I2C modules to one Arduino. Each has its own 4.7 kΩ pull-up resistor on SDA and SCL, and the bus becomes unreliable. Why might that happen, and what can you change?
Show answer
The three pull-ups are in parallel, so the total resistance falls to about 1.6 kΩ. That is strong enough that devices may struggle to pull the line fully low, which causes errors. Remove or disable the pull-ups on two of the modules (many boards have a solder jumper or removable resistors), leaving one set on the bus.
Exercise
Name the two signals of an I2C bus and say which device generates each clock pulse. Then explain why two devices with the same address cannot share one bus.
Show answer
The signals are SDA (data) and SCL (clock). The controller, normally the Arduino, generates the clock on SCL. The controller selects a target by sending its address, so two targets with the same address would both acknowledge and both reply, and their responses would collide and corrupt the data.