Audio System
The ESP32-P4-WIFI6-DEV-KIT includes a complete audio signal chain: an ES8311 audio codec, an NS4150B power amplifier for speaker output, a 3.5mm headphone jack, and an onboard SMD microphone. This guide covers hardware connections and I2S configuration for playback and recording.
Audio hardware overview
Section titled “Audio hardware overview” I2S Bus ESP32-P4 ────────────────── ES8311 Codec │ ┌──────────┼──────────┐ │ │ │ Speaker Headphone Microphone (NS4150B) (3.5mm) (SMD) Amplifier Jack Onboard| Component | Details |
|---|---|
| Audio codec | ES8311 — 24-bit, 8—96 kHz sample rates, I2C control + I2S data |
| Power amplifier | NS4150B — 3W mono, Class D, drives the speaker connector |
| Speaker connector | MX1.25 2-pin, rated for 8 ohm 2W speakers |
| Headphone output | 3.5mm stereo jack |
| Microphone | SMD MEMS microphone, soldered on-board |
I2S pin mapping
Section titled “I2S pin mapping”The audio codec connects to the ESP32-P4 via I2S with the following GPIO assignments:
| Signal | GPIO | Direction | Description |
|---|---|---|---|
| MCLK | GPIO13 | Output | Master clock to codec |
| SCLK (BCLK) | GPIO12 | Output | Bit clock |
| ASDOUT | GPIO11 | Output | Audio data out (ESP32-P4 to codec) |
| LRCK (WS) | GPIO10 | Output | Left/right word select |
| DSDIN | GPIO9 | Input | Audio data in (codec to ESP32-P4) |
| PA_Ctrl | GPIO53 | Output | Power amplifier enable (HIGH = on) |
Speaker connection
Section titled “Speaker connection”-
Locate the MX1.25 2-pin speaker connector on the board (labeled SPK or SPEAKER on the silkscreen).
-
Connect an 8-ohm speaker rated at 2W or less. The connector accepts a standard MX1.25 plug.
-
Ensure correct polarity if your speaker is marked — reversed polarity will not damage the amplifier but will invert the audio phase.
Headphone jack
Section titled “Headphone jack”The 3.5mm jack provides stereo output directly from the ES8311 codec. It is active whenever the codec’s DAC output is enabled — no additional GPIO control is needed.
When headphones are inserted, you may want to disable the speaker amplifier (drive PA_Ctrl LOW) to avoid simultaneous output. The board does not have automatic headphone detection in hardware, so this must be handled in software if desired.
I2S playback setup (ESP-IDF)
Section titled “I2S playback setup (ESP-IDF)”-
Add the ES8311 component to your project:
Terminal window idf.py add-dependency "espressif/es8311" -
Configure and initialize the I2S interface and codec:
#include "driver/i2s_std.h"#include "es8311.h"#define I2S_NUM I2S_NUM_0#define SAMPLE_RATE 44100#define PA_CTRL_GPIO GPIO_NUM_53// Enable the speaker amplifiergpio_set_direction(PA_CTRL_GPIO, GPIO_MODE_OUTPUT);gpio_set_level(PA_CTRL_GPIO, 1);// I2S standard mode configurationi2s_chan_handle_t tx_handle;i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM, I2S_ROLE_MASTER);i2s_new_channel(&chan_cfg, &tx_handle, NULL);i2s_std_config_t std_cfg = {.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(SAMPLE_RATE),.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO),.gpio_cfg = {.mclk = GPIO_NUM_13,.bclk = GPIO_NUM_12,.ws = GPIO_NUM_10,.dout = GPIO_NUM_11,.din = GPIO_NUM_9,},};i2s_channel_init_std_mode(tx_handle, &std_cfg);i2s_channel_enable(tx_handle); -
Initialize the ES8311 codec over I2C:
es8311_handle_t es8311 = es8311_create(I2C_NUM_0, ES8311_ADDR);es8311_init(es8311, &(es8311_codec_config_t){.sample_rate = SAMPLE_RATE,.bit_width = 16,});es8311_set_voice_volume(es8311, 70); // 0-100 -
Write audio data to the I2S channel:
size_t bytes_written;i2s_channel_write(tx_handle, audio_buffer, buffer_len,&bytes_written, portMAX_DELAY);
I2S recording setup (ESP-IDF)
Section titled “I2S recording setup (ESP-IDF)”Recording from the onboard microphone uses the same I2S bus in receive mode.
-
Create an I2S receive channel (can coexist with the transmit channel on the same I2S peripheral):
i2s_chan_handle_t rx_handle;i2s_new_channel(&chan_cfg, NULL, &rx_handle);i2s_channel_init_std_mode(rx_handle, &std_cfg);i2s_channel_enable(rx_handle); -
Configure the ES8311 for ADC (recording) mode:
es8311_set_mic_gain(es8311, ES8311_MIC_GAIN_42DB); -
Read audio samples:
uint8_t rec_buffer[1024];size_t bytes_read;i2s_channel_read(rx_handle, rec_buffer, sizeof(rec_buffer),&bytes_read, portMAX_DELAY);
Troubleshooting
Section titled “Troubleshooting”| Symptom | Cause | Fix |
|---|---|---|
| No speaker output | PA_Ctrl not enabled | Set GPIO53 HIGH before writing audio data |
| Distorted audio | Speaker impedance too low or volume too high | Use 8-ohm speaker; reduce es8311_set_voice_volume() |
| No microphone input | Mic gain too low | Increase gain via es8311_set_mic_gain() |
| Crackling / clicks | Buffer underrun | Increase I2S DMA buffer size or reduce sample rate |
| I2C init fails | Wrong I2C address or bus | ES8311 default address is 0x18; verify I2C bus number |
Further reading
Section titled “Further reading”- ES8311 datasheet
- ESP-IDF I2S driver documentation
- Waveshare demo package — audio playback and recording examples