Tuesday, May 6, 2014

PC storage oscilloscope and logic analyzer

A logic analyzer is a useful tool when working with micro-controllers, but when compared to the price of Arduino compatible boards, they are expensive.  While the ability to decode different protocols is a nice convenience, the key functionality of a logic analyzer is the ability to record a signal over a period of time.  For low-speed signals in the tens of kbps, a PC audio codec can perform this function.

The audio codecs in most modern PCs are capable of at least 96kbps stereo sampling at 16 bits per sample, with 192kbps/24-bit "HD audio" being relatively common.  This is enough to analyze a 38.4kbps UART signal in modest detail (5 samples per bit).  With some hardware to connect to the audio input and software to record and view the signal data it is possible to have the functionality of a basic storage oscilloscope and logic analyzer.

What's needed for the hardware is a 3.5mm stereo plug and 3 wires.  The largest tab is ground, and the smaller two are the left and right channels.  I used 24awg wire from a spool of ethernet cable since its cheap (~10c/ft), and when the ends are stripped it easily plugs into breadboards.
I initially had some problems recording from line input until I figured out I had to unplug the microphone - I guess the recording inputs are multiplexed.

For my testing, I recorded the 2kbps output of a pro mini running VirtualWire.  The pro mini is running at 5V, while line input voltages should be ~1V peak, so I used a 100kOhm resistor in series with pin 12 to limit the voltage at the line input.

For recording software, Audacity is probably the most popular free audio program, and one that I tested.  Here is a view of one of the output pulses captured at 192kbps:
The fluctuations at the corners of the square wave is something you would not see with a digital logic analyzer.  What you see above is normal, as a true square wave is a physical impossibility - all signals are summations of sinusoidal waveforms.  The straight lines audacity draws between points doesn't reflect that reality, so I tried resampling to a higher frequency (768kbps):
The corner of the signal looks much better, but the low amplitude ripple that was visible before seems to have been filtered out.  In the resampled signal the slight downward slope of the signal is more visible.  That is because the line input has a DC blocking capacitor which causes the 0-1V signal peak to drift down until it is roughly balanced around 0V.

Another program I tested is GoldWave 5.70.  I didn't find the interface very intuitive.  My first problem was getting it to record at 192kbps.  I eventually found the control properties, which indicated the default WASAPI maxed out at 96kbps.  Switching to DirectSound allowed me to use 192kbps.
Here's a capture from GoldWave using 192ksps:
I find the grid makes analyzing the signal easier than with Audacity.  I also tested resampling, which is limited to a maximum of 500ksps:
It seems to do a better job at resampling than Audacity.

I also tested Soundcard Oscilloscope.  It seems to be limited to 44ksps 16-bit samples and doesn't support viewing of recorded signals.  Visual Analyzer allows for analyzing a signal in many different ways, however I could not find a way to record at more than 96ksps.

I'll finish by pointing out that when trying to analyze faster communications, rather than getting a faster scope or analyzer, consider slowing down the transmitter.  For example it would not be practical to analyze 230.4kbps serial UART output when logging at 192ksps.  However setting the CKDIV8 fuse on an AVR MCU when running the same code would output a 28.8kbps signal, which can easily be captured with a PC audio codec.

No comments:

Post a Comment