31 I2C: Bring-Up and Diagnosis
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.
| Stage | What to do | What success looks like |
|---|---|---|
| Confirm address | Scan for 0x76 or 0x77 | The address matches the sensor’s SDO pin setting. |
| Initialize driver | Start the BME280 library with the detected address | Startup message reports the sensor is ready. |
| Read slowly | Request temperature, humidity, and pressure every 2 seconds | Values change smoothly and remain physically plausible. |
| Compare environment | Warm the sensor with your hand or move it near airflow | Temperature/humidity respond in the expected direction. |
| Diagnose failure | If readings fail, inspect bus and address first | Most failures are wrong address, missing pull-ups, or swapped wires. |
| Symptom | Likely cause | Fix |
|---|---|---|
| “Sensor not found” | Wrong address or wiring | Try 0x76 and 0x77; recheck SDA/SCL and power. |
| Readings freeze | Bus lockup or clock stretching timeout | Slow the bus, shorten wires, and add bus recovery. |
| Values are unrealistic | Wrong sensor/library mode | Verify the exact BME280/BMP280 module and driver settings. |
Checkpoint: 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
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:
- What happens if both masters try to send a START condition simultaneously?
- 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
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.
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:
- Minimum resistor (max current): I2C spec limits sink current to 3 mA at V
OL= 0.4 V. With 3.3 V supply: R_min = (VDD- VOL) / Isink= (3.3 - 0.4) / 0.003 = 967 Ω ≈ 1 kohm - 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:
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 source | Typical value | Example contribution |
|---|---|---|
| Controller SDA/SCL input | 5-10 pF | ESP32 pins: 10 pF |
| Sensor/display input | 5-10 pF each | BME280: 5 pF; SSD1306: 8 pF |
| PCB trace | 1-2 pF/cm | 15 cm trace at 2 pF/cm: 30 pF |
| Jumper wires | 50-100 pF/m | Keep jumpers short for fast mode |
| Example total | Sum all connected loads | ESP32 + BME280 + SSD1306 + trace ≈ 53 pF |
Maximum resistor for 400 kHz (fast mode):
| Calculation step | Value |
|---|---|
| Fast-mode rise-time limit | 300 ns |
| Estimated bus capacitance | 53 pF |
| Denominator | 0.8473 × 53 pF = 44.9 pF |
| Maximum pull-up value | 300 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:
| Scenario | Problem | Fix |
|---|---|---|
| Long cable (>30 cm) | Bus capacitance >200 pF, rise time too slow at 4.7k | Use 2.2 kohm or reduce speed to 100 kHz |
| Many devices (>8) | Combined capacitance >100 pF | Use 2.2 kohm pull-ups |
| 3.3V bus with 5V tolerant device | Logic thresholds mismatch | Use 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 enough | Use current-source active pull-ups; passive resistors cannot meet the HS-mode rise-time spec |
| Breadboard prototype | Breadboard adds 20-50 pF per row | Use 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.
Checkpoint: 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.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.
| Check | What to record | Failure it catches |
|---|---|---|
| Physical path | Controller pins, connector order, shared ground, VDD, level shifter if used, and the final cable or PCB route | Swapped SDA/SCL, floating ground, 5 V sensor on a 3.3 V bus, or breadboard-only success |
| Address scan | Observed 7-bit addresses, expected devices, and any reserved or duplicate addresses | Wrong address constants, duplicate modules, or missing devices |
| Pull-up range | VDD, estimated capacitance, selected resistor, target speed, and measured rise time when possible | Slow edges at 400 kHz or excessive sink current from too-strong pull-ups |
| Transaction proof | START, address byte, ACK, register select, repeated START, read/write data, and STOP behavior | Sending the unshifted address byte, missing ACKs, or incorrect read/write direction |
| Recovery path | Bus timeout, nine SCL recovery pulses, device reset behavior, and retry limit | Firmware hanging forever after a power glitch or stuck SDA line |
| Release condition | Pass/fail evidence at the final speed with all sensors, display modules, and enclosures connected | A 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
| Topic | Chapter | Description |
|---|---|---|
| Wired Protocol Foundations | Wired Communication Fundamentals | Review synchronous vs asynchronous, open-drain topology, and master-slave architecture before moving to SPI |
| High-Speed Peripherals | SPI Protocol | Learn 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 Serial | UART and RS-232 Serial Communication | Explore point-to-point asynchronous serial communication used for GPS modules, debug consoles, and PC interfaces |
| Wired Protocol Survey | Wired Communication Protocols | Compare I2C, SPI, UART, CAN, and 1-Wire side-by-side to select the right protocol for your IoT design |
| LAN Connectivity | Wired Access: Ethernet | Extend from chip-level buses to LAN-scale wired networking with Ethernet, switches, and IEEE 802.3 standards |
| Protocol Stack Context | OSI and TCP/IP Layered Models | Situate 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.
