ESP32 I2S sine player
A MAX98357A I2S amplifier on an ESP32 playing a 440 Hz sine into a speaker, the samples computed in the sketch with ESP_I2S.h. No WAV file and no SD card. The whole build, an ESP32 DevKit and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Intermediate Runs in your browser. Free, and no account needed.
The parts retail for about $13. On Mokxi they're free.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// MAX98357A 440 Hz tone on an ESP32.
//
// The MAX98357A is an I2S amplifier: it takes the bit clock (BCLK), the word
// select (LRC) and the samples (DIN) and drives a speaker directly. With SD
// left open it plays the average of the left and right channels; GAIN left
// open is 9 dB.
//
// This sketch uses ESP_I2S.h, the I2S library of Arduino-ESP32 3.x. In
// setup() it fills a table with one cycle of a sine; loop() keeps writing the
// next block of samples, the same value to the left and the right slot, so
// the speaker hears a steady 440 Hz. At 16 kHz, 440 Hz is 36.36 samples a
// cycle, so the table is read with a 16.16 fixed-point phase rather than a
// whole number of samples, and no float is touched per sample.
//
// Wiring: VIN to 5V (VIN on the DevKit), GND to GND, BCLK to GPIO 26,
// LRC to GPIO 25, DIN to GPIO 22, a speaker across SPK+ and SPK-.
#include <ESP_I2S.h>
#include <math.h>
#define I2S_BCLK 26
#define I2S_LRC 25
#define I2S_DIN 22
const uint32_t kSampleRate = 16000;
const uint32_t kFrequency = 440;
const int kTable = 256;
const int16_t kAmplitude = 8000; // about a quarter of full scale
I2SClass i2s;
int16_t sine[kTable];
int16_t block[2 * 128]; // 128 stereo frames: left, right, left, right ...
uint32_t phase = 0;
// Table steps a sample, in 16.16 fixed point: 440 * 256 / 16000.
const uint32_t kStep = (uint32_t)(((uint64_t)kFrequency * kTable << 16) / kSampleRate);
void setup() {
Serial.begin(115200);
for (int i = 0; i < kTable; i++) {
sine[i] = (int16_t)(kAmplitude * sinf(2.0f * (float)M_PI * i / kTable));
}
i2s.setPins(I2S_BCLK, I2S_LRC, I2S_DIN);
if (!i2s.begin(I2S_MODE_STD, kSampleRate, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO)) {
Serial.println("Failed to initialize I2S!");
while (true) delay(1000);
}
Serial.println("Playing 440 Hz");
}
void loop() {
for (int i = 0; i < 128; i++) {
int16_t s = sine[(phase >> 16) & (kTable - 1)];
block[2 * i] = s; // left
block[2 * i + 1] = s; // right
phase += kStep;
}
i2s.write((uint8_t *)block, sizeof(block));
}
Parts list
3 parts, plus the jumper wires. They retail for about $13; on Mokxi they're free.
How it is wired
7 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- ESP32 DevKit V1 pin D25; I2S amplifier, MAX98357A pin LRC
- ESP32 DevKit V1 pin D26; I2S amplifier, MAX98357A pin BCLK
- Ground: ESP32 DevKit V1 pin GND; I2S amplifier, MAX98357A pin GND
- ESP32 DevKit V1 pin VIN; I2S amplifier, MAX98357A pin VIN
- ESP32 DevKit V1 pin D22; I2S amplifier, MAX98357A pin DIN
- I2S amplifier, MAX98357A pin SPK+; Speaker, 8 ohm pin 1
- I2S amplifier, MAX98357A pin SPK-; Speaker, 8 ohm pin 2
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.