Chapters

31 I2C: Bring-Up and Diagnosis

networking-core
wired
comm
i2c

31.1 Start With the Situation

The bus rules are known, but a real sensor still may not answer. The team must run a repeatable bring-up, inspect timing and pull-ups, and separate address, wiring, arbitration, and device faults.

31.2 Overview

This route turns I2C theory into sensor bring-up and fault diagnosis evidence.

This is part 2 of 2. Review I2C: Transactions and Bus Design when you need the first route.

31.3 Learning Objectives

By the end of this chapter, you will be able to:

  • bring up a BME280 on an I2C bus
  • diagnose pull-up and clock-stretching failures
  • verify arbitration and transaction evidence

31.4 Chapter Roadmap

Follow the original sections below in order. They begin at the reviewed split boundary and keep every worked example, figure, check, and supporting banner with the section that owns it.

StageWhat to doWhat success looks like
Confirm addressScan for 0x76 or 0x77The address matches the sensor’s SDO pin setting.
Initialize driverStart the BME280 library with the detected addressStartup message reports the sensor is ready.
Read slowlyRequest temperature, humidity, and pressure every 2 secondsValues change smoothly and remain physically plausible.
Compare environmentWarm the sensor with your hand or move it near airflowTemperature/humidity respond in the expected direction.
Diagnose failureIf readings fail, inspect bus and address firstMost failures are wrong address, missing pull-ups, or swapped wires.
SymptomLikely causeFix
“Sensor not found”Wrong address or wiringTry 0x76 and 0x77; recheck SDA/SCL and power.
Readings freezeBus lockup or clock stretching timeoutSlow the bus, shorten wires, and add bus recovery.
Values are unrealisticWrong sensor/library modeVerify the exact BME280/BMP280 module and driver settings.

Packet PeteCheckpoint: Bring-Up Evidence

You now know:

  • A scanner proves address-level ACK behavior, not that every register read, sensor mode, or timeout path is correct.
  • A useful lab record names wiring, voltage, pull-ups, observed addresses, and one successful transaction before the driver is trusted.
  • If the same sensor appears only at a lower speed, the failure points toward timing, capacitance, pull-ups, or clock stretching rather than cloud or application code.

The labs show whether devices answer. The remaining design question is whether the electrical edge speed gives those answers enough timing margin.

31.5 Check I²C Arbitration

Check I²C Arbitration

Scenario: You’re designing a robotics platform with two microcontrollers (ESP32 and Arduino) that both need to control a shared OLED display (I2C address 0x3C) and BME280 sensor (0x76). Both MCUs operate as I2C masters on the same bus with 4.7k pull-ups.

Think about:

  1. What happens if both masters try to send a START condition simultaneously?
  2. Why doesn’t this cause electrical damage to the devices?

Key Insight: I2C uses wired-AND arbitration through open-drain outputs. When multiple masters transmit simultaneously, any device pulling SDA LOW wins (LOW overrides HIGH). During arbitration, masters compare transmitted bits to actual bus state:

  • Master transmits 1 (releases line HIGH)
  • Bus reads 0 (another master pulled LOW)
  • First master detects mismatch → backs off gracefully
  • No electrical conflict because open-drain outputs never drive HIGH (only pull LOW or float)

This elegant mechanism prevents bus damage. Push-pull outputs (like SPI) would short VCC to GND if two devices drive opposite levels, potentially destroying the chips. The trade-off: RC charging through pull-up resistors limits rise time, restricting I2C to ~400 kHz vs SPI’s 80+ MHz.

Verify Your Understanding:

  • Calculate arbitration delay: With 400 pF bus capacitance and 4.7 kΩ pull-ups, rise time = 0.8473 × 4,700 Ω × 400 pF = 1.59 µs. Since the I2C standard-mode clock period is 10 µs (100 kHz), this rise time is marginal; for reliable multi-master operation at 400 kHz (2.5 µs period), the rise time budget of 300 ns would be exceeded—requiring 2.2 kΩ pull-ups or reducing bus capacitance.
  • Why faster rise times require smaller pull-ups: At 100 kHz (standard mode), 10k resistors work. At 400 kHz (fast mode), 2.2k needed for adequate rise time.

