PWM learning lab with a scope
Build it step by step
0 of 8 doneWire the knob
Outer legs to 5V and GND, middle to A0.
Wire the LED
D9 through 220 ohm to the LED and GND.
Wire the button
D2 to GND.
Upload the sketch
The LED brightness follows the knob.
Measure the average
Meter from D9 to GND.
Plot it
Note the duty and voltage at five knob positions.
Change the frequency
Press the button and watch the LED at 30 Hz.
Repeat the measurement
The average stays the same.
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
- Development BoardsBuyArduino Uno R3 × 1
- SemiconductorsBuyLED 5 mm red × 1
- PassivesBuyResistor 220 Ω × 1
- PassivesBuy
- SwitchesBuy
- Tools & InstrumentsBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Pot outer legs | Uno 5V and GND | Sets the duty |
| Pot middle leg | Uno A0 | Reads the knob |
| Uno D9 | 220 ohm resistor | PWM output on Timer 1 |
| 220 ohm resistor | Red LED anode (long leg) | LED load |
| LED cathode (short leg) | Uno GND | Common ground |
| Button | Uno D2 to GND | Changes the PWM frequency |
| Multimeter red probe | Uno D9 | Measures the average voltage |
| Multimeter black probe | Uno GND | Common 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.
| Check | Expected |
|---|---|
| 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 |
| Frequency 30 Hz | Visible flicker |
| Frequency 31 kHz | Steady, 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 →
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.
