Implementing STM32 I2S audio DAC and amplifier projects brings professional-quality digital audio to your embedded builds. The I2S (Inter-IC Sound) protocol is the standard for connecting microcontrollers to DACs, amplifiers, and microphone arrays. This tutorial covers STM32 I2S configuration and builds a working audio playback system.
Table of Contents
- I2S Protocol Explained
- Indian Components for I2S Audio
- STM32CubeMX I2S Configuration
- Audio DAC Output Code
- Connecting MAX98357 Amplifier
- DMA Audio Playback
- Frequently Asked Questions
I2S Protocol Explained
I2S (Inter-IC Sound), developed by Philips, uses three signals: BCLK (bit clock), LRCLK/WS (word select/left-right clock), and SD (serial data). The LRCLK runs at the sample rate (e.g., 44.1 kHz for CD quality) and indicates left/right channel. BCLK is typically 32–64× the LRCLK for 16–32 bit samples. STM32’s I2S peripheral can act as master (generating clocks) or slave (following external clocks).
Indian Components for I2S Audio
I2S DAC and amplifier modules available in India:
- MAX98357A I2S amplifier module (₹200–350): Class D 3.2W amplifier with built-in I2S DAC. Direct from STM32 I2S to speaker. Very popular for IoT audio projects.
- PCM5102A DAC module (₹250–400): High-quality stereo DAC, 32-bit/384 kHz. Connects to 3.5mm headphone jack or line-out.
- PT8211 DAC module (₹80–150): Budget 16-bit stereo DAC, basic audio quality but extremely affordable.
- INMP441 I2S MEMS microphone (₹150–250): For audio input applications — speech recognition, sound level monitoring.
STM32CubeMX I2S Configuration
For STM32F411 (Black Pill, Nucleo-F411RE available in India at ₹1,500–2,500):
- Enable I2S2 peripheral (or I2S1 on your specific board)
- Set Mode: Master Transmit
- Set Audio Frequency: 44100 Hz
- Set Data Format: 16 Bits Data on 16 Bits Frame
- Enable DMA for I2S2_TX (DMA1 Stream4 for F4 series)
- Pins: I2S2_CK → PB13, I2S2_SD → PB15, I2S2_WS → PB12
Audio DAC Output Code
/* STM32 I2S audio playback with DMA */
#include "stm32f4xx_hal.h"
#include "math.h"
I2S_HandleTypeDef hi2s2;
DMA_HandleTypeDef hdma_spi2_tx;
#define SAMPLE_RATE 44100
#define BUFFER_SIZE 256
uint16_t audioBuffer[BUFFER_SIZE]; // 16-bit stereo samples (L, R interleaved)
// Generate sine wave (440 Hz, A4 note)
void generateSineWave(uint16_t *buffer, int size) {
static float phase = 0.0f;
float freq = 440.0f;
float step = 2.0f * M_PI * freq / SAMPLE_RATE;
for (int i = 0; i 2.0f * M_PI) phase -= 2.0f * M_PI;
}
}
int main(void) {
HAL_Init();
SystemClock_Config();
MX_I2S2_Init();
MX_DMA_Init();
generateSineWave(audioBuffer, BUFFER_SIZE);
// Start circular DMA audio output
HAL_I2S_Transmit_DMA(&hi2s2, audioBuffer, BUFFER_SIZE);
while(1) {
// Main loop free - audio runs autonomously via DMA
}
}
Connecting MAX98357A Amplifier
MAX98357A connection to STM32 I2S:
| MAX98357A Pin | STM32F4 (I2S2) |
|---|---|
| VIN | 5V (separate power for speaker) |
| GND | GND (shared) |
| DIN | PB15 (I2S2_SD) |
| BCLK | PB13 (I2S2_CK) |
| LRC | PB12 (I2S2_WS) |
| SD (shutdown) | Float = 3W, GND = shutdown |
DMA Audio Playback
Use DMA double-buffering for seamless audio: STM32’s DMA half-complete and complete callbacks fill the buffer while the other half plays. This prevents audio glitches from CPU interrupt latency.
// DMA callbacks for double-buffer audio
void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s) {
generateSineWave(&audioBuffer[0], BUFFER_SIZE/2); // Fill first half
}
void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s) {
generateSineWave(&audioBuffer[BUFFER_SIZE/2], BUFFER_SIZE/2); // Fill second half
}
Frequently Asked Questions
Can STM32 I2S play WAV files from SD card?
Yes. Read WAV file data from SD card (via SPI FATFS) into the DMA audio buffer. Parse the WAV header to extract sample rate and bit depth, configure I2S accordingly, then stream raw PCM data.
What speaker should I use with MAX98357A in India?
Any 4Ω or 8Ω speaker works. Common 2-inch (50mm) speakers from Indian electronics markets at ₹50–100 work well. For 3W MAX98357A output, use speakers rated at least 1W.
Is there an I2S microphone module for Indian voice projects?
Yes. INMP441 MEMS microphone (₹150–250) and SPH0645LM4H are I2S digital microphones available in India. Both work with STM32 I2S in slave receive mode.
Add comment