Ramblings of an aging IT geek
← Ramblings of an aging IT geek
hardware

A Cheap Logic Analyser and Finally Seeing the Bus

Buying a sub-twenty-pound logic analyser to debug an I2C sensor that wouldn't talk, and discovering the problem was never in my code.

A breadboard with jumper wires and a small circuit

I had a temperature sensor that refused to answer. An ordinary BME280 on I2C, the sort of thing thousands of people wire up on a wet Sunday without incident, and mine returned nothing but timeouts. I'd checked the address, the pull-ups, the wiring, the address again. I rewrote the driver. I swapped the sensor. I did the full ritual of blaming everything except the thing that was actually wrong, which is to say I had no idea what was actually wrong.

The honest problem was that I was debugging blind. I could see what my microcontroller thought it was sending, but I had no way to see what was actually on the wire. So I finally bought the thing I'd been putting off for years: a cheap eight-channel logic analyser, the little blue clone that costs less than a takeaway and shows up as a Saleae-compatible device.

Plug it in, watch the wire

The workflow is almost embarrassingly simple. Clip the probes onto SDA, SCL and ground. Open the capture software, tell it those two channels are an I2C bus, hit record, and run the failing code. A few seconds later you have the actual electrical truth laid out as a waveform, with the protocol decoded on top.

A circuit board under test with probe clips attached

And there it was. My controller was sending the start condition, the address, the read bit, and then the line just sat there. No ACK. The sensor wasn't ignoring me out of spite. It genuinely never saw a valid address, because the address byte on the wire was not the address byte in my code.

The culprit was the oldest one in the book. I'd written the 7-bit address shifted left by one, and the library was shifting it again. Two left shifts, an address pointing at nobody, and a sensor sitting there politely confused. The analyser showed me 0xEC on the bus when I expected 0x76. Five minutes of looking at the waveform replaced two evenings of guessing.

Why I should have bought one years ago

The lesson isn't really about I2C. It's that for embedded work, the gap between what you think is happening and what is actually happening on the physical wire is enormous, and you cannot reason your way across it. You have to look. A logic analyser turns an invisible serial bus into something you can read like a printout, with timing you can actually measure.

It won't help with analogue problems, noise, ringing, marginal voltages: for that you still want a scope. But for the digital protocols I deal with most, I2C, SPI, plain UART, it answers the only question that matters when something won't talk, which is "what did you actually send". I've used it three more times since, and each time it took something baffling and made it boring in about a minute. The best twenty quid I've spent on the bench in years.