Try Embedded

Logic analyzers and oscilloscopes

Seeing what really happens on the wires: sample rates, decoding, triggers and what to look for.

Seeing the signals

A debugger shows what the CPU thinks happened. A logic analyzer and an oscilloscope show what actually happened on the wires. Most "the sensor does not answer" problems are solved in minutes with one of them, after hours of staring at code.

Logic analyzerOscilloscope
ShowsDigital levels: 0 or 1 over timeThe real voltage over time
Channels8 to 32, cheaply2 to 4 usually
Good forProtocols (UART, I2C, SPI), timing between many signals, decoding bytesSignal quality: rise times, ringing, noise, wrong voltage levels, analog signals
MissesAnything analog: a slow edge or a 2 V "high" just looks like 0 or 1Long captures of many signals, protocol decoding (on most cheap scopes)

Start with the logic analyzer to see what was sent; reach for the scope when the bytes look right but the device still misbehaves, which points at signal quality.

The simulator has a logic analyzer part (te-logic-analyzer): wire its channels to any pins, run, and it records every edge, decodes UART, I2C or SPI, and exports a VCD file for PulseView. It also has a 2-channel oscilloscope part (te-oscilloscope) that shows the voltage itself, with a trigger and measurements of the levels, frequency and duty cycle. Its circuit model has instant edges, so it shows true levels but not rise times or ringing; the exercises below show those.

Sample rate: fast enough to see each bit

A logic analyzer samples each channel at a fixed rate and records the level. To see a signal reliably, sample at at least 4, ideally 10 times its fastest bit rate. Sampling too slowly misses short pulses and makes timing jitter by a whole sample period.

SignalBit timeSample at least
UART 115200 baud8.7 µs500 kHz to 1 MHz
I2C 400 kHz2.5 µs2 to 4 MHz
SPI 8 MHz125 ns32 to 80 MHz

Decoding by hand: a UART byte

A decoder does what you would do by eye: find the falling edge of the start bit, then sample each bit in its middle. This decodes a byte from analyzer samples:

#include <stdio.h>
#include <stdint.h>

/* Samples of a UART line, 4 samples per bit (an analyzer at 4x the baud rate).
   Idle high, start bit low, 8 data bits LSB first, stop bit high: this is 'K' (0x4B). */
static const uint8_t line[] = {
  1,1,1,1,  0,0,0,0,                 /* idle, start bit */
  1,1,1,1,  1,1,1,1,  0,0,0,0,  1,1,1,1,  /* bits 0..3 = 1,1,0,1 */
  0,0,0,0,  0,0,0,0,  1,1,1,1,  0,0,0,0,  /* bits 4..7 = 0,0,1,0 */
  1,1,1,1,  1,1,1,1,                 /* stop bit, idle */
};
#define PER_BIT 4

int main(void) {
  int n = sizeof line;
  for (int i = 1; i < n; i++) {
    if (line[i - 1] == 1 && line[i] == 0) {          /* falling edge: a start bit begins */
      int mid = i + PER_BIT / 2;                       /* middle of the start bit */
      uint8_t byte = 0;
      for (int bit = 0; bit < 8; bit++) {
        int at = mid + (bit + 1) * PER_BIT;            /* middle of each data bit */
        if (line[at]) byte |= (uint8_t)(1u << bit);   /* LSB first */
      }
      int stop = mid + 9 * PER_BIT;
      printf("byte 0x%02X '%c', stop bit %s\n", byte, byte, line[stop] ? "ok" : "MISSING (framing error)");
      i = stop;
    }
  }
  return 0;
}

Sampling in the middle of each bit is what makes UART tolerate a small baud-rate mismatch: the sample point can drift almost half a bit before reading the wrong value.

Triggers

A capture is only useful if it contains the moment that matters. A trigger starts (or centres) the capture on a condition:

  • an edge on one channel (the chip-select falling, a reset line rising),
  • a pattern across channels,
  • on better tools, a decoded value: "I2C write to address 0x68", "UART byte 0x0D".

A spare GPIO makes a powerful trigger: toggle it in the code just before the event you want to see, and trigger on it. It also measures timing: set a pin high at the start of an ISR and low at the end, and the analyzer shows exactly how long it takes and how often it runs.

What to look for

Symptom on the analyzerLikely cause
I2C: address byte sent, then NACK (SDA high on the 9th clock)Wrong address (7-bit vs 8-bit confusion), device not powered, wrong bus
I2C: SDA or SCL stuck lowA device holding the bus after a reset mid-transfer: clock it out with 9 pulses
SPI: correct clocks, garbage dataWrong mode (clock polarity or phase), or MSB/LSB order
UART: framing errorsBaud rate mismatch, wrong data bits or stop bits, inverted signal
Nothing at all on the pinsPeripheral clock off, pin not switched to its alternate function

