Sound
Mini speaker
A small speaker with a real amplifier that plays tunes. One button plays or stops, two change the volume, and a ring of light shows the level.
- Hard
- about 80 min
- ESP32-S3
- 8 parts
- 14 wires
- 109 lines of code
ESP32 packs are built on real parts: the simulator runs Arduino Uno circuits only.
Idea
A buzzer can only click on and off. Here the board calculates the shape of the sound wave, 16,000 points every second, and sends it as numbers to the amplifier. That is how all digital audio works.
What you learn
Digital audio over I2S, making a tone from a sine wave, and volume as a multiplier.
How it works
input
PLAY button
QUIETER button
LOUDER button
decides
ESP32-S3
output
Amplifier + speaker
LED ring
Powered with 5 V through the board's USB-C port.
A reference design. The sketch is compiled with the real ESP32 toolchain, and its pins are checked against the wiring table automatically. It has not yet been built and tested on real parts by us, so wire it on the bench and run the checks before you build it into a case. Real modules vary: follow the labels printed on yours, not their position in the picture.
Parts
0 of 8 ready
No prices here: they change by shop and by week. Buy each part by its name and the note beside it.
You also need
A computer with Arduino IDE 2 installed (free)
3D wiring
One jumper per step, with the USB cable unplugged. Several wires may share 3V3, 5V or GND: the expansion board or breadboard gives each its own pin.
Step 0 of 14
Lay out the parts
Seat the board on its expansion board or breadboard and lay the modules out round it. Nothing is connected yet.
All connections
mini-speaker.ino
// Mini speaker
// Plays tunes through a real amplifier. PLAY starts the next tune or stops
// it; the other two buttons change the volume, shown on the ring of light.
#include <ESP_I2S.h>
#include <Adafruit_NeoPixel.h>
const int PIN_PLAY_A = 5;
const int PIN_QUIETER_A = 6;
const int PIN_LOUDER_A = 7;
const int PIN_AMP_BCLK = 41;
const int PIN_AMP_LRC = 42;
const int PIN_AMP_DIN = 2;
const int PIN_RING_DIN = 16;
const int SAMPLE_RATE = 16000;
const int LED_COUNT = 12;
I2SClass i2s;
Adafruit_NeoPixel ring(LED_COUNT, PIN_RING_DIN, NEO_GRB + NEO_KHZ800);
// Tunes: note frequency in Hz (0 = a rest) and length in milliseconds.
struct Note { int hz; int ms; };
const Note TUNE_A[] = {{262, 300}, {262, 300}, {392, 300}, {392, 300}, {440, 300}, {440, 300}, {392, 600},
{349, 300}, {349, 300}, {330, 300}, {330, 300}, {294, 300}, {294, 300}, {262, 600}};
const Note TUNE_B[] = {{330, 250}, {330, 250}, {349, 250}, {392, 250}, {392, 250}, {349, 250}, {330, 250}, {294, 250},
{262, 250}, {262, 250}, {294, 250}, {330, 250}, {330, 375}, {294, 125}, {294, 500}};
const Note TUNE_C[] = {{392, 200}, {0, 50}, {392, 200}, {440, 400}, {392, 400}, {523, 400}, {494, 800}};
const Note* TUNES[] = {TUNE_A, TUNE_B, TUNE_C};
const int TUNE_LENGTHS[] = {sizeof(TUNE_A) / sizeof(Note), sizeof(TUNE_B) / sizeof(Note), sizeof(TUNE_C) / sizeof(Note)};
const int TUNE_COUNT = 3;
int volume = 6; // 0..12, one LED each
int tune = -1;
bool playing = false;
int noteIndex = 0;
void showVolume() {
for (int i = 0; i < LED_COUNT; i++) ring.setPixelColor(i, i < volume ? ring.Color(0, 60, 160) : 0);
ring.show();
}
bool tapped(int pin) {
if (digitalRead(pin) != LOW) return false;
delay(25);
if (digitalRead(pin) != LOW) return false;
while (digitalRead(pin) == LOW) delay(5);
return true;
}
void checkButtons() {
if (tapped(PIN_PLAY_A)) {
playing = !playing;
if (playing) {
tune = (tune + 1) % TUNE_COUNT;
noteIndex = 0;
}
}
if (tapped(PIN_LOUDER_A) && volume < LED_COUNT) {
volume++;
showVolume();
}
if (tapped(PIN_QUIETER_A) && volume > 0) {
volume--;
showVolume();
}
}
// One note as a sine wave, with a short fade at each end so it does not click.
void playNote(const Note& note) {
static int16_t buffer[256];
const int total = (long)SAMPLE_RATE * note.ms / 1000;
const float amplitude = 28000.0f * volume / LED_COUNT;
const int fade = SAMPLE_RATE / 100;
for (int done = 0; done < total;) {
int count = min(256, total - done);
for (int i = 0; i < count; i++) {
int n = done + i;
float envelope = n < fade ? (float)n / fade : n > total - fade ? (float)(total - n) / fade : 1.0f;
buffer[i] = note.hz == 0 ? 0 : (int16_t)(sin(2.0f * PI * note.hz * n / SAMPLE_RATE) * amplitude * envelope);
}
i2s.write((uint8_t*)buffer, count * sizeof(int16_t));
done += count;
}
}
void setup() {
pinMode(PIN_PLAY_A, INPUT_PULLUP);
pinMode(PIN_QUIETER_A, INPUT_PULLUP);
pinMode(PIN_LOUDER_A, INPUT_PULLUP);
ring.begin();
ring.setBrightness(60);
showVolume();
i2s.setPins(PIN_AMP_BCLK, PIN_AMP_LRC, PIN_AMP_DIN);
i2s.begin(I2S_MODE_STD, SAMPLE_RATE, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO);
}
void loop() {
checkButtons();
if (!playing) {
delay(10);
return;
}
playNote(TUNES[tune][noteIndex]);
noteIndex++;
if (noteIndex >= TUNE_LENGTHS[tune]) {
noteIndex = 0; // the tune repeats until PLAY is pressed again
delay(400);
}
}
Libraries
- Adafruit NeoPixel: install it from Sketch > Include Library > Manage Libraries.
Uploading
First time with an ESP32: in Arduino IDE open Boards Manager, search “esp32” and install “esp32 by Espressif Systems”.
- Open the sketch in Arduino IDE.
- Plug the board in with the USB-C cable.
- Tools > Board > ESP32S3 Dev Module, then Tools > Port and choose the board's port.
- Press Upload (the arrow). Wait for “Done uploading”.