Size the pull-up resistors
Work out the smallest and largest pull-up that meet the I2C specification. An easy embedded C problem (I2C), run and graded in the browser.
easy · I2C · i2c · hardware · calculation
I2C lines are open-drain: devices only pull them low, and a resistor pulls them high. Its value has two limits:
- Minimum: when a device pulls the line low it must sink at most 3 mA while holding it at 0.4 V or less: R_min = (VDD − 0.4 V) / 3 mA.
- Maximum: the line must rise to 70 % within the allowed rise time t_r, against the bus capacitance C. The rise takes 0.8473 × R × C, so R_max = t_r / (0.8473 × C).
Write pullup_range(vdd_mv, rise_ns, cap_pf, r_min, r_max), giving both limits in ohms. Round the minimum up and the maximum down, so any value between them is guaranteed to work. Use integer arithmetic (wide enough not to overflow); 0.8473 is 8473 / 10000.
Starter code
void pullup_range(uint32_t vdd_mv, uint32_t rise_ns, uint32_t cap_pf, uint32_t *r_min, uint32_t *r_max) {
*r_min = vdd_mv / 3; // mV / mA = ohm
*r_max = rise_ns * 1000 / cap_pf; // ns / pF = kohm
}Example tests
3.3 V fast mode
show(3300, 300, 100);Expected output:
3300 mV, 300 ns, 100 pF: 967 to 3540 ohm5 V standard mode, a long bus
show(5000, 1000, 200);Expected output:
5000 mV, 1000 ns, 200 pF: 1534 to 5901 ohmLearn it first
- I2C · Addressing, repeated start, clock stretching and bus recovery.
More I2C problems
- Scan the bus · Find every device on the bus without probing the reserved addresses.
- Read a 16-bit register · Write the register pointer, read two bytes back with a repeated start, and handle a NACK.
- Recover a stuck bus · Clock a stuck device out with up to nine pulses, then send a STOP.
- Driver: write an EEPROM over I2C, with ACK polling · Send an I2C write with the hardware's byte-by-byte handshake, then poll until the EEPROM finishes writing.
- Driver: read registers with a repeated start · Write a register address, then read bytes back without releasing the bus in between.
- Fix: every second transfer is cut short · A write routine leaves STOPF set, so the next transfer thinks it has already finished.
- HVAC controller: trust the humidity reading · Combine the bytes an I²C humidity sensor returns, check their CRC, and scale them.