31.6 Quiz: I2C Protocol

Quiz Questions


Ground quiz questions with the visual at Figure 31.1. Start from I2C Interface Module, but keep LED visible while evaluating i2c provides a simple, efficient interface for connecting low-speed peripherals like sensors, eeproms, and displays. its two-wire design minimizes.

Complete I2C protocol overview showing physical layer (two-wire bus), data layer (address frame with R/W bit, data frames with ACK), and logical layer (master-slave communication with clock stretching and arbitration for multi-master scenarios)
Figure 31.1: I2C provides a simple, efficient interface for connecting low-speed peripherals like sensors, EEPROMs, and displays. Its two-wire design minimizes PCB routing complexity while supporting multiple devices.

Trace the visual from I2C Interface Module to LED in Figure 31.1; verify PWR before concluding. Together those labels make i2c provides a simple, efficient interface for connecting low-speed peripherals like sensors, eeproms, and displays. its two-wire design minimizes testable. Apply their boundary when working through quiz questions.


31.7 Calculate Pull-Up Resistors

Choosing the right pull-up resistor value is the single most common source of I2C failures. Too high and the bus cannot rise fast enough; too low and the devices cannot sink enough current to pull the bus low.

The constraints:

  1. Minimum resistor (max current): I2C spec limits sink current to 3 mA at VOL = 0.4 V. With 3.3 V supply: R_min = (VDD - VOL) / Isink = (3.3 - 0.4) / 0.003 = 967 Ω ≈ 1 kohm
  2. Maximum resistor (rise time): The bus must rise from 0V to 70% of VCC within the rise time specification. Rise time depends on bus capacitance.

Rise time formula:

tr=0.8473×Rpull×Cbust_r = 0.8473 \times R_{pull} \times C_{bus}

Where the maximum rise time is 1000 ns for standard mode (100 kHz) and 300 ns for fast mode (400 kHz).

Calculating bus capacitance:

Capacitance sourceTypical valueExample contribution
Controller SDA/SCL input5-10 pFESP32 pins: 10 pF
Sensor/display input5-10 pF eachBME280: 5 pF; SSD1306: 8 pF
PCB trace1-2 pF/cm15 cm trace at 2 pF/cm: 30 pF
Jumper wires50-100 pF/mKeep jumpers short for fast mode
Example totalSum all connected loadsESP32 + BME280 + SSD1306 + trace ≈ 53 pF

Maximum resistor for 400 kHz (fast mode):

Calculation stepValue
Fast-mode rise-time limit300 ns
Estimated bus capacitance53 pF
Denominator0.8473 × 53 pF = 44.9 pF
Maximum pull-up value300 ns ÷ 44.9 pF = 6.68 kohm

Result: For this 3-device bus at 400 kHz, use pull-ups between 967 Ω and 6.68 kohm. The standard 4.7 kohm falls comfortably in the middle — which is why 4.7 kohm is the universal default recommendation.

When 4.7 kohm does NOT work:

ScenarioProblemFix
Long cable (>30 cm)Bus capacitance >200 pF, rise time too slow at 4.7kUse 2.2 kohm or reduce speed to 100 kHz
Many devices (>8)Combined capacitance >100 pFUse 2.2 kohm pull-ups
3.3V bus with 5V tolerant deviceLogic thresholds mismatchUse level shifter, not just pull-up changes
High-speed mode (3.4 MHz)Rise time budget only 40 ns (≤ 100 pF load); passive pull-ups cannot charge fast enoughUse current-source active pull-ups; passive resistors cannot meet the HS-mode rise-time spec
Breadboard prototypeBreadboard adds 20-50 pF per rowUse 2.2 kohm and limit to 100 kHz

