Lesson 11: Automatic Playback After Recording¶
Compatible development board: CrowPanel Advanced 7 / 9 / 10.1-inch ESP32-P4 HMI AI Display Development Board
Model compatibility: The 7-inch, 9-inch, and 10.1-inch models are fully interchangeable in terms of hardware interfaces and software code; only their physical sizes differ. Please select a model based on your actual display size and use case; this lesson requires no code modifications.
1. Course Introduction¶
Record 5 seconds of audio using the onboard PDM microphone, process it at 16 kHz / 16-bit / mono, and then play it back through the I2S speaker.
2. Learning Objectives¶
Upon completing this lesson, you should be able to:
- Understand the initialization of audio GPIO, PDM input, and I2S output.
- Understand the WAV header, peak detection, and safe gain in
recording(). - Complete a 5-second recording and automatic playback.
3. Preparation¶
- Hardware: One CrowPanel Advanced 7 / 9 / 10.1-inch ESP32-P4 HMI AI Display development board (with onboard PDM microphone, I2S audio output, amplifier, and speaker), and one USB Type-C data cable that supports data transfer.
- The audio parameters are 16 kHz, 16-bit, mono.
- The onboard microphone and speaker are already wired; however, you still need to upload code and supply power via the UART0 USB connection.
Code reference link: https://github.com/Elecrow-RD/CrowPanel-Advanced-7inch-ESP32-P4-HMI-AI-Display-1024x600-IPS-Touch-Screen/tree/master/example
4. Software Operation Steps¶
Double-click to open the Lesson 11 code. (.ino file)
Microphone and speaker related pin descriptions
Configure the following options accordingly.
- Board:
ESP32P4 Dev Module - Core Debug Level:
Info - Flash Frequency / Mode / Size:
80MHz/QIO/16MB (128Mb) - Partition Scheme:
16M Flash (3MB APP/9.9MB FATFS) - PSRAM:
Enabled - USB Mode:
Hardware CDC and JTAG - Port: After connecting the USB data cable to the board's UART0, select the newly appeared COM port under "Tools → Port".
Follow the library import steps explained in detail in Lesson 1 to import the library files required by this project into the development environment, ensuring that the code can correctly locate the relevant dependencies during compilation, thereby guaranteeing normal program operation.
5. Hardware Operation Steps¶
Now that the code is ready, we need to flash the ESP32-P4 to see the results in action.
First, connect the Advance-P4 device to your computer host via a USB cable.
The onboard microphone and speaker are already wired;
Before uploading the code, first select the upload configuration by following "4. Software Operation Steps".
Connect UART0, compile and upload using the general configuration; keep the board powered on.
After recording for 5 seconds, the speaker should play the just-recorded audio.
6. Key Code Explanation¶
6.1 Recording and Safe Gain¶
origin_buffer = i2s_mic.recordWAV(5, &read_bytes);
read_bytes -= 44;
float safe_gain = 32767.0f / max_val;
float final_gain = min(desired_amplification, safe_gain);
The first 44 bytes form the WAV header; the code iterates over the 16-bit samples to find the maximum absolute value and uses safe_gain to prevent overflow after multiplication gain, then calls i2s_spk.write() to output.
6.2 I2S Initialization¶
i2s_mic.setPinsPdmRx(MIC_GPIO_CLK, MIC_GPIO_SDIN);
i2s_mic.begin(I2S_MODE_PDM_RX, 16000, 16, mono);
i2s_spk.setPins(AUDIO_GPIO_BCLK, AUDIO_GPIO_LRCLK, AUDIO_GPIO_SDATA);
i2s_spk.begin(I2S_MODE_STD, 16000, 16, mono);
mic_loudspeaker_init() first turns off the audio power, then sets the PDM input and I2S output pins, and starts both ends at the same sample rate and bit width.
7. Experimental Observations¶
- After the device records for about 5 seconds, the speaker automatically plays the just-recorded sound; the serial port shows the recording and playback status.
- Open the serial monitor, and you can see when recording starts and when sound is played.
(For how to open the serial monitor, please refer to Lesson 1, where we provide a detailed explanation.)
8. Common Issues and Troubleshooting¶
- If there is no sound, check AUDIO_GPIO_CTRL, the power supply, and the I2S pins.
- If the sound is distorted, lower
desired_amplification; if the duration is abnormal, checkread_bytesand the WAV header handling.








