Testing out an Arduino as logic analyzer

There is a nice work in the open source on using Arduino as a logic analyzer. I tested that out and could get it to work on 6 channels at 4 MHz. Rather impressive.
I made a movie of the analyzer, testing it on another Arduino that is using a shift register.

I recently started to use this Arduino logic analyser to assist in the development of some I2C bit banging for the PIC16F series. I have an old Tektronix D755 oscilloscope but this does not let me capture a single shot so the logic analyser is proving invaluable. The biggest problem I have is understanding the configuration options !

I did include the 2 MHz and 4 MHz sampling rate code in the Arduino build and it fills just over half (141,592 bytes) of my Mega2560 .

What config problems did you have? Triggering? I found that that was the most difficult to understand till I found out that the Arduino code doesn't support complex (and) conditions.

The 2/4 mhz problem that I stated in the video was my misunderstanding. I found out later that with all source files copied, it worked, but in the Uno and 2009 that I have, the capture time is very small.

Jan Cumps wrote:What config problems did you have? Triggering? I found that that was the most difficult to understand till I found out that the Arduino code doesn't support complex (and) conditions.

The 'triggering' is my biggest single problem but luckily at the moment for I2C bit banging and the AD9850 control I can get away with the simplest triggering. Based on what you say above maybe I'm looking for functionality that is not implemented.

I suspect that most of my configuration problems will go when I get more familiar with the whole and understand the limitations.

The 2/4 mhz problem that I stated in the video was my misunderstanding. I found out later that with all source files copied, it worked, but in the Uno and 2009 that I have, the capture time is very small.

At them moment I am only using 1 MHz sampling because of a problem I'm finding with 2 and 4 MHz. I seem to be getting a repeatable gap in the collection when using 2 and 4 MHz that is not there at 1 MHz. I have spent some time trying to understand from where the gap originates but cannot pin it down. In the morning when, I'm less tired, I will take a couple of screen shots and post them.

Even with the limitations and problems I'm having it is still nice tool to have available.

To make sure I don't have a corrupted download and build, this morning I have re-download all the software and re-built the Arduino logic analyser and uploaded it to the Mega2560. I have configured some hardware and monitor 3 outputs. A serial output at 115.2 KBaud, a 25 KHz output from an AD9850 and a 39 KHz crystal output. Running the logic analyser at 1 MHz and 2 MHz I get traces as attached.

It can be seen that there is an apparent gap in the collection at 2 MHz but more careful examination shows that it is the samples that are shifted right by about 200 uS. Similar results are obtained at 4 MHz sample rate with the shift again being about 200 uS. Note that the lower blue trace is just an unused floating input and is always 1 except in the 'gap' .

When I have finished my current project (late next week maybe) I shall have a look at the Arduino logic analyser software to see if there is anything obviously wrong. Maybe it is as simple as the time stamp associated with the samples being calculated wrongly!

I have a square wave generator here. Is it possible for me to test your issue with a fixed frequency wave? What frequency and duty cycle would be needed to replicate your setup ( my math skills are bad )?

Jan Cumps wrote:I have a square wave generator here. Is it possible for me to test your issue with a fixed frequency wave? What frequency and duty cycle would be needed to replicate your setup ( my math skills are bad )?

It will be interesting to know whether or not the problem is related to the type of Arduino. A 25 KHz square wave on the first channel shows the effect nicely. See below.

I've posted my preliminary results testing a 25K TTL level square wave,
sampling at 1M, 2M and 4M.
I have no dips in the results - although the capture timeon my UNO is way shorter than what you can achieve.

I've filmed my replication attempt and will post a link to that once uploaded.

Thanks for that Jan, very interesting. Your collection period is long enough that my 200 uS gap would have been apparent so it looks like the problem is either Arduino type dependent or due to a flaw in my Mega. Once I have finished my current project I will investigate the problem.

The only thing I can think about is that on your version an interrupt is interfering with the sampling. Was not expecting that would be an issue, because the Arduino sketch is directly speaking to the AVG registers.

Jan Cumps wrote:The only thing I can think about is that on your version an interrupt is interfering with the sampling. Was not expecting that would be an issue, because the Arduino sketch is directly speaking to the AVG registers.

My first thought was that it was an interrupt latency problem but it does not seem to be missing samples but samples shifted in time.

Over lunch I have established that the 'gap' is not independent of the sample frequency but about 250 uS at 2 MHz sample rate and about 125 uS at 4 MHz sample rate. This means that the gap is very close to 512 samples for both 2 and 4 MHz. I like the number 512 since it could indicate a simple software bug.

Edit: I couldn't leave it alone ! There is a very suspicious bit of code in the 2 MHz source.

I think the #ifdef ... #elseif is flawed since for the Mega the samples between 530 and 1023 inclusive are not used.

I can't let it go now I am so close!

Further edit : taking out those conditional compilation statements so that ALL the 'logicdata' array is used solves the problem ! I shall have to fix the 4 MHz file and then create a bug report to file.