Debugging tip: If I2C works intermittently or only at 100 kHz but fails at 400 kHz, the pull-up resistor value is almost always the cause. An oscilloscope on SDA/SCL will show rounded rising edges (too slow) or ringing (too fast), confirming the diagnosis.

Try It: I2C Pull-Up Resistor Calculator

Packet PeteCheckpoint: Pull-Up Window

You now know:

  • Pull-ups are a range, not a default copied blindly from a breakout board.
  • For the worked 3.3 V, 53 pF, 400 kHz example, the safe window is about 967 ohm to 6.68 kohm, which explains why 4.7 kohm is usually comfortable.
  • When the bus gets longer or gains devices, check the rise-time budget before changing firmware delays or sensor libraries.

Common Pitfalls

Pull-up resistors that are too weak (>10 kΩ) cause slow rising edges that fail at higher speeds. Resistors too strong (<1 kΩ) consume excessive power and may damage open-drain outputs. Fix: calculate the pull-up value based on bus capacitance and target speed; 4.7 kΩ is a safe default for 100 kbps on short buses.

Adding a second I²C temperature sensor with the same address as the first causes them to respond simultaneously, corrupting data. Fix: check address settings (hardware address pins or software configuration) before combining multiple devices of the same type on one bus.

A power glitch during an I²C transaction can leave the SDA line stuck low, locking the bus. The master cannot reset the bus without clocking SCL 9 times to free the stuck slave. Fix: implement a bus recovery routine in firmware that generates 9 SCL pulses to reset any locked slave device.

31.8 Label the Diagram

31.9 Code Challenge

31.10 I2C Shared-Bus Reliability Record

I2C works because every device shares the same two wires and follows the same electrical rule: devices may pull SDA or SCL low, but pull-up resistors bring the lines high again. That makes the bus simple, but it also means one wrong device, weak pull-up, duplicate address, voltage mismatch, or stuck line can affect every device on the bus. The firmware call that says read register 0xF7 from address 0x76 only succeeds when the physical bus, address phase, ACK bits, timing, and recovery assumptions all hold together.

Good I2C design is therefore not just picking a library. It is a bus health record: addresses are unique, pull-ups fit the capacitance and speed, voltage levels match, transactions ACK, and firmware can recover when a slave leaves SDA low. That record matters most in IoT nodes where several low-speed parts share the bus: a BME280 sensor, an EEPROM, a real-time clock, and an OLED display may all look independent in software, but electrically they are attached to the same SDA/SCL pair.

The overview decision is whether the bus boundary is appropriate for the job. I2C is a strong fit for short, local, board-level communication where the controller owns the transaction schedule and peripherals are close enough that capacitance stays controlled. It becomes fragile when a design treats it like an installation cable, mixes voltage domains casually, hides duplicate fixed addresses behind library defaults, or adds modules until the pull-ups can no longer produce clean edges at the chosen speed.

The visual evidence for i2c shared-bus reliability record sits in Figure 31.2. Find I2C Protocol: Reading Temperature from BMP280 beside Master before interpreting i2c reliability is built from linked layers: shared open-drain wiring, an address byte with a read/write bit, ack slots after each byte, and.

An I2C master reads temperature from BMP280 slave 0x76 using START, address+W, register 0xFA, ACK, Re-START and address+R. Data 0x7A ends with NACK and STOP.
Figure 31.2: I2C reliability is built from linked layers: shared open-drain wiring, an address byte with a read/write bit, ACK slots after each byte, and transaction framing with START, repeated START, and STOP conditions.

Use Master to test I2C Protocol: Reading Temperature from BMP280 in the diagram at Figure 31.2. Then inspect Slave 0x76 as the final qualifier on i2c reliability is built from linked layers: shared open-drain wiring, an address byte with a read/write bit, ack slots after each byte, and. That sequence keeps i2c shared-bus reliability record tied to what is visibly labelled. Two shared wires: SDA carries address and data. SCL provides timing. Every attached device sees both lines, so wiring length, module count, connector quality, and breadboard capacitance accumulate into one bus limit.

