Sub topic 2
Two wires: SDA, SCL and open-drain lines
This chapter explains the electrical side of I2C: what the two wires carry, why no device ever drives a line high, and what that means for your wiring. It assumes you can write a basic Arduino sketch, use the Serial Monitor, wire a breadboard, and read binary and hexadecimal numbers. You do not need to know the transaction format yet. That comes in a later chapter.
The two signals
I2C (Inter-Integrated Circuit) connects many devices using only two shared signal wires, plus power and ground.
- SCL (Serial Clock) carries the clock. The controller generates it, and it tells every device when to sample the data line.
- SDA (Serial Data) carries the data bits. Both the controller and the targets use it, but never at the same moment.
Every device on the bus connects to the same SDA wire and the same SCL wire. All devices must also share a common ground. Without a shared ground, the voltage levels on the two wires mean nothing to a device that uses a different reference.
On a classic Arduino Uno, SDA is pin A4 and SCL is pin A5. On the Uno R3 they are also broken out as two extra header pins near AREF. Other boards use different pins, so check the board's pinout. In code you never name these pins. Wire.begin() configures them for you.
| Board | SDA | SCL |
|---|---|---|
| Uno R3, Nano | A4 | A5 |
| Mega 2560 | 20 | 21 |
| Leonardo | 2 | 3 |
Why a shared wire needs special drivers
Think about two ordinary digital outputs wired together. If one pin drives HIGH (connected internally to the supply voltage) and the other drives LOW (connected internally to ground), current flows straight from supply to ground through the two output stages. This is a short circuit. It can damage one or both chips, and the voltage on the wire is undefined.
A bus where any device may talk at any time needs a scheme where such a clash cannot happen. I2C solves this with open-drain signaling.
Open-drain signaling
An open-drain output has only one active switch: a transistor that connects the line to ground. The name comes from the transistor's drain terminal, which is left "open", with no internal connection to the supply.
An open-drain device can do exactly two things to a line:
- Pull it low. The transistor turns on and connects the line to ground.
- Release it. The transistor turns off, and the device stops influencing the line.
A device never drives the line high. A line is high only when nobody is pulling it low, and something else, an external pull-up resistor, brings the voltage up to the supply level. The next chapter covers pull-ups in detail. For now, treat the resistor as a weak connection from each line to the supply voltage.
The resulting logic
Because the line is high only when every device has released it, the bus behaves like a logical AND of all the devices' outputs.
| Device A | Device B | Line level |
|---|---|---|
| Releases | Releases | High (pull-up) |
| Pulls low | Releases | Low |
| Releases | Pulls low | Low |
| Pulls low | Pulls low | Low |
This is sometimes called a wired-AND. It has three practical consequences.
- Clashes are harmless. If two devices pull low at once, the line goes low and nothing is damaged. A clash between a "high" driver and a "low" driver cannot happen, because no device has a high driver.
- Any device can claim the line. Pulling low always wins over releasing. That is what lets several devices share one line.
- The line can be read while it is being driven. A device can pull a line low and also check its level. If the device tries to release the line but sees it low, it knows another device is holding it down.
The last point is the basis of two I2C features. A device can detect that another controller is using the bus (arbitration), and a target can hold SCL low to make the controller wait (clock stretching). This tutorial covers clock stretching in its own chapter.
How the bus signals bits
With open-drain lines, a bit on SDA is decided by who pulls low:
- A 0 bit is sent by pulling SDA low.
- A 1 bit is sent by releasing SDA and letting the pull-up raise it.
The rule for reading is simple. The sender sets SDA while SCL is low. The receiver samples SDA while SCL is high. During the high phase of SCL, SDA must stay stable. The only times SDA may change while SCL is high are the special START and STOP conditions, which mark the beginning and end of a transaction.
SCL is also open-drain. Normally the controller is the only device that pulls it low, but a target may hold it low as well. This makes SCL a shared line in the same way SDA is.
Who drives the wires
The roles change by line and by moment:
| Line | Controller | Target |
|---|---|---|
| SCL | Pulls low and releases to produce the clock | Normally only listens. May hold low to ask for more time |
| SDA (controller sends) | Pulls low or releases for each bit | Listens |
| SDA (target sends) | Releases the line and listens | Pulls low or releases for each bit |
| SDA (acknowledge) | Acknowledges data it received | Acknowledges address and data it received |
An acknowledge is a target (or the controller, when it reads data) pulling SDA low for one clock cycle to say "received". A later chapter covers it. Note how it uses the open-drain rule: the sender releases SDA for that bit, and the receiver pulls it low. If nobody pulls it low, the pull-up leaves it high, and that high level means "no acknowledge".
What this means for your wiring
Pull-ups are not optional
An open-drain bus with no pull-up never goes high. Both lines would float, the clock would never rise, and every transaction would fail. Many breakout boards include pull-up resistors, so a bus often works without you adding any. This hides the requirement until you connect a bare chip, or until you connect many boards whose pull-ups combine into a value that is too low. The next chapter covers how to choose values.
Idle state is high
When nobody is using the bus, both SDA and SCL are released, so both read high. A quick check with a multimeter on an idle bus should show the supply voltage on both lines. If a line reads close to 0 V while idle, either a device is holding it low, or the line is shorted to ground.
The rise is slower than the fall
A transistor pulls a line low quickly. The pull-up resistor raises it slowly, because the resistor must charge the capacitance of the wires and device pins. This asymmetry limits how fast the bus can run and how long the wires can be. Later chapters on bus speed and bus capacitance return to it.
All devices must agree on the logic level
A device that treats 3.3 V as its supply cannot safely sit on a line that a pull-up holds at 5 V. A later chapter on level shifting covers this.
A first look in code
You do not control the line drivers yourself. The Wire library does it through the AVR's hardware two-wire interface (called TWI). Your sketch only needs to start the library:
#include <Wire.h>
void setup() {
Serial.begin(9600);
Wire.begin(); // join the bus as the controller; enables SDA and SCL
Serial.println("Wire started");
}
void loop() {
}Wire.begin() with no argument makes the board the controller. On AVR boards such as the Uno, it also enables the chip's internal pull-up resistors on SDA and SCL. These internal pull-ups are weak, roughly 20 kΩ to 50 kΩ, which is too weak for most buses. Treat them as a convenience for a very short bus with one device, not as a design. Use external pull-ups for anything else.
Common mistakes at this level
- Leaving out the common ground. Two boards connected only by SDA and SCL often show random behavior. Connect their grounds.
- Assuming "no response" means "broken code". An idle line stuck low, or a line that never rises, produces the same silence as a wrong address.
- Measuring only the average voltage. A multimeter shows a steady level, but not a slow rise edge. An oscilloscope or logic analyzer shows whether the edges are clean.
- Swapping SDA and SCL. The bus fails silently. The labels on breakout boards are small, so check them first.
Summary
SDA carries data and SCL carries the clock. Both are open-drain: devices can only pull a line low or release it, and a pull-up resistor makes the line high. This makes the bus a wired-AND, so collisions between devices cannot cause a short circuit, and it lets one wire serve many devices. It also means the pull-up resistors are part of the design.
Exercise
Two targets on the same SDA line are both sending. Target A sends a 1 bit and target B sends a 0 bit at the same time. What level does the line have, and why is there no electrical damage?
Show answer
The line is low. Target A sends a 1 by releasing the line, and target B sends a 0 by pulling it to ground. Pulling low overrides the pull-up, so the line is low. No damage occurs because neither device drives the line high, so no current path from the supply to ground exists except through the pull-up resistor, which limits the current.
Exercise
You power up an Arduino with a bare I2C chip (no breakout board) wired to SDA and SCL, and add no resistors. Why would you expect the bus to fail, and what is the smallest fix?
Show answer
With no pull-up, open-drain lines can only be pulled low and never rise, so SDA and SCL float and the signals are invalid. The smallest fix is to add one external pull-up resistor from each of SDA and SCL to the supply voltage, typically in the low kilohm range such as 4.7 kΩ.