C12 · Lesson 23 · Making sound with PWM
Pitch, notes and timing from a square wave
A small speaker or piezo buzzer driven by a PWM pin makes a tone whose pitch is the PWM frequency. Change the frequency every few hundred milliseconds and it plays a tune: that's how countless appliances beep and chirp. Doing it well takes a little music theory (each note's frequency), a little arithmetic (how close an integer PERIOD gets to it) and careful timing, because a melody with sloppy rhythm sounds wrong long before a slightly sharp note does.
- The program plays a C major scale on the speaker on PWM channel 2: each note 200 ms, with a 50 ms gap. Run it, then press Listen to hear the run.
- Drag the timeline: the speaker's readout shows each note's frequency as it plays. The notes come from the table at the top.
- Change the duty from 32768 (50%) to 3000 and listen again: the same pitches, thinner and quieter.
Predict
A4 is 440 Hz. What's the A one octave higher?
880 Hz: an octave is a doubling of frequency. Between them lie 12 semitones, each a factor of , so that 12 of them multiply to exactly 2.
Name it
Notes
In equal temperament, the note with MIDI number n (A4 is 69, middle C is 60) has
Each semitone multiplies the frequency by ; each octave doubles it.
Pitch from PWM
The tone's frequency is the PWM frequency, , with PERIOD a whole number, so a target pitch is only approximate. Musicians measure small pitch differences in cents, hundredths of a semitone:
Errors under about 5 cents are inaudible; at 10 MHz, PERIOD values in the tens of thousands keep every note within a few hundredths of a cent.
Timbre and loudness
A 50% square wave contains the fundamental and the odd harmonics (3f, 5f, 7f...) with amplitudes falling as 1/n. Narrower pulses weaken the fundamental and change the timbre; duty cycle is a crude volume control.
Rhythm
At a tempo of B beats per minute, a quarter note lasts seconds. A melody is a list of (note, duration) pairs; leaving a short gap between notes keeps repeated notes distinct.
Worked example
What PERIOD plays A4, and how far off is the result?
Show the solution
- Cycles per period. so PERIOD = 22,727.
- The frequency it plays.
- How far off, in cents. cents sharp, far below what anyone can hear.
Answer PERIOD = 22,727, which plays 440.005 Hz, 0.02 cents sharp.
Worked example
A tune is written at 120 beats per minute. How long are a quarter note and an eighth note?
Show the solution
- Quarter note. A quarter note is one beat:
- Eighth note. It's half that, 250 ms.
- Fill it. A 200 ms note with a 50 ms gap fills an eighth exactly.
Answer a quarter note lasts 0.5 s and an eighth note 250 ms.
Worked example
What TOP plays C5 (523.25 Hz), toggling the pin on each wrap, and how close is it?
Show the solution
- Cycles per wrap. Each wrap is half a cycle:
- Round to a whole TOP. TOP = 9555 gives 9556 cycles per wrap.
- The tone.
- How close. That's 0.004% flat, about 0.06 cents.
Answer TOP = 9555, which plays 523.23 Hz, 0.004% flat.
Check
What's the frequency of MIDI note 60?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
Check
What PWM PERIOD comes closest to 523 Hz on the 10 MHz board?
Check
What frequency does PERIOD = 22727 make?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
Check
A note meant to be 440 Hz plays at 445 Hz. How many cents off is it?
Check
At 90 beats per minute, how long is a note of 1 beats?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Check
A 440 Hz square wave: at what frequency is its harmonic number 3?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
Check
Which change makes a PWM tone quieter without changing its pitch?
Check
Play MIDI note N (64) on the speaker on PWM channel 2, as a 50% square wave. (The frequency table is up to you: compute it, or hard-code it.)
C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.
Check
A timer toggles a buzzer pin on each wrap to play 440 Hz.
(a)
What TOP (with PRESCALE = 0, nearest)?
(b)
What frequency actually plays?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
Check
A tune is written at 96 beats per minute, a quarter note per beat.
(a)
How long is a quarter note?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
(b)
How long is a dotted quarter (one and a half beats)?
Explain why
Why is a lookup table of PWM periods the usual way to play notes on a microcontroller?
Computing a note's frequency needs a power of two with a fractional exponent, , and then a division to turn it into a period: floating-point work that's slow, or unavailable, on a small chip. But only a few dozen notes are ever used, and each one's period never changes, so they can all be computed once, on a computer, and stored as integers. Playing a note is then a single array read and a register write. The table takes a few bytes per note, and it can be checked against the formula before it's ever compiled in.
Practice
Keep practicing
12 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.