Hopefully the final edit : There was the same problem in the 4 MHz code and removing all the conditional compilation fixes that as well. I can go to bed this evening very happy!

Though I based my problem analysis on the assumption that 512 was the magic number it turns out it was actually 531! One has to be lucky sometime .

I've already raised an issue and explained the problem. The comments on the download page confuse me a bit over what the Mega1280 is capable of capturing and since I only have a Mega2560 I can't fully test my fix. If the writer comes back to me I can send him patch files but the fix is very very simple.

Wow, this has already been fixed by the author on GitHub. Cool. From detection to fix in one day, can only happen with Open Source

OCUP UML fundamental and ITIL foundation

Andrew Gillham

Greenhorn

Posts: 1

posted 4 years ago

2

Jan Cumps wrote:Wow, this has already been fixed by the author on GitHub. Cool. From detection to fix in one day, can only happen with Open Source

Thanks everyone for finding this / submitting the issue etc. Obviously I didn't test this new code out on the Mega too well! Regarding triggers, the 2MHz & 4MHz sample rates can't do very accurate triggers at all and the basic trigger is not really at the correct clock rate. The reason for that is the busy wait I'm using to look for the trigger condition takes too many CPU cycles to be accurately sampling at those rates. So if you have a very high clock rate you're trying to trigger on, like fast I2C or fast SPI it might not work that well. The 1MHz sample rate's trigger is a bit better timing wise but still not that good.

But overall the triggers are basic. You can only watch for conditions that occur at the same time, not serially. If you want a really good inexpensive logic analyzer, (besides using an Arduino with the known limitations) I would highly recommend the Open Bench Logic Sniffer (or sometimes call Open Logic Sniffer or OLS) which has many more features. For $50 it is a nice device.

Let me know (via GitHub preferably) of any issues / feature requests / whatever you have as I want to make sure the logic analyzer is reliable and useful for people that don't want to invest (yet) in a fancier one.
Thanks again for bringing this to my attention.

After Andrew Gillham recommended the Open Bench Logic Sniffer I spend a couple of days researching it and then ordered one from 'Seeed' in China. The board and two sets of leads ( one is most most definitely needed and is probably enough ) came to about UK £41 including delivery which was significantly cheaper than buying from the US. Delivery took just 8 days and since UK customs did not demand their pound of flesh I was well happy. No USB cable came with the board but I never throw anything away (this upsets my wife dreadfully) so I just searched through my enormous junk box to find one that fitted. I'm not sure what one calls the connector but it will be something like a 'mini' or 'micro' USB. I will try to photograph it if anyone is interested.

I connected the Sniffer to the AD9850 signal generator board I have been building, fired up the OLS software that I had been using with the Arduino, changed the configuration so that it used the Sniffer and collected data from the AD9850 signals being generated by the control hardware. Brilliant !

When using the Arduino as a logic analyser it was difficult to get the trigger conditions right and could only trigger properly with a 500 KHz sampling rate. This meant that I had to set the AD9850 control clock pulses to about 5 microseconds wide which in turn mean the AD9850 frequency adjustment took about a millisecond. With the Logic Sniffer I was able to sample at 20 MHz and to use control clock pulses of less than 1 microsecond. The AD9850 frequency adjustment now takes under 200 microseconds. A typical AD9850 frequency adjustment signal view is attached; it show changing the frequency from 100 KHz down to 25 KHz.

My experience over the last month or so is that using the Arduino as a logic analyser is pretty good if one is working with signals of about 5 microseconds or longer but once one starts dealing with sub-microsecond signals then it runs out of steam and the Open Bench Logic Sniffer is required. Since using the Sniffer on the AD9850 I have moved on to reading the DHT11 and am now back on bit banging I2C. I could work with the Arduino as a logic analyser for both the DHT11 and I2C design but since I have the Logic Sniffer I will obviously use it.

ShouldYOU buy an Open Bench Logic Sniffer ? If you have a spare Arduino and are working with frequencies up to about 500 KHz then using the Arduino as a logic analyser is all you really need. If you don't have a spare Arduino it is hardly worth buying one just to use it as a logic analyser when for about twice the price of a genuine Arduino Mega256 you can buy an Open Bench Logic Sniffer.

I wasn't really expecting that an Arduino would be a full fledged logic analyzer. But it certainly is a cool exercise to try and use it that way.
That is fully compatible with the initial intention of its creators: a learning and hacking device ;)
Andrew wrote in his notes that the triggering is not up to the mark. And by looking at the what's possible with the Arduino I'm not expecting that anything better can be achieved at the frequencies that matter.
It might be possible that by removing support for the > 1MHz frequencies you have enough memory space left to write a better trigger.

The description of Sniffer sounds promising. I've seen Dave Jones from the EEVBlog using the Saleae.
I don't have the need to an analyzer at this moment, but if I need one, it 'd probably be one of these two.