← How-Tos
electronics 2 days ago ◑ 4 views ◯ 6 min read

MEMS Microphone Arrays on ESP32: Beamforming and Direction-of-Arrival for Sound-Triggered Projects

esp32mems microphonebeamformingdirection of arrivalgcc-phati2sinmp441audio dsp

Most maker-focused ESP32 audio projects stop at a single microphone: one INMP441 or SPH0645 wired up for voice capture or simple sound triggering. A single mic tells you that a sound happened, but it can't tell you where it came from. Add two, three, or four MEMS microphones in a known physical arrangement and the ESP32's two cores have enough headroom to run basic beamforming and direction-of-arrival (DoA) estimation — enough to build a shop safety trigger that knows which direction a bang came from, a sound-following camera mount, or a crude voice-tracking assistant that always points toward whoever is talking. This guide covers the array hardware, the I2S wiring and timing constraints that make multi-mic capture work, and the DSP techniques that turn four correlated audio streams into a bearing.

Why Arrays Instead of One Mic

A single omnidirectional MEMS mic captures amplitude and frequency content but has no spatial information. With two or more mics at known positions, the tiny time-of-arrival difference between channels for a sound coming from one direction versus another becomes measurable, and that time difference (on the order of microseconds to tens of microseconds for mic spacings of a few centimeters) is enough to estimate a bearing using cross-correlation or more advanced beamforming math. This is the same underlying principle used in smart speakers and hearing-aid beamforming, just at a much smaller computational budget.

Choosing Microphones and Array Geometry

Two practical mic types work well with ESP32:

For geometry, a simple square or linear array of 4 mics spaced 3-6 cm apart gives a reasonable starting point: close enough that the sound arrives within the array's spatial aliasing limit for the frequencies you care about (voice or shop impact sounds, roughly 300 Hz-4 kHz), far enough apart that the inter-mic time delay is still resolvable at your sample rate. A square array (one mic per corner) extends simple 1-axis time-difference bearing into a full 2D or rough 3D direction estimate; a linear array only resolves a front/back-ambiguous line.

Wiring Multiple I2S Mics to One ESP32

The ESP32's hardware I2S peripheral count is limited (most variants expose two I2S controllers, and one is often reserved if you're also driving a DAC or SD card), so a 4-mic array is usually built one of two ways:

  1. TDM-style sharing on a single I2S bus: Many I2S MEMS mics support a hardware L/R select pin, letting you put two mics on left and right channels of one I2S data line, with WS/SCK shared. Some mic breakout combinations (like paired INMP441 modules) support stacking this way for 2 channels per bus.
  2. Multiple ESP32 cores or chips with a shared sample clock: For a true 4+ channel array, many DIY builds use a single ESP32-S3 (which has more flexible I2S/LCD-camera peripheral routing usable for parallel sampling) or distribute mics across two I2S peripherals and synchronize buffers in software using a shared hardware timer trigger.

Whichever wiring you choose, the critical requirement is a shared bit clock and word-select line across all mics in the array. If each mic runs on its own independent I2S clock domain, your inter-channel timing drifts and DoA estimation falls apart; a shared SCK/WS bus, fed from a single master, is what keeps samples time-aligned.

Direction-of-Arrival: GCC-PHAT Cross-Correlation

The generalized cross-correlation with phase transform (GCC-PHAT) algorithm is the practical choice for an ESP32-class DSP budget. In broad strokes:

  1. Capture a synchronized frame (e.g., 1024 samples) from each microphone pair.
  2. Compute the cross-power spectrum between the pair using FFT (the esp-dsp library's real FFT routines are fast enough for this at typical audio sample rates on an ESP32-S3).
  3. Normalize by magnitude (the "phase transform" part of GCC-PHAT) to suppress the effect of reverberant room reflections, which otherwise badly confuse simple cross-correlation in a typical shop environment full of hard, reflective surfaces.
  4. Inverse-FFT the result back to the time domain; the peak location in this cross-correlation function gives the time delay between the two mics for the dominant sound source.
  5. Convert time delay to an angle using the known mic spacing and the speed of sound, then combine delay estimates from multiple mic pairs in the array to triangulate a 2D bearing.

This is computationally realistic on an ESP32-S3 for frame sizes in the 512-2048 sample range at a reasonable update rate (several estimates per second), which is plenty for a direction-aware trigger or slow camera-pointing application. It is not fast or accurate enough to compete with a dedicated beamforming DSP chip for real-time multi-speaker separation, and background shop noise (CNC spindles, dust collection, compressors) will degrade accuracy — test in your actual noise environment before committing to a design.

Practical Project Ideas

Calibration and Pitfalls

A microphone array on an ESP32 won't rival a commercial smart-speaker's beamforming, but it is a genuinely capable, inexpensive way to add spatial awareness to a sound-triggered project, and the GCC-PHAT math involved is a good, self-contained introduction to practical array signal processing on embedded hardware.