With a scope, look for slow rising edges on I2C (pull-up resistors too large for the bus capacitance), ringing and overshoot on fast edges (long wires, no termination), and a "high" that is not high enough (a 3.3 V output driving a 5 V input).

Pitfall: Connect the analyzer's ground to the circuit's ground. Without it, the levels float and the capture is noise that looks like a broken protocol.

Interview question: An I2C sensor never answers. How do you debug it?

Answer: I put a logic analyzer on SDA and SCL and trigger on the start condition. If there is no activity, the peripheral or pins are not set up: clock, alternate function, or the code never reaches the transfer. If the address goes out and is NACKed, I check the address (7-bit versus the 8-bit form with the R/W bit), that the device is powered and not held in reset, and its address pins. If lines are stuck low, a device is holding the bus and needs clocking out. If the bytes look right but it is unreliable, I check the pull-ups and rise times with a scope.

Exercises: debug from the capture

Each exercise below is a bug report with the capture you would take on the bench, from a logic analyzer or, for the last three, an oscilloscope. Read the waveform before answering: measure against the grid, count the bits and find the acknowledge slots. The decoded bytes, or the scope's measurements, appear once you answer, so you can check your reading.

The next three need the voltages themselves, so they are oscilloscope captures.

Build it: a UART decoder for the logic analyser

This is step 4, the last of the module project. A few returned watches show garbled debug output. The team captured the UART line with a logic analyser at 8 samples per bit, but the analyser's software cannot handle the glitches on this line. You write the decoder, exactly the way the analyser's own decoders work.

Lab: Decode UART bytes from oversampled line levels

The capture is an array of line levels, 0 or 1, at 8 samples per bit. The line idles high (1). A frame is a start bit (0), 8 data bits, least significant first, and a stop bit (1).

Write uart_decode(s, n, out, max, framing):

  1. Scan for a falling edge: a 0 sample right after a 1 (the sample before index 0 counts as 1).
  2. Check the middle of the start bit, 4 samples after the edge. If the line is back to 1, it was a glitch: ignore it and keep scanning from the next sample.
  3. Read data bit k (0 to 7) from the middle of its bit, at edge + 4 + 8 * (k + 1).
  4. Check the stop bit at edge + 4 + 8 * 9. If it is 0, count a framing error in *framing and drop the byte. Otherwise store the byte (if fewer than max are stored).
  5. Either way, continue scanning from the middle of the stop bit. A frame that would run past the end of the capture is not decoded.

Return the number of bytes stored.

Going deeper

Interview question: A wireless doorbell misses one press in fifty. How would you use your instruments to find out why?

Answer: Put a logic analyzer on the button pin, the MCU's wake-up and the radio's SPI or UART, triggered on the press, and capture until a miss happens. See whether the press was seen, whether the radio was asked to send, and whether it did. Use the oscilloscope on the supply when the radio transmits: a sagging rail that resets the MCU is a classic cause.

Interview question: How do you measure how long an interrupt handler takes on real hardware?

Answer: Set a spare GPIO high at the start of the ISR and low at the end, and capture it with a logic analyzer or scope: the pulse width is the handler's run time, the spacing shows how often it runs, and with other signals captured too you see the latency from the triggering event. The GPIO writes cost a few cycles, which is negligible. Alternatively, read the DWT cycle counter at entry and exit, which needs no instrument but changes the code more.

Interview question: What is the difference between a logic analyzer and a mixed-signal oscilloscope?

Answer: A logic analyzer records many digital channels as 0s and 1s, usually with deep memory and protocol decoders, but cannot show analog behaviour. An oscilloscope shows true voltages on a few channels. A mixed-signal oscilloscope combines both, with analog channels and a set of digital channels on one time base, so you can see a protocol's decoded bytes alongside the analog quality of the same signals.

Explain it: In your own words, explain when you would use a logic analyzer and when an oscilloscope.

Answer: I use a logic analyzer first when I need to see what was communicated: which bytes went over UART, I2C or SPI, the order and timing of many digital signals, or how long code takes using a GPIO toggle. It only shows 0 and 1. I switch to an oscilloscope when the digital content looks right but things still fail, because then the problem is often electrical: slow or ringing edges, noise, or wrong voltage levels, which only a scope shows.