ENع
←Logic analyser debugging
Lesson 29 · UART, I2C and SPI in firmware

Debugging your driver with a logic analyser: triggers, NACKs and glitches

14 min10 steps5 questionsIntermediate
About this lesson

An Arduino Uno R3 asks an LM75B temperature sensor for a reading over I2C and gets nothing back. Clip a logic analyser onto SCL and SDA, pick a sample rate, and trigger on the START condition. The capture decodes as address 0x10 and a NACK, and that leads straight to the bug: the driver passed the 8-bit address 0x90. Then hunt a 100 ns glitch that hides between slow samples, and catch it with a faster rate and a pulse-width trigger. The Hardware lesson “Serial buses” explains the wires and the Hardware lesson “Test and debug” shows what a logic analyser is; here you use one on your own driver, after “SPI in firmware: modes and chip select”.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 29 · Interactive
01 / 10

Clip on the probes

Our Uno asks an LM75B temperature sensor for a reading over I2C and gets nothing back. Clip on a logic analyser: channel 0 to SCL, channel 1 to SDA, and its ground to the board's ground.

board     Arduino Uno R3 (ATmega328P, 5 V)
sensor    LM75B, runs on 2.8 to 5.5 V
CH0  ->  SCL (clock)      CH1  ->  SDA (data)
GND  ->  board ground: the 0 V both read against
the analyser records each wire as 0 or 1
Loading figure…
or ← →
Read the lesson step by step
  1. Clip on the probes

    Our Uno asks an LM75B temperature sensor for a reading over I2C and gets nothing back. Clip on a logic analyser: channel 0 to SCL, channel 1 to SDA, and its ground to the board's ground.

    board     Arduino Uno R3 (ATmega328P, 5 V)
    sensor    LM75B, runs on 2.8 to 5.5 V
    CH0  ->  SCL (clock)      CH1  ->  SDA (data)
    GND  ->  board ground: the 0 V both read against
    the analyser records each wire as 0 or 1
  2. Sample fast enough

    The analyser samples: it checks every wire again and again. Wire runs I2C at 100 kHz, so one clock lasts 10 µs. At 4 MS/s (million samples a second) that is 40 samples per clock: every edge shows.

    I2C clock     100 kHz  ->  10 µs per clock
    sample rate   4 MS/s  ->  one sample every 250 ns
    per clock     10 µs / 250 ns = 40 samples
    (Wire on the Uno: TWBR = (16 MHz / 100 kHz - 16) / 2 = 72)
  3. Trigger on the START

    Memory is limited, so tell the analyser which moment to keep: a trigger. SDA falling while SCL is high happens only at an I2C START. It waits for that edge, then keeps the samples just before and after it.

    trigger     CH1 (SDA) falling edge
                while CH0 (SCL) is high
    I2C START   SDA goes high -> low while SCL is high
    pre-trigger buffer: the samples just before the edge
    capture     t = 0 .. 112 µs, START at 5 µs
  4. Decode the address byte

    Run the driver. At each SCL rise the decoder reads SDA: 0 0 1 0 0 0 0, then 0 for write. That is address 0x10. But the sensor's address is 0x48.

    read SDA on every SCL rising edge:
    bits      0 0 1 0 0 0 0 0
    byte      0x20 = address 0x10 (7 bits) + W (0)
    expected  address 0x48
    
    the same decoder, as a Python simulation:
    def decode(scl, sda):            # one 0/1 per sample
        bits, started = [], False
        for i in range(1, len(scl)):
            if not started:
                started = scl[i] and sda[i-1] and not sda[i]  # START
            elif scl[i] and not scl[i-1]:   # SCL rising edge
                bits.append(sda[i])
        byte = int("".join(map(str, bits[:8])), 2)
        return byte >> 1, byte & 1, bits[8]  # address, R/W, ACK bit
  5. Ninth clock: NACK

    On the ninth clock the Uno lets go of SDA, and the chip with that address should pull it low: an ACK. SDA stays high: a NACK. No chip has address 0x10, so endTransmission() returns 2.

    clock 9   Uno releases SDA
              low  = ACK  (a chip answered)
              high = NACK (nobody did)
    captured  1  ->  NACK
    endTransmission() = 2: "received NACK on transmit of address"
  6. Why 0x10, not 0x48?

    The code passes 0x90, the 8-bit form that already holds the write bit. Wire expects 7 bits and shifts left itself: 0x90 becomes 0x120, the top bit falls off the byte, and 0x20 goes out.

    driver    ADDR = 0x90 = 1001 0000
    twi.c     twi_slarw |= address << 1;   (uint8_t)
              0x90 << 1 = 0x120  ->  0x20 in 8 bits
    on wire   0x20 = 0010000 + 0  ->  address 0x10, write
  7. Fix it, capture again

    Set ADDR to 0x48. Now 0x90 goes out: 1001000, then write. On the ninth clock the sensor pulls SDA low: ACK. endTransmission() returns 0, and the read brings back 0x19 0x00: 25 °C.

    driver    ADDR = 0x48  (1001 + A2 A1 A0 = 000)
    on wire   1 0 0 1 0 0 0 0  = 0x90
    clock 9   0  ->  ACK, endTransmission() = 0
    Temp      0x19 0x00 -> top 11 bits = 200
              200 x 0.125 = 25 °C
  8. A glitch between samples

    Days later, some readings come back wrong, yet every capture looks clean. The cause is a 100 ns spike on SCL. At 4 MS/s the analyser samples every 250 ns, so the spike can slip between two samples.

    spike      100 ns high, at 1060 .. 1160 ns
    4 MS/s    samples at 1000 and 1250 ns: both 0
               0 samples land on the spike
    a pulse shorter than the sample gap can vanish
  9. A pulse trigger catches it

    Sample at 50 MS/s, every 20 ns, and trigger on SCL high for less than 1 µs. A real clock stays high at least 4 µs, so only a glitch fires it. It fires: the sensor took the spike as an extra clock.

    50 MS/s     one sample every 20 ns
               5 samples land on the 100 ns spike
    trigger    CH0 (SCL) high pulse < 1 µs
    real SCL   high >= 4 µs (Standard-mode)
    an extra rising edge = an extra clock: bits shift by one
  10. Move the wire, prove it

    The spike comes from a motor wire laid beside SCL. Move it away and leave the pulse trigger armed while the board runs. If it never fires again, the glitch is gone, and so are the bad readings.

    cause     motor wire beside SCL couples spikes onto it
    fix       route it away from the bus wires
    proof     same pulse trigger, left armed: no trigger
    no fire = no glitch: the fix is measured, not guessed

Quiz Unlocks after the last step.