Electronic – arduino – What’s the source of this noise in the Arduino oscillator

arduinonoiseoscillatoroscilloscope

I've been experimenting with techniques to rapidly switch on and off the digital output pins in an Arduino. This is the code I use:

#define PIN3_ON PORTD |= 0x8
#define PIN3_OFF PORTD &= ~0x8
#define NOP __asm__ __volatile__ ("nop\n\tnop\n\tnop\n\tnop\n\tnop\n\t")

void setup() {
    pinMode(3, OUTPUT);
}

void loop()
{
    cli();
    while (1) {
        PIN3_ON;
        NOP;
        NOP;
        PIN3_OFF;
        NOP;
        NOP;
    }
}

When I observe the output under a scope, I see some sort of quickly decaying oscillation after the pin is switched off:

enter image description here

I have observed this on an Arduino Uno and Nano328. What is the reason for this oscillation?

Best Answer

Try clipping your ground lead onto the ground pin that's physically closest to the pin you're looking at. If that doesn't change anything, and if you can, take up any slack in the ground lead of your scope by wrapping it around the probe.

I can't find a good article right off the bat (I'm sure that every scope maker out there has one) -- here's the few pointers I know:

  • Always ground the probe. If the signal is the least bit fast or important, ground each probe close to the signal.
  • You can do the ground-wrapping trick to make things a bit better.
  • If it's a really sharp-edged signal and you need to be sure, ground the probe right at the tip. If you pull the spring hook off the end of a typical O-scope probe, you'll see a metal sleeve around the tip. That's the probe ground. You can wrap a wire around that and ground it as close as possible to the signal you're probing (or you can look in the stuff that came with the probe and see if there's a little sprig-looking thing in there -- that's what it's for).
  • Make sure your probes are compensated, and calibrated if accuracy is important.

(If someone reading this knows a good article or YouTube presentation, please post a link -- this is one of those things that I've collected bits & pieces of over the years but have never had to articulate clearly).