Debugging your driver with a logic analyser: triggers, NACKs and glitches
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”.
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
Read the lesson step by step
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 1Sample 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)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 µsDecode 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 bitNinth 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"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, writeFix 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 °CA 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 vanishA 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 oneMove 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
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01Which trigger fires only at the start of an I2C transaction?
The I2C specification defines a START as a HIGH to LOW transition on SDA while SCL is HIGH. Data bits only change while SCL is low, so this edge never happens in the middle of a byte.
02On the ninth clock after the address byte, SDA stays high. What does that mean?
On the ninth clock the controller lets go of SDA and the addressed chip should pull it low (ACK). If SDA stays high it is a NACK; the first cause in the specification is that no chip has that address. Wire's endTransmission() then returns 2.
03The driver calls Wire.beginTransmission(0x90). Which byte goes out on the wire?
Wire expects a 7-bit address and shifts it left into an 8-bit value with the write bit: 0x90 << 1 = 0x120, and only the low 8 bits survive, 0x20. That decodes as address 0x10, write. Passing 0x48 sends 0x90.
04A 100 ns spike on SCL never shows at 4 MS/s. Why?
A sample is a snapshot. With 250 ns between snapshots, a 100 ns pulse is missed whenever no snapshot lands inside it. At 50 MS/s (every 20 ns) 5 samples always land on it.
05Why does a trigger on "SCL high for less than 1 µs" catch only glitches on a 100 kHz bus?
The I2C specification sets the minimum HIGH time of SCL in Standard-mode at 4 µs. A high pulse under 1 µs is therefore never a legal clock, and leaving the trigger armed after the fix proves the glitch is gone.
Glossary
- Logic analyser
- A tool that records many digital wires at once as 0s and 1s over time, and can decode them into bytes of a protocol such as I2C.
- Sample rate
- How many times per second the analyser checks each wire, in MS/s (million samples per second). A pulse shorter than the gap between samples can be missed.
- Trigger
- The event the analyser waits for, such as an edge or a pulse of a given width. It keeps the samples just before and after it.
- START condition
- SDA going from high to low while SCL is high. Every I2C transaction begins with one.
- ACK / NACK
- On the ninth clock of each byte the receiver pulls SDA low to acknowledge (ACK). If SDA stays high it is a Not Acknowledge (NACK).
- 7-bit address
- The I2C address of a chip, sent as the top 7 bits of the first byte; the eighth bit is read (1) or write (0).
- Standard-mode
- The basic I2C speed class: up to 100 kbit/s. Faster classes are Fast-mode (400 kbit/s) and Fast-mode Plus (1 Mbit/s).
- Glitch
- A short unwanted pulse on a wire, for example coupled from a noisy wire beside it. On a clock line it can count as an extra clock.
Notes and sources
- The capture is drawn with idealised square edges and an evenly spaced 100 kHz clock (SCL high and low 5 µs each). A real bus has slow rising edges set by the pull-up resistors, and the Uno's clock is not exactly half high and half low.
- I2C addresses are 7 bits. Some datasheets print an 8-bit form with the read/write bit already added (0x90 to write, 0x91 to read for address 0x48). Arduino's Wire wants the 7-bit form and adds the bit itself.
- Only the address write is drawn. The full reading is: write pointer 0x00, then read two bytes. The LM75B's Temp register keeps an 11-bit two's complement value in the top 11 bits, 0.125 °C per step, so 0x19 0x00 is 200 x 0.125 = 25.0 °C. In the driver, read the two bytes into separate variables (byte msb = Wire.read(); byte lsb = Wire.read();) before joining them: C++ does not fix which side of | runs first, so (Wire.read() << 8) | Wire.read() can swap them.
- UM10204 asks Fast-mode chips to filter out spikes shorter than 50 ns; Standard-mode has no such rule. A 100 ns spike is longer than that filter, so a chip can count it as a clock edge.
- Whether the 4 MS/s capture misses a 100 ns spike depends on where it falls: with 250 ns between samples it is missed whenever no sample lands inside it. At 50 MS/s at least 5 samples always land on it.
- NXP UM10204, I2C-bus specification and user manual (START, ACK/NACK, addressing, timing)
- NXP LM75B digital temperature sensor data sheet
- Arduino AVR core: Wire twi.c (address << 1, TWBR)
- Arduino AVR core: Wire twi.h (TWI_FREQ 100 kHz)
- Arduino language reference: Wire.beginTransmission() (source)
- Arduino language reference: Wire.endTransmission() (source)
- Saleae Logic user guide (sample rate, edge and pulse-width triggers)
- Saleae: memory usage with triggered captures (pre-trigger buffer)
Quiz Unlocks after the last step.