Square Wave from Arduino Due. Is it square enough?

With some code from "stimmer" I did the old standby of banging a square wave from an Arduino Due using digitalWrite and then doing it again using port manipulation. Port manipulation accomplishes a 21 Mhz "square" wave, exactly 1/4 of the rated clock speed of 84 Mhz. It's not that square, though I guess it would trigger a digital input fine. FWIW, the waveform it does make it certainly does reproduce the same way each time though. Is that right, is this a clean enough square wave to use with most other digital ICs?

digitalWrite:

Port manipulation:

Your scope is 70MHz I believe, so a 20MHz square wave will only show upto the third harmonic...

MarkT:
Your scope is 70MHz I believe, so a 20MHz square wave will only show upto the third harmonic...

Now I do not know a lot about scopes, I've got more scope here than knowledge for sure, but it's a digital scope with a 2GS/s sampling rate. I'm pretty sure it uses all the sampling ability even though it (in software!) limits the maximum resolution to 70mhz (and they spec different probes for over 100Mhz too). That is interesting, though, I am going to have to borrow a better scope and try it out on another one.

Hmmm, all that ringing on the signal leads me to believe you don't have the "ground" wire
on the scope probe positioned properly. It should be connected to the ground point nearest
the signal being measured, and not somewheres on the other side of the board, etc.

Plus you should darn well be using a 10X scope probe for high-frequency measurements.

oric_dan(333):
Hmmm, all that ringing on the signal leads me to believe you don't have the "ground" wire
on the scope probe positioned properly. It should be connected to the ground point nearest
the signal being measured, and not somewheres on the other side of the board, etc.

Plus you should darn well be using a 10X scope probe for high-frequency measurements.

OK, I am willing to take instruction here for sure. The way I have this set up I have, and maybe this is too long, a 3" jumper on the 2 pin on the Arduino board and a 2" jumper on GND nearest that pin and I have the probes attached with alligator clip (gnd) and hook clip (2 pin) the jumpers. I calibrated the probes prior to taking the measurement.

Where the scope says "DC 10:1" I am pretty sure that means it is in the 10X mode - it said it was in another menu as well. These probes are Agilent N2862B 10:1 150Mhz passive probes. There is no 1:1 option.

What should I do differently? Thanks for suggestions!

OK, I shoved a couple of 1 pin male headers in there to keep it as short as possible. Much better now. Anything else I can do to improve it?

Looks much better now, although the probe grounding could probably be improved
a bit. Most important is the total length of the probe ground wire, including extensions,
be the minimum possible. The longer wire produces a big inductive loop that rings and
propagates noise like a loop antenna.

Now, when feeding your 20-Mhz signal out to the rest of the world, you can try using a
small value R [ie, 22 or 47 ohms] in "series" in the "output" line, to limit ringing. Look up
something called "source series termination".

oric_dan(333):
Looks much better now, although the probe grounding could probably be improved
a bit. Most important is the total length of the probe ground wire, including extensions,
be the minimum possible. The longer wire produces a big inductive loop that rings and
propagates noise like a loop antenna.

Now, when feeding your 20-Mhz signal out to the rest of the world, you can try using a
small value R [ie, 22 or 47 ohms] in "series" in the "output" line, to limit ringing. Look up
something called "source series termination".

So I have gotten the leds from the Arduino as short as possible to the probe and grounding clip. The probe itself as provided by Agilent has a hook for the probe and a grouding clip that you hook onto the probe around the neck where the wire itself is 3.5" long and it is extended in length with a crescent clip on one end to attach to the probe and the alligator clip on the other. The spec on the probe wire is 1.2 meters. So I wonder, is the "problem" the Agilent ground wire? It could be shorter but it's pretty darn short already and Agilent is clearly happy with it. This is the probe with the grounding clip attached:

http://www.home.agilent.com/en/pd-1938439-pn-N2862B/passive-probe-101-150-mhz-12-m

Are there better choices? Agilent seems to think this one is $110 worth of probe and they gave me two with the scope.

Critical thing is the loop at the tip of the probe, not the length of the coax. [time
for sleep now].

The scope probe is vital. You can pay more for a good scope probe than many scopes.
Your real wave shape is square, anything else is either an effect of the load or an artefact of the measurement. You learn to see the wave not what is shown on the screen. The bandwidth of the amplifier in the scope also has an effect. You can sample as fast as you like but you won't get over this. In general you will only see a squarish wave when your scope bandwidth is at least ten times the frequency you are looking at.

I did the old standby of banging a square wave from an Arduino Due using digitalWrite

If you really want to know your Due's pin flipping capability, google "arm bit banding".

JoeN:
but it's a digital scope with a 2GS/s sampling rate.

That just tells you how fast the A/D samples.

