Lessons and the learning game

Sound and audio on microcontrollers

Make sound and hear it: tone(), PWM audio, a DAC, I2S into an amplifier, clips from a card, a microphone that hears pitch, and a voice clock.

Take the course at /learn/path/sound-and-audio. It is an intermediate course on the Arduino Uno and the ESP32: it assumes the Uno course.

The benches

Each lesson has a small bench of its own:

  • Tone: an Uno with a piezo on pin 8 and a frequency counter on the same pin.
  • PWM audio: pin 9 through two stages of 1 k and 100 nF, with a scope and a counter before and after the filter.
  • DAC: an MCP4725 on I2C, its output on a scope and a counter.
  • I2S out: an ESP32 with a MAX98357A amplifier and an 8 ohm speaker.
  • DFPlayer: a DFPlayer Mini on a half breadboard with a speaker, its BUSY pin on pin 3, two buttons and, for the clock, a piezo.
  • I2S in: an ESP32 with an INMP441 microphone.

The piezo and speaker probes read the pitch they are driven at and the frequency counter reads any signal, so most steps check pitch automatically.

The lessons

Tone and square waves. A coin pickup, a power-up and a laser sweep, and why E6 comes out at 1322.8 Hz.

PWM audio. 62.5 kHz PWM, an 8 kHz sample interrupt, a phase accumulator and a filter make a 440 Hz sine, then 880.

The MCP4725 DAC. A real voltage, and the bus that limits it to 870 samples a second here.

I2S with the MAX98357A. 16-bit samples to a class D amplifier; the gain and shutdown pins wired on the canvas.

DFPlayer clips. A sound board with play and next buttons, the BUSY pin and the end-of-clip message.

The INMP441 microphone. Level in dBFS, pitch from zero crossings, dB SPL from the sensitivity, and a loudness flag.

Boss: a talking clock. Number clips joined into "ship time is eight o'clock", a button to ask, a ding-dong chime on the hour.

The sketches

Sketch Board What it does
sndtone Uno Coin, power-up and laser sounds with tone()
sndpwm Uno A 440 Hz sine from 62.5 kHz PWM and a sample table
snddac Uno A sine on the MCP4725 and its measured sample rate
sndi2s ESP32 A sine over I2S to the MAX98357A
snddf Uno A DFPlayer sound board
sndmic ESP32 INMP441 level and pitch
sndclock Uno The spaceship voice clock

What the simulation leaves out

There is no audio: the piezo and speaker show the pitch they are driven at, and the DFPlayer shows which clip it is playing and how far in, but nothing reaches your speakers. The INMP441 makes up its sound (a voice, a tone or noise at the level you pick). The I2C bus is slower than a real Uno's, so the DAC's sample rate is lower than on the bench. On the ESP32, use double rather than float in a sketch: float arithmetic needs the chip's FPU, which Mokxi does not model.

Displays: characters, digits and dots and the MAX98357A part page.