PWM Tone Lab
Run the PWM Tone Lab MicroSim Fullscreen
Edit in the p5.js Editor
About This MicroSim
A Pico makes sound by switching a pin on and off quickly. This is PWM (pulse-width modulation). Two numbers control the result:
- Frequency is how many on/off cycles happen each second. It sets the pitch.
- Duty cycle is the fraction of each cycle that the pin is on. It changes the character of the tone, but not its pitch.
The black oscilloscope shows 10 milliseconds of the signal (1 ms per grid division). The readout above it gives the period (\( T = 1/f \)) and the on and off times. The piano keyboard highlights the musical note closest to the frequency, and Show formula prints \( f = 440 \times 2^{n/12} \) for that note, where \( n \) is the number of semitones above or below A4.
The MicroSim opens silent. Sound starts only when you press Play. A browser's plain square-wave oscillator is always 50% duty, so this MicroSim builds the tone from a custom waveform that matches the duty cycle you choose.
How to Use
- Press Play to hear the tone, and Stop to silence it.
- Drag the Frequency slider. Count how many cycles fit in the 10 ms window and watch the highlighted piano key move.
- Use Jump to a note to go straight to A4, C5, and other notes.
- Drag the Duty cycle slider. The cycles stay the same width while the on part grows or shrinks. Try 0% and 100%: the pin never switches, so there is no sound.
- Check Show formula to see how the note's frequency is calculated.
Iframe Embed Code
You can add this MicroSim to any web page by adding this to your HTML:
1 2 3 4 | |
Lesson Plan
Grade Level
9-12 (Clocks and Watches with AI)
Duration
15 minutes
Prerequisites
- What a GPIO pin's high (3.3 V) and low (0 V) states mean
- Frequency measured in hertz
Learning Objectives
Students will be able to:
- Explain that frequency sets pitch and duty cycle sets how long each cycle is on
- Calculate the period, on time and off time for a given frequency and duty cycle
- Apply \( f = 440 \times 2^{n/12} \) to find the frequency of a note
Activities
- Explore (4 min): Press Play and jump between A4 and A5. Read both frequencies. What happens to the frequency when the note goes up one octave?
- Calculate, then check (6 min): At 1,000 Hz and 25% duty, calculate the period, on time and off time by hand. Set the sliders and compare with the readout.
- Listen for duty (5 min): Keep the frequency at 440 Hz and move the duty cycle from 50% to 10%. Does the pitch change? Describe how the sound changes. Then use the formula to predict the frequency of C6 (\( n = 15 \)) and check it with the note menu.
Assessment
- The student explains why a 2,000 Hz wave shows twice as many cycles in 10 ms as a 1,000 Hz wave.
- The student calculates that a 500 Hz wave has a 2 ms period, and that 25% duty gives 0.5 ms on.
- The student uses the formula to find middle C (\( n = -9 \)): about 261.6 Hz.
References
- Pulse-width modulation - Wikipedia article on PWM signals, frequency and duty cycle.
- Piano key frequencies - Wikipedia table and formula for the frequency of every note relative to A440.
- MicroPython PWM class - Official documentation for
freq()andduty_u16()on the Pico. - OscillatorNode (MDN) - How the browser's Web Audio API generates the tone you hear.