Try Embedded

Oscilloscope basics: see PWM, a voltage and a pulse

Learn the oscilloscope step by step: Autoset, reading frequency and duty, cursors, AC coupling and catching a single pulse.

Basic · Beginner

In a real product: Hardware validation lab (Industrial): Engineers verify signal timing and quality on a scope.

A logic analyzer tells you whether a wire is 0 or 1. An oscilloscope shows the voltage itself, so it answers questions like "how bright is this PWM really?", "what voltage is the knob giving the ADC?" and "how long was that pulse?". This circuit gives it three signals to look at, and the steps below walk through the controls one at a time.

What you'll learn

  • What Autoset does, and how to read frequency, duty cycle, mean and peak-to-peak voltage
  • How the time base (time per division) and volts per division change what you see
  • Measuring with cursors: the time between two points, and the voltage at a level
  • What AC coupling hides, and how Single catches an event that happens once

How it works

The sketch reads the potentiometer on A0 and drives a PWM on D9 with it: analogWrite(9, analogRead(A0) / 4). Pin 9 runs its PWM at 490 Hz, and the duty cycle follows the knob from 0 to 100 %. Each time the button on D2 is pressed, D7 gives one 2 ms pulse.

The scope's CH1 (yellow) watches the PWM on D9 and CH2 (cyan) the knob's voltage on A0. The Oscilloscope panel under the canvas has every control; press ? in it for a guide to each one.

Try this

  • Start: run the simulation and press Autoset. CH1 shows a square wave and CH2 a flat line. Under the screen, CH1 reads about Freq 490 Hz and Duty 25 %: the knob starts at a quarter turn.
  • Turn the knob (click the potentiometer) and watch the duty cycle follow it. Turn on Mean in the Measure row: for a 5 V PWM, the mean is 5 V × duty, the voltage an LED or a motor "feels" on average. CH2's Max is the knob's voltage, the same one analogRead turns into 0 to 1023.
  • Time base: click ◂ and ▸ in the M box. Fewer periods fit on the screen as it gets faster, and more as it gets slower. The scope steps through 1, 2 and 5 like a real one.
  • Cursors: click on a rising edge of CH1 (cursor A), then Shift+click on the next one (cursor B). Δt is the period, about 2.04 ms, and 1/Δt the frequency. Switch the cursors to Volts and click CH2's line to read its voltage off the screen.
  • AC coupling: press DC in CH1's box to change it to AC. The wave drops so that its average sits at 0 V: AC coupling shows only the changes, which is how you see a small ripple riding on a large DC voltage. Press it again to go back to DC.
  • Catch a single pulse: move CH2's wire from A0 to D7. Set the trigger (T box) to 2 ↑, set the M box to 1 ms/div, and press Single: the status turns amber and waits. Press the button on D2. The scope stops on the pulse. Turn on +Width in the Measure row: about 2.01 ms, the sketch's delay(2) plus the time digitalWrite itself takes. The cursors give the same answer.
  • Stop and Run: press Stop to freeze the screen while you read it, and Run to go back to the live signal.

sketch.ino

// Signals for the oscilloscope: PWM on D9 whose duty follows the potentiometer on A0 (CH1 and CH2 of the scope),
// and a 2 ms pulse on D7 each time the button on D2 is pressed.
const int PWM_PIN = 9;     // Timer1: 490 Hz
const int PULSE_PIN = 7;
const int BUTTON = 2;

bool wasPressed = false;

void setup() {
  pinMode(PULSE_PIN, OUTPUT);
  pinMode(BUTTON, INPUT_PULLUP);
}

void loop() {
  analogWrite(PWM_PIN, analogRead(A0) / 4);   // 0 to 1023 becomes a duty of 0 to 255
  bool pressed = digitalRead(BUTTON) == LOW;
  if (pressed && !wasPressed) {               // once a press, on the way down
    digitalWrite(PULSE_PIN, HIGH);
    delay(2);
    digitalWrite(PULSE_PIN, LOW);
  }
  wasPressed = pressed;
  delay(5);
}

Lessons behind it