Sub topic 3
Why the bus needs pull-up resistors
This chapter assumes you know the roles of SDA and SCL and the idea of open-drain signaling from the previous chapter. You should also be comfortable with Ohm's law and with reading a Serial Monitor. Here you will see why an I2C bus cannot work without pull-up resistors. You will also learn how to choose their value and how to check that your bus has them.
What a pull-up does
Every device on an I2C bus drives SDA and SCL in only one direction. A device can pull a line down to ground, or it can let go of the line. No device ever drives a line up to the supply voltage. This is open-drain signaling (called open-collector in older parts).
Nothing in the devices makes the line high. If no device pulls the line low, it floats, and a floating wire has no defined logic level. A resistor from each line to the supply voltage fixes this. It is called a pull-up resistor. It does two things:
- When every device has released the line, the resistor pulls it up to the supply voltage. The bus reads as logic 1.
- When any device pulls the line low, the resistor limits the current that flows to ground. The line reads as logic 0.
This is why two devices can never fight over the bus. If one device pulls low and another "releases", the result is simply low. The bus behaves as a wired-AND: the line is high only if every device lets it be high. Clock stretching and arbitration both rely on this property.
Rising edges are slow, falling edges are fast
The two directions of a transition are not symmetric.
- Falling edge: a transistor inside a device switches on and drains the line quickly.
- Rising edge: the line is a capacitor. It includes the pin capacitance of every device, the wire, and the breadboard. The pull-up resistor has to charge that capacitor through its own resistance, so the line rises along an RC curve.
The I2C specification measures rise time between 30% and 70% of the supply voltage. For a simple RC circuit this time is:
tr = 0.8473 × Rp × CbHere Rp is the pull-up resistance and Cb is the total bus capacitance. A larger resistor or more capacitance gives a slower edge. The specification limits the rise time for each speed mode:
| Bus mode | Clock | Maximum rise time |
|---|---|---|
| Standard | 100 kHz | 1000 ns |
| Fast | 400 kHz | 300 ns |
If the edge is too slow, the receiver may sample the line before it reaches a valid high level. You then see garbled data or NACKs at random.
Choosing a value: two limits
Choosing a pull-up means staying between two limits.
Upper limit: the rise time
Rearrange the rise-time formula to get the largest allowed resistor:
Rp(max) = tr(max) / (0.8473 × Cb)The table shows this limit for several bus capacitances:
| Total capacitance | Standard mode (1000 ns) | Fast mode (300 ns) |
|---|---|---|
| 100 pF | 11.8 kΩ | 3.54 kΩ |
| 200 pF | 5.9 kΩ | 1.77 kΩ |
| 400 pF | 2.95 kΩ | 885 Ω |
Lower limit: the sink current
When a device pulls the line low, it must sink the current through the pull-up and still reach a valid low voltage. The specification assumes a device guarantees a low level of at most 0.4 V while sinking 3 mA. The smallest allowed resistor is therefore:
Rp(min) = (Vcc − 0.4 V) / 3 mA- At 5 V: (5 − 0.4) / 0.003 ≈ 1.53 kΩ.
- At 3.3 V: (3.3 − 0.4) / 0.003 ≈ 967 Ω.
Pick a value between the two limits. Values of 2.2 kΩ to 4.7 kΩ suit most 5 V hobby buses at 100 kHz. For example, with 100 pF at 100 kHz and 5 V the window runs from 1.53 kΩ to 11.8 kΩ, and 4.7 kΩ sits comfortably inside it. A lower value gives sharper edges but draws more current and leaves less margin on the low level. A higher value saves power but slows the edges.
The last row of the table shows a problem. At 400 pF in Fast mode the upper limit is 885 Ω, which is below the 5 V lower limit of 1.53 kΩ. No resistor satisfies both limits. You must reduce the capacitance, lower the clock, or use a device with stronger drivers.
Computing the window in a sketch
The following sketch calculates both limits from values you supply. It runs on any Arduino board that has a Serial port.
// Computes the allowed pull-up window for an I2C bus.
const float VCC = 3.3; // supply voltage in volts
const float BUS_CAP_PF = 150.0; // estimated total bus capacitance in pF
const float RISE_TIME_NS = 300.0; // 1000 for Standard mode, 300 for Fast mode
void setup() {
Serial.begin(9600);
float capFarads = BUS_CAP_PF * 1e-12;
float riseSeconds = RISE_TIME_NS * 1e-9;
float rMax = riseSeconds / (0.8473 * capFarads);
float rMin = (VCC - 0.4) / 0.003;
Serial.print("Rp min (ohms): ");
Serial.println(rMin, 0);
Serial.print("Rp max (ohms): ");
Serial.println(rMax, 0);
if (rMax < rMin) {
Serial.println("No valid pull-up: reduce capacitance or clock speed.");
} else {
Serial.println("Choose a standard value inside this window.");
}
}
void loop() {
}Several pull-ups in parallel
Most breakout boards include their own pull-up resistors, often 4.7 kΩ or 10 kΩ on each line. Pull-ups on one line are connected in parallel, so their combined resistance is lower than any single one. Three boards with 10 kΩ each give 10k / 3 ≈ 3.3 kΩ. Six boards with 4.7 kΩ each give about 783 Ω, which is below the 5 V lower limit.
Adding devices therefore does two things at once. It adds capacitance, which lowers Rp(max). It also adds parallel pull-ups, which lowers the actual resistance. If the combined value drops below Rp(min), the devices cannot pull the line low cleanly. In that case, remove or desolder the pull-ups on some boards, and keep one set of pull-ups for the whole bus.
Arduino's internal pull-ups
On AVR boards such as the Uno, the Wire library turns on the microcontroller's internal pull-ups when you call Wire.begin(). These are roughly 20 kΩ to 50 kΩ. That is too weak for reliable I2C beyond a very short wire at low speed. They also act in parallel with any external resistors, which adds almost nothing. Treat them as a fallback that happens to make one short test wire work. Do not rely on them for a real bus. Other platforms differ. Some boards have no internal pull-ups on the I2C pins, and some Wire implementations do not enable them.
Checking for pull-ups before writing any I2C code
You can test the idle level of the lines without using Wire at all. This sketch sets SDA and SCL as plain inputs, with the internal pull-ups off. It then reads their level. The SDA and SCL pin names are defined by the board package for most boards.
void setup() {
Serial.begin(9600);
pinMode(SDA, INPUT); // INPUT, not INPUT_PULLUP: test the external resistors only
pinMode(SCL, INPUT);
delay(10);
Serial.print("SDA idle level: ");
Serial.println(digitalRead(SDA) ? "HIGH" : "LOW");
Serial.print("SCL idle level: ");
Serial.println(digitalRead(SCL) ? "HIGH" : "LOW");
}
void loop() {
}Both lines should read HIGH. A LOW reading means a pull-up is missing, a device is holding the line down, or the line is shorted to ground. A line that is HIGH in some runs and LOW in others is floating, which means no pull-up is attached. A multimeter set to voltage gives the same information. Measure from each line to ground with the bus idle, and expect a value close to Vcc.
Common errors
- No pull-ups at all. The scanner finds nothing, or
endTransmission()returns 2 (NACK on address). This happens on a bare microcontroller and on bare sensor chips. Breakout boards usually include resistors. - Pull-ups to the wrong voltage. A 3.3 V sensor with a pull-up to 5 V can be damaged, or it can read a level outside its limits.
- Too many parallel pull-ups. The combined resistance falls below Rp(min), and the low level no longer reaches 0.4 V.
- Too large a resistor for the speed. The bus works at 100 kHz and fails after
Wire.setClock(400000).
Exercise
A 3.3 V bus runs at 400 kHz (Fast mode, 300 ns maximum rise time) with an estimated 150 pF of total capacitance. What range of pull-up values is allowed, and is 1.5 kΩ a valid choice?
Show answer
Rp(max) = 300 ns / (0.8473 × 150 pF) ≈ 2.36 kΩ. Rp(min) = (3.3 − 0.4) V / 3 mA ≈ 967 Ω. The window is about 967 Ω to 2.36 kΩ, so 1.5 kΩ is valid. 2.2 kΩ is also valid but leaves less margin on the rise time.
Exercise
You connect three breakout boards to one 5 V bus. Each board has a 10 kΩ pull-up on SDA. What is the combined pull-up resistance on SDA, and is it within the 5 V lower limit?
Show answer
Three equal resistors in parallel give 10 kΩ / 3 ≈ 3.3 kΩ. The 5 V lower limit is about 1.53 kΩ, so 3.3 kΩ is above it and the low level is safe. You still need to check the upper limit against the bus capacitance and clock speed.