Open-drain signalling: Devices only pull low. Pull-ups create high levels, set rise time, and determine whether standard mode, fast mode, or Fast-mode Plus is realistic for the assembled hardware.

Addressed turns: Only the addressed device should ACK and respond. Duplicate fixed addresses make two devices answer at once; reserved addresses and shifted address bytes create equally confusing scanner results.

Core trade-off: I2C saves pins and wiring, but it trades that simplicity for shared-bus limits: short distance, bounded capacitance, unique addresses, voltage-domain discipline, and careful recovery from stuck or slow devices. When those limits are too tight, the engineering fix is usually to slow the bus, split it, add a TCA9548A-style multiplexer, move a noisy or distant device to another interface, or change the board layout before firmware workarounds hide a fragile design.

31.11 I2C Bring-Up Record

A reliable I2C build has a short verification record before firmware assumptions harden. The record proves the physical wiring, electrical timing, address map, and recovery behavior under the target clock speed. Start it before sensor-driver integration: record the board voltage, the microcontroller pins assigned to SDA and SCL, the pull-up source, the expected device addresses, and the clock rate that the final product will use.

The practitioner mistake is to treat an I2C scanner result as complete proof. A scan only proves that some device pulled ACK during the address phase. It does not prove the register map is correct, that read/write bytes are formed correctly, that clock stretching is handled, that the bus survives reset timing, or that the same wiring will work at 400 kHz after all devices and cables are installed.

CheckWhat to recordFailure it catches
Physical pathController pins, connector order, shared ground, VDD, level shifter if used, and the final cable or PCB routeSwapped SDA/SCL, floating ground, 5 V sensor on a 3.3 V bus, or breadboard-only success
Address scanObserved 7-bit addresses, expected devices, and any reserved or duplicate addressesWrong address constants, duplicate modules, or missing devices
Pull-up rangeVDD, estimated capacitance, selected resistor, target speed, and measured rise time when possibleSlow edges at 400 kHz or excessive sink current from too-strong pull-ups
Transaction proofSTART, address byte, ACK, register select, repeated START, read/write data, and STOP behaviorSending the unshifted address byte, missing ACKs, or incorrect read/write direction
Recovery pathBus timeout, nine SCL recovery pulses, device reset behavior, and retry limitFirmware hanging forever after a power glitch or stuck SDA line
Release conditionPass/fail evidence at the final speed with all sensors, display modules, and enclosures connectedA prototype that works on the bench but fails after the production harness or housing is fitted

Before coding: Confirm voltage compatibility, shared ground, pull-ups, address pins, and cable length. These are hardware facts, not software preferences, and they should be visible in the schematic or bring-up log before the first driver abstraction is trusted.

During bring-up: Run a scanner, then capture at least one successful register read and one expected failure path such as no device at an unused address. Use the device datasheet register, not only a high-level sensor library example.

Before release: Test at the final clock speed with all devices attached, then verify timeout and bus-recovery code instead of assuming the library handles lockup. Repeat after warm reset and power interruption because stuck-SDA faults often appear during startup sequencing.

Escalation trigger: If the design needs long cables, many devices, high speed, or duplicate fixed addresses, add a multiplexer, reduce speed, split the bus, or choose a more suitable wired interface. For field wiring or noisy industrial cabinets, I2C is usually the wrong boundary; use it inside a board or short enclosure run, then bridge to CAN, RS-485, Ethernet, or a wireless link as the installation distance grows.

31.12 Rise Time Sets the Real I2C Speed

I2C looks digital in code, but the rising edge is analog. The line rises through a resistor into the total bus capacitance, so more devices, longer traces, jumpers, and breadboards all slow the transition from low to high. The receiver samples logic levels at clocked moments; if the edge is still rising, the bus can fail only at higher speed or only with every device attached. This is why the NXP I2C-bus timing limits specify rise-time budgets, not just clock rates.

