Skip to content

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.

I2S Bus
ESP32-P4 ────────────────── ES8311 Codec
┌──────────┼──────────┐
│ │ │
Speaker Headphone Microphone
(NS4150B) (3.5mm) (SMD)
Amplifier Jack Onboard
ComponentDetails
Audio codecES8311 — 24-bit, 8—96 kHz sample rates, I2C control + I2S data
Power amplifierNS4150B — 3W mono, Class D, drives the speaker connector
Speaker connectorMX1.25 2-pin, rated for 8 ohm 2W speakers
Headphone output3.5mm stereo jack
MicrophoneSMD MEMS microphone, soldered on-board

The audio codec connects to the ESP32-P4 via I2S with the following GPIO assignments:

SignalGPIODirectionDescription
MCLKGPIO13OutputMaster clock to codec
SCLK (BCLK)GPIO12OutputBit clock
ASDOUTGPIO11OutputAudio data out (ESP32-P4 to codec)
LRCK (WS)GPIO10OutputLeft/right word select
DSDINGPIO9InputAudio data in (codec to ESP32-P4)
PA_CtrlGPIO53OutputPower amplifier enable (HIGH = on)
  1. Locate the MX1.25 2-pin speaker connector on the board (labeled SPK or SPEAKER on the silkscreen).

  2. Connect an 8-ohm speaker rated at 2W or less. The connector accepts a standard MX1.25 plug.

  3. Ensure correct polarity if your speaker is marked — reversed polarity will not damage the amplifier but will invert the audio phase.

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.

  1. Add the ES8311 component to your project:

    Terminal window
    idf.py add-dependency "espressif/es8311"
  2. 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 amplifier
    gpio_set_direction(PA_CTRL_GPIO, GPIO_MODE_OUTPUT);
    gpio_set_level(PA_CTRL_GPIO, 1);
    // I2S standard mode configuration
    i2s_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);
  3. 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
  4. Write audio data to the I2S channel:

    size_t bytes_written;
    i2s_channel_write(tx_handle, audio_buffer, buffer_len,
    &bytes_written, portMAX_DELAY);

Recording from the onboard microphone uses the same I2S bus in receive mode.

  1. 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);
  2. Configure the ES8311 for ADC (recording) mode:

    es8311_set_mic_gain(es8311, ES8311_MIC_GAIN_42DB);
  3. 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);
SymptomCauseFix
No speaker outputPA_Ctrl not enabledSet GPIO53 HIGH before writing audio data
Distorted audioSpeaker impedance too low or volume too highUse 8-ohm speaker; reduce es8311_set_voice_volume()
No microphone inputMic gain too lowIncrease gain via es8311_set_mic_gain()
Crackling / clicksBuffer underrunIncrease I2S DMA buffer size or reduce sample rate
I2C init failsWrong I2C address or busES8311 default address is 0x18; verify I2C bus number