Lesson plan

Sound and audio on microcontrollers lesson plan

Make sound and hear it, on the Uno and the ESP32: square waves with tone(), a sine from PWM, an MCP4725 DAC and its speed limit, I2S to a MAX98357A, recorded clips on a DFPlayer, an INMP441 microphone, and a spaceship voice clock.

4 chapters, 7 lessons, about 6 hours, on the Arduino Uno R3. Every lesson runs in the browser, with nothing to install.

The unit

Objectives and materials

By the end, students can

  • Square waves, PWM samples and a real voltage from a DAC.
  • I2S out to a speaker, and recorded clips from a card.
  • An I2S microphone: level, decibels and pitch.
  • A talking clock built from recorded words.

Materials for the whole unit

  • Arduino Uno R3
  • Capacitor x 2
  • DAC, MCP4725
  • DFPlayer Mini MP3 module
  • ESP32 DevKit V1
  • Frequency counter x 2
  • Half breadboard
  • I2S amplifier, MAX98357A
  • I2S microphone, INMP441
  • Oscilloscope
  • Piezo speaker
  • Pushbutton x 2
  • Resistor x 2
  • Speaker, 8 ohm

Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.

Chapter 1. About 2 h 25 min

Making a waveform

Square waves, PWM samples and a real voltage from a DAC.

LessonTimeWhat students do
Tone and square wavesA piezo on pin 8 plays a coin pickup, a power-up and a laser sweep: tone() makes a square wave, and the piezo and a frequency counter read the pitch back, to the hundredth of a hertz.50 min
  1. Play the coin sound
  2. The pitch you really get
  3. The counter agrees
  4. A power-up chord
  5. A laser sweep
  6. What a square wave is
  7. A brighter coin
PWM audioA real sine from a pin that can only be HIGH or LOW: 62.5 kHz PWM, a sample table played 8000 times a second by a timer interrupt, and two RC stages that smooth it into 440 Hz.50 min
  1. Run it
  2. The carrier
  3. The filter keeps the tone
  4. Samples and a phase accumulator
  5. The rules of sampling
  6. An octave up
  7. Where PWM audio is used
The MCP4725 DACA 12-bit DAC on I2C puts out a real voltage, no PWM and no filter. Then its speed limit: the sketch measures its own sample rate, and the pitch it can reach.45 min
  1. A real voltage
  2. Count the samples
  3. The counter agrees
  4. The full swing
  5. Why the bus is the limit
  6. Fewer points, higher pitch

Materials

  • Arduino Uno R3
  • Capacitor x 2
  • DAC, MCP4725
  • Frequency counter x 2
  • Oscilloscope
  • Piezo speaker
  • Resistor x 2

Check for understanding

Each student finishes "The MCP4725 DAC" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Fewer points, higher pitch".

Chapter 2. About 1 h 40 min

Digital audio

I2S out to a speaker, and recorded clips from a card.

LessonTimeWhat students do
I2S with the MAX98357AAn ESP32 sends a sine as 16-bit samples over I2S to a MAX98357A, which drives an 8 ohm speaker by itself. Then the amplifier’s gain and shutdown pins, wired on the canvas.50 min
  1. Samples over a bus made for audio
  2. The speaker plays A4
  3. Read the amplifier
  4. Wire the gain
  5. Shut it down
  6. An octave up
  7. Why I2S
DFPlayer clipsA DFPlayer Mini decodes recorded clips from a microSD card and drives the speaker itself; the Uno sends it commands over serial. Two buttons, a BUSY pin and a spaceship sound board.50 min
  1. A sound board
  2. Play a clip
  3. The BUSY pin
  4. Next clip
  5. When a clip ends
  6. Start somewhere else
  7. Clips or synthesis

Materials

  • Arduino Uno R3
  • DFPlayer Mini MP3 module
  • ESP32 DevKit V1
  • Half breadboard
  • I2S amplifier, MAX98357A
  • Pushbutton x 2
  • Resistor
  • Speaker, 8 ohm

Check for understanding

Each student finishes "DFPlayer clips" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Clips or synthesis".

Chapter 3. About 50 min

Listening

An I2S microphone: level, decibels and pitch.

LessonTimeWhat students do
The INMP441 microphoneSound in: an INMP441 sends 24-bit samples over I2S to an ESP32, which works out the level in dBFS from their RMS and the pitch from counting zero crossings, and flags when it gets loud.50 min
  1. A microphone with its own ADC
  2. How loud
  3. A pure tone
  4. dB SPL and dBFS
  5. Is it listening?
  6. A clap detector
  7. What the microphone hears here

Materials

  • ESP32 DevKit V1
  • I2S microphone, INMP441

Check for understanding

Each student finishes "The INMP441 microphone" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "What the microphone hears here".

Chapter 4. About 1 h

Boss project

A talking clock built from recorded words.

LessonTimeWhat students do
Boss: a talking clockA spaceship voice clock: a DFPlayer strings number clips into "ship time is eight o’clock", a button asks for the time, and a piezo chimes ding-dong on the hour.60 min
  1. Mission clock online
  2. Ship time
  3. Ask the time
  4. Hear it start
  5. The hour chime
  6. On the hour
  7. Announce the hour
  8. Make it yours

Materials

  • Arduino Uno R3
  • DFPlayer Mini MP3 module
  • Half breadboard
  • Piezo speaker
  • Pushbutton
  • Resistor
  • Speaker, 8 ohm

Check for understanding

Each student finishes "Boss: a talking clock" on their own, without opening the hints. The simulator checks 8 of its 8 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Make it yours".

Your class

Teach this unit as a class

Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.

The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.