The useful mental model is an RC charge curve. With a larger pull-up resistor, the device sinks less current when it pulls low, but the high transition is slower. With a smaller pull-up, the edge is faster, but every device must be able to pull the line below the valid-low threshold while sinking the extra current. A design that moves from 4.7 kohm to 2.2 kohm may fix a 400 kHz rise-time failure, but it also changes power and VOL margin.

Addressing and timing failures often look similar from firmware: a missing ACK. The debug path is to separate causes. If an address scan finds nothing, check power, ground, pull-ups, voltage, and address shifting. If a device appears at 100 kHz but fails at 400 kHz, inspect rise time and capacitance. If the bus hangs after resets, implement recovery pulses and timeouts before retrying the transaction.

The same physical rule explains multi-master arbitration. Each controller watches SDA while transmitting; if it releases the line high but another controller pulls it low, it has lost arbitration and must stop driving. That wired-AND behavior prevents direct output contention, but it does not make multi-master systems automatically simple. The software still needs ownership rules, bounded retries, and logging that distinguishes arbitration loss from a missing slave ACK.

Rise-time limit: The pull-up resistor and bus capacitance define whether the line reaches a valid high level before the next clock sample. Fast mode leaves far less time for the same edge than standard mode.

Clock stretching: A slave may hold SCL low while it prepares data. Firmware must tolerate this within a bounded timeout, and the timeout should be measured against the slowest real device on the bus.

Bus lockup: A reset or interrupted transaction can leave SDA low. The master needs an explicit recovery sequence, commonly clocking SCL until the slave releases SDA, then issuing STOP and reinitializing the peripheral.

Debug sequence: Probe idle levels first: both SDA and SCL should sit high at the correct voltage. Then check a START condition, the shifted address byte, and the ACK slot. Only after the electrical and frame evidence is clean should the investigation move to driver configuration, register addresses, or application-layer parsing.

31.13 Knowledge Check

31.14 Summary

This chapter covered I2C (Inter-Integrated Circuit) protocol:

  • Two-wire bus (SDA for data, SCL for clock) supports multiple devices
  • Open-drain outputs with pull-up resistors (typically 4.7k) enable multi-master operation
  • Pull-up value depends on bus capacitance — 4.7k works for most setups, use 2.2k for long cables or many devices
  • 7-bit addressing allows up to 112 devices on a single bus
  • Synchronous protocol with master-controlled clock and optional clock stretching
  • Half-duplex communication with START, STOP, and ACK/NACK conditions
  • Common devices: Temperature sensors (BME280), displays (SSD1306), IMUs (MPU6050), RTCs (DS3231)

31.15 What’s Next

TopicChapterDescription
Wired Protocol FoundationsWired Communication FundamentalsReview synchronous vs asynchronous, open-drain topology, and master-slave architecture before moving to SPI
High-Speed PeripheralsSPI ProtocolLearn the 4-wire full-duplex SPI bus used for SD cards, displays, and ADCs that need speeds far beyond I2C’s 3.4 MHz limit
Long-Distance SerialUART and RS-232 Serial CommunicationExplore point-to-point asynchronous serial communication used for GPS modules, debug consoles, and PC interfaces
Wired Protocol SurveyWired Communication ProtocolsCompare I2C, SPI, UART, CAN, and 1-Wire side-by-side to select the right protocol for your IoT design
LAN ConnectivityWired Access: EthernetExtend from chip-level buses to LAN-scale wired networking with Ethernet, switches, and IEEE 802.3 standards
Protocol Stack ContextOSI and TCP/IP Layered ModelsSituate I2C at the physical and data-link layers of a full networking stack to understand how it fits into larger IoT architectures

31.16 Key Takeaway

I2C is useful for short, shared-board communication, but pull-ups, bus capacitance, addressing, clock speed, and stuck-bus recovery limit how far it can stretch.