In your case, you are using peak-detect which is throwing samples away. Even though you are significantly oversampling, you should really turn that sampling mode off if you're trying to visualize what the waveform actually looks like.

JoeN:
limits the maximum resolution to 70mhz (and they spec different probes for over 100Mhz too)

That isn't quite right. The bandwidth of the scope defines the analog bandwidth that gets sampled by the A/D. So this tells you no matter how fast the A/D runs, the fastest signal your scope will accurately measure is 70MHz. The analog bandwidth can also tell you the fastest rise time the scope will be able to reproduce/measure. If your rise time matches the analog bandwidth, that's a good sign your signal contains content faster than your scope can reproduce.

As Mark mentioned, this means with a 21MHz signal, you're only seeing the 3rd harmonic. Without the 5th (and maybe 7th) you won't see much of a square wave.

Remember that square waves are actually multiple sine waves added together.

Remember that square waves are actually multiple sine waves added together.

That applies for any wave shape that is not a sin wave.
Edges like you get in square waves have an infinite number of sin wave harmonics making up that vertical edge.

Remember that square waves are actually multiple sine waves added together.

To be more precise a square wave is made up of it's fundamental frequency plus all it's odd harmonics. So your scopes (and it's probe) ability to accurately display the real wave shape of the applied square wave is how many of those harmonics can the measurement instrument system accurately reproduce, which is related to the bandwidth specification of the measurement system. That is why GM stated that a good rule of thumb is that scope (and probe) should be rated at 10X the bandwidth of the frequency to be measured.

To put that into perspective that means to accurately display the true wave shape of a 10 Mhz square wave one should be using a measurement system (such as scope plus it's probe) rated to be flat out to 100 Mhz, or better yet higher then that. A scope rated at 10 Mhz will not be able to accurately display a 10 Mhz square wave.
So the ARM chip is developing true square waves (perhaps limited only by it's raising and falling transition specs, the tops and bottoms are 'flat' leaving the chip, so any variation (dips and peaks) in the wave shape is an artefact of your measurement system.

Lefty

It could be shorter but it's pretty darn short already and Agilent is clearly happy with it. This is the probe with the grounding clip attached:

http://www.home.agilent.com/en/pd-1938439-pn-N2862B/passive-probe-101-150-mhz-12-m

I'm sure that probe is fine, and not the problem. Those things are made for much higher
frequencies than you're using. Just don't add any extra length in hooking up the ground lead.
One thing you might try is using an output pin of the DUE that is close to a ground pin on a
header. Also, you definitely should try source series terminaton, as I mentioned.

The Arduino board female headers are not very ideal for sending out high-frequency signals
either. Especially if you're tapping into them using those funny little wire leads with male pins.
Totally not optimal.

The best way to sample a logic signal is to send the signal down a matched transmission line, such as 70 ohm coax - however to do that you might need something like a 70 ohm resistor in series with 1k, the logic signal drives this voltage divider, the coax samples the small voltage from the 70 ohm resistor (with very little distortion up to 10GHz or more), and at the other end of the signal a 70 ohm input-impedance amplifier can reconstruct the signal. The whole voltage divider setup ought to be in a coaxial cavity with carefully tuned impedance
to ground to match the signal impedance.

Its all very complex and inconvenient and is an acknowledgement that logic signals switch at microwave frequencies to microwave
circuit techniques are required to catch the fastest details.

Standard analog scope probes are a resistive divider and capacitive divider in parallel, which ought to have low distortion in theory, but the
length of coax acts as a complex impedance dependent on frequency at higher frequencies.

When you have a significant area of loop between the probe tip and the ground croc-clip this is going cause distortion
by adding significant stray inductance, and pick up noise by electromagnetic induction as well.

70 ohm resistor in series with 1k

What about 140:140 ohm divider, or etc, for more bang?

You need to match the transmission line impedance - in my example 70 ohm coax - fail to match impedances and you
lose the distortion-free properties of a transmission line. In my example I should have said a 75 ohm and 1k divider - the
two resistors in parallel are the effective impedance seen by the transmission line.

Having said that you can just have the 1k at the board end and purely use the 70ohm termination at the scope end,
probably better.

140 || 140 = 70.

If you really want to know your Due's pin flipping capability, google "arm bit banding".

I wouldn't expect bit-banding to be any faster than the parallel port "set" and "reset" registers... Either way you can modify a single bit with a single instruction. (now, whether the compiler produces equally good code for both cases is a separate question.)

Any discrepancies you see in the "square wave" shape that are due to the scope probe are also going to be present in any other "wire" that you connect to a pin toggling at that frequency. Putting a ~20MHz signal down a wire is not as trivial as it sounds (consider the old "thick ethernet", which had a 20MHz signal rate, sort of.)