Stupid Arduino Tricks – Logic Analyser
Posted in Arduino, Projects, Software on May 16th, 2011 by Andrew – Comments OffChecking the values of the digital pins on an Arduino is best done with a logic analyser or oscilloscope. But scopes are expensive, and while logic analyser prices have come down a lot since I last looked, they’re still expensive enough to give pause before purchasing one, especially when the thing you want to attach it to is a $30 microcontroller development board.
The solution? Write a logic analyser in software! Others have done this on the Arduino, but those were meant to make a traditional, external logic analyser. This Stupid Arduino Trick is meant to be embedded in other projects to read the digital lines directly while the other project is running, no probes required.
There are down sides to doing this, of course. The added software load from the logic analyser will mess with the timing of the software. And data collection sucks up a lot of RAM — the Arduino only has 2 KB to play with to begin with.
To address the first problem, the best you can do is write tight code. Data collection happens on a periodic basis in an interrupt service routine. Since in general, I don’t know the environment that the logic analyser will work in, I decoupled the logic analyser from any specific timer. It’s up to the user to add a call to the logic analyser’s timer event. This way, the logic analyser can be given its own timer ISR, or it can piggyback on an in-use timer ISR in the project code.
The sampling rate of the logic analyser is configured in the same way — the user is responsible for setting up the timer ISR period manually.
The logic analyser does offer an interface for setting up which digital lines to trace (up to eight), and the trigger conditions that will start the thing collecting data. Trigger conditions include rising edge, falling edge, high, and low for up to eight lines.
To address the second problem, pin values are stored as bits in a character array. Characters being 8 bits wide on the Arduino, the logic analyser can sample 8 digital pins each interrupt.
The logic analyser runs a simple, three-state state machine.
After configuration and the issuance of the start command, the logic analyser enters the WAITING FOR TRIGGER state, where it checks the sampled inputs for the trigger conditions on the specified digital lines. Once the trigger condition is met, the logic analyser enters the TRACING state, where it collects data until its trace buffer is full. Once it’s full, the logic analyser enters the DONE state, where it does nothing each interrupt. The DONE state is also the initial state of the logic analyser.
Once data is collected, it can be sent to the host computer via the serial / USB port on the Arduino. The data is formatted as comma separated values for easier import into your favorite spreadsheet application for analysis and will look something like this:
Source code for the logic analyser is available on GitHub. The Arduino sketch there includes the unit test for the logic analyser. To use the logic analyser in your own sketch, either copy LogicAnalyser.cpp and LogicAnalyser.h to a folder called LogicAnalyser in the libraries folder of your Arduino projects directory, or include the two files directly into your Arduino project’s sketch. The second method is probably preferable since it will allow you to change the trace buffer size to fit your needs and memory restrictions.
