Simvik

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 ohm

5 V standard mode, a long bus

show(5000, 1000, 200);

Expected output:

5000 mV, 1000 ns, 200 pF: 1534 to 5901 ohm

Learn it first

  • I2C · Addressing, repeated start, clock stretching and bus recovery.

More I2C problems