Back to resources
HIL
Testing
Firmware

Breaking the I2C Bus on Purpose

A sensor that stops answering, a bus held low, a reading that freezes or drifts. The BenchPod plays the sensor and injects those faults on real wires, so the recovery code that ships untested gets a test of its own.

Edward Viaene · November 19, 2026 · 6 min read

Every sensor driver has an error path, and almost none of them have a test. On the bench the sensor always answers, so the code that runs when it doesn't (the retry, the bus reset, the alarm) only ever runs in the field. That's where a loose connector or a sensor in a brown-out holds SDA low, and the firmware finds out whether its recovery code works.

The BenchPod can play the sensor on two of its channels. Since it's the sensor, it can also misbehave on command: stop acknowledging, hold the bus, freeze a reading, flip a bit. This article runs those faults against our thermostat node and shows what to assert for each.

The faults

A fault is one call on a running sensor. It starts right away and lasts until you clear it, or for duration_s seconds:

env = bp.sensors.start("bmp280", instance="env", pins={"sda": "i2c_sda", "scl": "i2c_scl"},
                       address=0x76, values={"temperature_c": 25.0, "pressure_pa": 101325.0})

env.fault("no_response", duration_s=5)        # the address is not acknowledged for 5 s
env.fault("bus_stuck", duration_s=0.5)        # SDA held low for 0.5 s (at most 1 s)
env.fault("stuck")                            # readings freeze at their current values
env.fault("offset", param="temperature_c", value=5)   # reads 5 degC high
env.clear_fault()

Here's the full set for an I2C sensor, with what we think good firmware should do about each:

FaultWhat the pod doesWhat good firmware does
no_responsestops acknowledging its addressretries a bounded number of times, then flags the sensor as lost; recovers when it answers again
bus_stuckholds SDA low (in the FPGA) for up to 1 sgives up on the transfer instead of hanging, resets its I2C peripheral, gets the bus back afterwards
stuckkeeps returning the same readingideally notices a value that never moves; at least doesn't make it worse
offset, gainshifts or scales one readingthe reading changes as expected; range or plausibility checks catch the extreme ones
corruptflips the bits in mask at register atrejects the data if the sensor has a CRC; otherwise a range check is the last line

UART sensors get garbage, drop and baud_error as well, and analog ones open and short. At most four faults run on one sensor at a time. The faults need firmware 3.10.3 or later; the sensor docs have every field.

A sensor that goes away

The node reads its environment sensor every 200 ms. When the sensor goes missing it should raise an env_lost alarm on three outputs: a line on its console, its alarm pin, and a CAN frame. When the sensor comes back, the alarm should clear on its own.

import pytest

@pytest.mark.hardware
def test_env_sensor_lost_and_back(benchpod, env):
    bp = benchpod
    with bp.open_uart() as uart:
        env.fault("no_response", duration_s=5)

        uart.expect("EVT alarm set=env_lost", timeout=3.0)
        assert bp.read_gpio("alarm") == 1

        # The fault ends by itself after 5 s; the node must notice without a reboot.
        uart.expect("EVT alarm clear=env_lost", timeout=8.0)
        assert bp.wait_for_level("alarm", 0, timeout=1.0)

env is a fixture that starts the emulated sensor (and the pod's pull-ups) and stops it afterwards. The alarm pin is named in the bench's wiring profile, so "alarm" works wherever a channel number would. The pod reads it as a live input while the node drives it.

The second expect is the important one. It's easy to write firmware that notices a sensor going away. It's less common to write firmware that notices it coming back, and a node that needs a power cycle to see its sensor again is a site visit.

Holding the bus low

bus_stuck is the nasty one. A target that holds SDA low mid-transfer (a sensor reset at the wrong moment does it) leaves the master unable to send a STOP or a START. Some drivers hang there forever.

Our node's MCU has a trap here. Its I2C peripheral sees SDA low, goes BUSY, and can stay BUSY after the target lets go, because it doesn't always notice the STOP that follows. Without help, every later read fails. The node's driver resets the peripheral after a failed transfer, and this is the test that keeps that fix in place:

import time

@pytest.mark.hardware
def test_bus_stuck_recovers(benchpod, env, node):
    env.fault("bus_stuck", duration_s=0.5)

    data, err = node.i2c_try("rd 76 d0 1")          # a read while SDA is low
    assert data is None, "a transfer on a stuck bus must fail, never return data"
    assert err.split()[0] in ("bus_busy", "timeout", "arb_lost", "bus_error")

    time.sleep(0.8)                                  # the pod has released SDA
    data, _ = node.i2c_try("rd 76 d0 1")
    assert data == b"\x58", "the bus must work again without a reboot"

node is the example's helper around the node's console commands. The read returns the BMP280's chip ID, 0x58, once the bus is back. The test checks two things: the firmware never returns data from a stuck bus, and it gets the bus back without a reboot. Delete the peripheral reset from the driver and the second assertion fails.

Readings that lie

The remaining faults don't break the bus. They make the data wrong, which is harder to catch because nothing reports an error.

stuck is the clearest example. The sensor keeps answering with the last reading while the real value moves on:

env.fault("stuck")
env.set(temperature_c=35.0)        # the room heats up; the sensor doesn't say so
time.sleep(1.0)
assert abs(node.env()["temp_c"] - 25.0) <= 0.1

That assertion passes on our node, and it's a finding rather than a success: the node keeps working with a frozen value and nothing tells anyone. Whether that's acceptable depends on the product. A thermostat in an office can live with it; a heater controller probably shouldn't. The test makes the behavior visible and pins it down, so a change is a decision and not an accident.

offset and gain are good for alarm thresholds. Instead of heating anything, shift the reading past the over-temperature limit and check that the alarm fires within its latency budget, with its hysteresis, on every output:

env.fault("offset", param="temperature_c", value=30)     # 25 degC reads as 55 degC
uart.expect("EVT alarm set=overtemp", timeout=2.0)

corrupt matters most for sensors with a CRC, like the Sensirion SHT4x. Flip one bit of the temperature CRC and the driver should drop that reading, not use it:

sht = bp.sensors.start("sht4x", pins={"sda": "i2c_sda", "scl": "i2c_scl"})
sht.fault("corrupt", at=0xFF, mask=0x01)     # break the temperature CRC only

Faults over time

A one-off fault tests one moment. Real failures come and go: a connector that drops out for 200 ms every few seconds, a sensor that browns out with the motor. A stimulus file plays readings and faults on a timeline, so a test can run a 60-second scenario with three short dropouts and one stuck period, and check that the node raised and cleared the right alarms in the right order.

The pod applies the timeline itself, so the timing doesn't depend on the network between your test and the pod. The same test runs over the LAN, the USB cable, or the cloud from GitHub Actions.

Where the tests live

Each fault in this article is a test in the open source `process-node-stm32` example: tests/test_sensors_faults.py covers no_response, corrupt, stuck and bus_stuck (plus the SPI and UART faults), and tests/test_alarms.py covers the sensor going missing and coming back, over-temperature on every output, and a short spike the debounce has to ignore.

None of it needs a broken sensor, a soldering iron or a person at the bench. The sensor library and the fault fields are on Emulated sensors, and the fixtures in the pytest docs.