Projects / PWM learning lab with a scope

PWM learning lab with a scope

Beginner1 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. UNO

    Wire the knob

    Outer legs to 5V and GND, middle to A0.

  2. UNO9.0 mA

    Wire the LED

    D9 through 220 ohm to the LED and GND.

  3. UNO

    Wire the button

    D2 to GND.

  4. UNO9.0 mA

    Upload the sketch

    The LED brightness follows the knob.

  5. UNO

    Measure the average

    Meter from D9 to GND.

  6. UNO

    Plot it

    Note the duty and voltage at five knob positions.

  7. UNO9.1 mA

    Change the frequency

    Press the button and watch the LED at 30 Hz.

  8. UNO

    Repeat the measurement

    The average stays the same.

In short

How do you build a PWM learning lab with a scope?

Fade an LED and compare duty, frequency and average voltage with a meter or scope. You need Arduino Uno R3, LED 5 mm red, Resistor 220 Ω, Potentiometer 10 kΩ, Tactile push button 6 mm, Digital multimeter and Breadboard 830 Tie Points. It takes 8 steps and about 60 minutes.

Level
Beginner
Time
About 60 min
Parts
7

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Pot outer legsUno 5V and GNDSets the duty
Pot middle legUno A0Reads the knob
Uno D9220 ohm resistorPWM output on Timer 1
220 ohm resistorRed LED anode (long leg)LED load
LED cathode (short leg)Uno GNDCommon ground
ButtonUno D2 to GNDChanges the PWM frequency
Multimeter red probeUno D9Measures the average voltage
Multimeter black probeUno GNDCommon ground

Code

PWM, or pulse width modulation, switches a pin on and off very fast and changes how long it stays on in each cycle, which is the duty cycle. The LED seems to fade because your eye averages the flashes, and a multimeter on DC volts also shows the average: at 50 percent duty about 2.5 volts, at 100 percent 5 volts. This lab makes that link visible. Turn the knob and note the duty from the serial monitor, the voltage on the meter and the brightness of the LED, then plot duty against volts, which should be a straight line. The button changes the PWM frequency on pin 9 from about 31 kilohertz down to 30 hertz by changing the Timer 1 divider. At 490 hertz and above the LED looks steady, while at 30 hertz you can see it flicker, although the meter still shows the same average. If you have an oscilloscope, connect it to pin 9 and watch the pulses get wider as the duty goes up. Changing Timer 1 affects the Servo library, so do not use that library in the same sketch.

Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

const int PWM_PIN = 9, POT = A0, BTN = 2;

// Timer 1 prescaler settings and the PWM frequency each gives on pins 9 and 10
const byte PRESCALE[5] = {1, 2, 3, 4, 5};
const char* FREQ[5] = {"31 kHz", "3.9 kHz", "490 Hz", "122 Hz", "30 Hz"};
int mode = 2;                                   // start at the default 490 Hz
bool wasDown = false;

void setFreq(int m) {
  TCCR1B = (TCCR1B & 0b11111000) | PRESCALE[m];
  Serial.print("Frequency: "); Serial.println(FREQ[m]);
}

void setup() {
  pinMode(PWM_PIN, OUTPUT);
  pinMode(BTN, INPUT_PULLUP);
  Serial.begin(9600);
  setFreq(mode);
}

void loop() {
  bool down = (digitalRead(BTN) == LOW);
  if (down && !wasDown) { mode = (mode + 1) % 5; setFreq(mode); delay(30); }
  wasDown = down;

  int value = analogRead(POT) / 4;              // 0 to 255
  analogWrite(PWM_PIN, value);

  float duty = value * 100.0 / 255.0;
  float avg = value * 5.0 / 255.0;              // expected average voltage at the pin
  Serial.print("Duty "); Serial.print(duty, 0);
  Serial.print(" %  expected "); Serial.print(avg, 2); Serial.println(" V");
  delay(300);
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Uno with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

Frequency: 490 Hz
Duty 50 %  expected 2.51 V
Duty 50 %  expected 2.49 V
Duty 50 %  expected 2.51 V
Duty 50 %  expected 2.49 V
Duty 50 %  expected 2.51 V

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Knob at minimumLED off, about 0 V
Knob half wayAbout 2.5 V on the meter
Knob at maximumLED full, about 5 V
Frequency 30 HzVisible flicker
Frequency 31 kHzSteady, meter unchanged

If it does not work

Meter reads nothing

Check the probes are on D9 and GND.

No fade

Check the LED direction and that D9 is used.

Button does nothing

Check D2 and GND.

Meter reading jumps

Switch to a higher PWM frequency.

Servo library does not work

It shares Timer 1, use a different timer.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the PWM learning lab with a scope?

Arduino Uno R3, LED 5 mm red, Resistor 220 Ω, Potentiometer 10 kΩ, Tactile push button 6 mm, Digital multimeter and Breadboard 830 Tie Points.

How long does the PWM learning lab with a scope take to build?

About 60 minutes for a beginner maker, in 8 steps.

How do I know the PWM learning lab with a scope works?

Knob at minimum: LED off, about 0 V. Knob half way: About 2.5 V on the meter. Knob at maximum: LED full, about 5 V.

Meter reads nothing: what should I check?

Check the probes are on D9 and GND.

No fade: what should I check?

Check the LED direction and that D9 is used.

Button does nothing: what should I check?

Check D2 and GND.

Meter reading jumps: what should I check?

Switch to a higher PWM frequency.