In the previous project, we built an ESP32+INMP441 microphone visualizer, a web page hosted on ESP32 that displays the microphone’s live audio waveform in a browser. It helped us confirm the microphone was working properly, but it only displayed the audio. Digital audio becomes truly useful once it’s stored, streamed, and edited, and that’s what this project adds.
This project is a complete, cross-platform audio recorder built around the same two inexpensive parts (an ESP32 development board and an INMP441 I2S MEMS microphone). The microphone captures sound, ESP32 digitizes it, and the audio travels over an ordinary USB cable to a desktop application that saves it as a standard WAV file. It’s the same cable used to program the board.
The system has two halves that communicate over the USB serial link:
- An ESP32 sketch (esp32_inmp441_usb_pcm.ino) that reads the microphone over I2S, streams 16-bit PCM audio to the computer in small packets, and responds to run, pause, and stop commands.
- 2. A Python desktop application (esp32_pcm_recorder.py) that receives the stream, buffers it during recording, and writes it to disk. Its minimal interface has three recording controls (Start, Pause/Resume, and Finish) and prompts for a save location when the recording ends.
The design keeps the hardware and software simple. There’s no USB Audio Class device, sound card, or special driver, only a plain serial port carrying a well-defined stream of audio samples. On the computer side, the application uses Tkinter and pyserial, so it runs unchanged on Windows, Linux, and macOS. The result is a practical tool for capturing voice notes or sound clips, as well as a clear example of how to move real-time audio from a microcontroller to a PC.
Recordings are saved in WAV format, which is lossless, so the PCM audio data is stored without any loss in quality.
Components required
- ESP32 development board x1
- INMP441 I2S microphone module x1
- Jumper wires
- USB cable + computer for flashing and the Serial Monitor
- A 2.4 GHz WiFi network (ESP32’s WiFi is 2.4 GHz only)
Circuit connections
This project uses a classic ESP32-WROOM DevKit with the pinout shown below, though ESP32-S3 also works.
There are six connections with no extra parts.
Keep the jumper wires short. BCLK runs near 1 MHz, and long leads raise the noise floor, which shows up as noise in the recording even in a quiet room. Don’t leave L/R floating. Tying it to GND puts the mic on the left slot, which is the slot the sketch reads. The INMP441’s sound port is on the bottom of the board, so mount it without blocking that hole.
The USB cable between ESP32 and the computer does double duty, powering the board and carrying the audio stream. No separate power supply is required.
On the software side, you’ll need the Arduino IDE with the ESP32 board package (version 3.0.0 or newer) and Python 3.8 or later with the pyserial library installed. No additional Arduino libraries are required, since the sketch only uses what ships with the ESP32 core.
How it works
When you plug ESP32 into the computer, the board appears as a standard serial port (the same port used to upload sketches). The application opens that port, and from then on, the two sides exchange two kinds of information.
Control commands flow from the PC to ESP32 as single characters. The application sends R to run or resume streaming, P to pause, and S to stop. ESP32 reads these characters and switches its behavior accordingly.
Audio flows from ESP32 to the PC as a continuous series of small binary packets called frames. Each frame begins with a four-byte marker, A1S2, followed by a count of the samples it contains and then the audio samples themselves as 16-bit numbers. The marker lets the application recognize where each frame begins. If a stray byte is dropped on the USB link, the application scans forward, finds the next marker, and carries on, so a momentary glitch can’t corrupt the rest of the recording.
The audio itself is 16-bit mono at a 16-kHz sample rate. That format is more than adequate for voice and general sound capture, and it keeps the data rate modest. At 16,000 samples per second and two bytes per sample, the stream needs about 32 kilobytes per second. The serial link runs at 921,600 baud, which carries that comfortably with room to spare.
On the computer, a background thread continuously reads bytes from the serial port, locates the frame markers, and extracts the samples. While the application is recording, it appends those samples to a buffer in memory. When it’s paused or stopped, it simply ignores them. When you press Finish, the application stops the stream and writes the buffered samples to a WAV file, adding the small header audio players need to interpret the raw samples correctly.
Because the control path and the audio path share one serial connection, the design stays simple, with a single cable, a single port, and a single stream.
ESP32 sketch
The sketch reads the microphone and sends audio when the desktop app asks for it. It’s built on the new ESP-IDF I2S channel driver, which is why it requires Arduino-ESP32 core 3.0.0 or newer.
Configuration and globals
At the top of the file, a set of constants defines everything that must match the Python side, including the serial baud rate (921600), the sample rate (16000), and the number of samples per frame, FRAME_SAMPLES (256). Three more constants name the GPIO pins for the I2S clock, word-select, and data lines.
The OUT_SHIFT constant sets the microphone gain. The INMP441 produces 24-bit samples, but the project transmits 16-bit samples, so each value must be scaled down, and OUT_SHIFT controls by how much. A value of 8 is unity (no gain), and smaller values make the audio louder. The default of 4 applies roughly sixteen-fold gain, which suits normal speech. If recordings come out too quiet, lower this number. If loud sounds distort, raise it.
A handful of global buffers follow. The rawFrames buffer holds the raw 32-bit samples read from I2S, outBuf holds the processed 16-bit samples ready to send, and MAGIC holds the four marker bytes. A single Boolean, streaming, tracks whether the board should currently be sending audio.
Initializing the microphone
The i2sMicInit() function sets up the I2S peripheral in three steps. First, it allocates a receive channel on I2S port 0, with ESP32 as the clock master. Next, it configures that channel for the standard (Philips) I2S format. Finally, it enables the channel, which starts the clocks and the direct memory access (DMA) engine that moves samples into memory.
Two configuration choices are specific to the INMP441. The slots are set to 32 bits wide in stereo framing because the microphone expects 64 bit-clock pulses per frame. The master clock pin is marked unused because this microphone generates its own internal clocks and doesn’t need a master clock signal. The code only creates a receive channel, since the board never transmits audio.
Reacting to commands
Inside loop(), the first task is checking for incoming commands. The code reads any available bytes from the serial port and interprets them. The R command sets streaming to true, while the P and S commands both set it to false. When streaming is false, the loop waits briefly and returns, so the board sits idle until the application tells it to run.
Reading and processing audio
When streaming is active, the sketch reads one block of audio from the I2S driver into rawFrames. Because the microphone is configured for stereo framing, the samples arrive interleaved as left-right pairs. The project only uses the left channel, since the L/R pin is grounded.
Each raw sample is a 32-bit word, with the microphone’s 24 valid bits in the upper portion. The code first shifts each sample right by 8 to recover a signed 24-bit value. It then sums these values to compute the block’s average, which represents the microphone’s dc offset, a small constant bias that’s present even in silence. Subtracting that average from every sample removes the offset, which centers the waveform and frees up headroom.
Next, the code applies gain and converts the samples to 16 bits. It shifts each dc-corrected sample right by OUT_SHIFT and clamps the result to the valid 16-bit range of −32768 to 32767, so loud sounds saturate cleanly instead of wrapping around into noise. The finished 16-bit samples go into outBuf.
Sending a frame
Finally, the sketch sends the frame. It writes the four marker bytes, the two-byte sample count, and then the block of 16-bit samples. ESP32 stores multi-byte numbers in little-endian order, which is exactly what the Python side expects, so the samples go out ready to use. The loop then repeats, reading and sending the next block. Because the I2S read waits until the samples are ready, the loop naturally paces itself to the audio rate, sending frames as fast as the microphone produces them and no faster.
That’s the entire firmware. It configures the microphone, listens for commands, and when told to run, reads the audio, cleans it up, and streams it out one frame at a time.
Python script
How the desktop Python app works
The cross-platform application that records and stores PCM audio on the computer is a Python script. It has two cooperating parts. A Recorder class handles the serial connection and audio buffer on a background thread, and an App class builds the window and responds to button presses. Keeping the serial work off the main thread lets the interface stay responsive while audio streams in continuously.
At the top of the file, a block of constants repeats the firmware settings, including the marker bytes, frame size, sample rate, channel count, sample width, and default baud rate. These values must be identical in both files, or the audio won’t be interpreted correctly.
The Recorder class
The Recorder holds all the state the background thread and the interface share. That includes the open serial port, the recording state (idle, recording, paused, or stopped), the list of captured audio frames, a running sample count, a flag that tracks whether the latest audio has been saved, the latest signal level for the meter, and any error message raised by the thread. A lock protects the pieces both threads access.
Connecting opens the serial port, clears any stale input, and sends an S command so the board starts in a known, stopped state. It then launches the background reader thread. Disconnecting reverses this process. It signals the thread to stop, waits for it to finish, tells the board to stop streaming, and closes the port.
The recording controls are small methods that change the state and send the matching command. The start() method clears the buffer, marks the recording as unsaved, sets the state to recording, and sends R. The pause_resume() method toggles between recording and paused, sending P or R as needed. The finish() method sets the state to stopped and sends S. Each method makes its change under the lock, so the reader thread always sees consistent values.
The background reader does most of the work. In a loop, it reads whatever bytes are available from the serial port, appends them to an internal buffer, and calls the parser. The parser repeatedly looks for a complete frame. It checks whether the buffer begins with the marker, and if it doesn’t, it searches forward for the next marker and discards the bytes before it. Once aligned, it reads the sample count, verifies that it matches the expected frame size, waits until the full payload arrives, and extracts the samples. This search-and-verify approach makes the stream self-correcting. If the parser ever loses alignment, it simply realigns on the next marker.
For each decoded frame, the reader does two things. First, it computes the peak amplitude and stores it as a level value for the meter. This happens whether or not a recording is in progress, so the meter responds to sound at all times. Second, if the state is recording, it appends the frame’s samples to the buffer and advances the sample count. Because the state check controls this step, pausing simply stops new audio from being stored, and the recording picks up cleanly when you resume.
Building the window with the App class
The App class builds a compact window with three groups. The Connection group holds a dropdown of available serial ports, a Refresh button, a baud-rate selector, and a Connect button. The Status group shows the current state, a timer for the recording length, and a level bar. The controls row holds the three recording buttons (Start, Pause, and Finish).
The app populates the port dropdown by querying the system for serial ports and listing each one with its description, which helps identify the board among other devices. Clicking Refresh repeats the query, so a board plugged in after the app opens will appear.
Connecting and recording from the interface
Pressing Connect opens the selected port and changes the button to Disconnect. Pressing it again closes the connection, with a confirmation prompt if a recording is in progress or unsaved. While connected, the port and baud selectors are disabled so they can’t be changed mid-session.
Start begins a new recording. If an unsaved recording already exists, the app asks for confirmation before discarding it, so you don’t lose work by accident. Pause toggles capture on and off, and the button changes to Resume while paused. Finish ends the recording and immediately opens a save dialog with a timestamped default filename, such as recording_20261005_193000.wav. If you choose a location, the application writes the file. If you cancel, it keeps the recording and lets you know, so you can try again instead of losing the audio.
Writing the WAV file
Saving uses Python’s built-in wave module. The application opens the chosen file, sets the format (one channel, two bytes per sample, and a 16-kHz frame rate), and writes the joined audio buffer. Those three settings turn the raw samples into a file any audio player can open correctly. After a successful save, the app clears the buffer and returns to its ready state for the next recording.
Keeping the display live
A short timer method runs several times a second. It updates the level bar with the latest peak value, refreshes the state label and length timer, and enables or disables the three buttons based on the current state. For example, Start is only available when the app is connected and not already recording, while Finish stays available whenever there’s captured audio to save, even after disconnecting. The same method watches for errors reported by the background thread, and if one appears, it displays the error and disconnects cleanly. This single periodic update keeps the window in sync with the app’s actual state.
Startup and safety
The program’s entry point first checks that pyserial is installed and shows a helpful message if it isn’t. It then creates the window and starts the interface. The app also protects against closing by mistake. If there’s an unsaved recording, it asks for confirmation before quitting, and it always disconnects cleanly to release the serial port.
Results
Video review
Below is a sample WAV recording made with the ESP32+INMP441 microphone setup, using the project’s ESP32 sketch as the firmware and its Python script as the desktop app.
Conclusion
This project demonstrates a clean, dependable way to move real-time audio from a microcontroller to a computer using only a USB cable and a well-defined serial protocol. ESP32 handles the time-critical work of reading the INMP441 over I2S, cleaning up the samples, and streaming them out in framed packets. The Python application handles the interactive side, including connecting, controlling the recording, buffering the audio, and writing a standard WAV file. Splitting the work this way keeps each side small and easy to understand.
A few design choices make the recorder practical. The framed protocol and its marker bytes let the receiver recover from dropped data, so recordings stay intact. The format and protocol constants are defined once at the top of each file and clearly marked, so changing the sample rate or gain only takes editing one or two lines on each side. Because the application only relies on Tkinter and pyserial, it also runs on Windows, Linux, and macOS without modification.
The result is a useful tool and a solid reference for anyone learning to connect embedded audio hardware to desktop software.
You may also like:
Filed Under: Tutorials, Video











Log in to leave a comment:
Lost your password?
Don't have an account? Register here