C1223 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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?

Name it

Notes

In equal temperament, the note with MIDI number n (A4 is 69, middle C is 60) has

f=440×2(n−69)/12Hz

Each semitone multiplies the frequency by 21/12; each octave doubles it.

Pitch from PWM

The tone's frequency is the PWM frequency, f=fclk/PERIOD, with PERIOD a whole number, so a target pitch is only approximate. Musicians measure small pitch differences in cents, hundredths of a semitone:

cents=1200log2⁡factualftarget

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 60/B 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
  1. Cycles per period. 107440=22727.3 so PERIOD = 22,727.
  2. The frequency it plays. 10722727=440.005Hz
  3. How far off, in cents. 1200log2⁡440.005440=0.02 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
  1. Quarter note. A quarter note is one beat: 60s120=0.5s
  2. Eighth note. It's half that, 250 ms.
  3. 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
  1. Cycles per wrap. Each wrap is half a cycle: 1072×523.25=9555.7
  2. Round to a whole TOP. TOP = 9555 gives 9556 cycles per wrap.
  3. The tone. f=1072×9556Hz=523.23Hz
  4. 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?

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.