Source: https://mokxi.com/libraries/esp32-i2s
Updated: 2026-11-02

Libraries / Communication
Arduino library · Communication

# ESP32 I2S: digital audio in from a microphone and out to an amplifier

I2S is the three-wire serial bus for digital audio: a bit clock (BCLK), a word select line (WS, also called LRCLK) that says left or right channel, and a data line. The ESP32 has I2S hardware that can sit on almost any GPIO, which is how projects read an INMP441 MEMS microphone or drive a MAX98357A class D amplifier straight into a speaker with no DAC in between.

Open the example in the editor See the calls

Built in on Mokxi: #include <ESP_I2S.h> and it builds, with nothing to install. Free, and no account needed.

## What I2S (ESP32 core and ESP-IDF) does

Arduino-ESP32 3.x provides I2SClass in ESP_I2S.h: set the pins, then begin() with the mode, sample rate, bit width and mono or stereo. Older core 2.x sketches use the I2S object from I2S.h, and many tutorials call the ESP-IDF driver in driver/i2s.h directly; the three are different APIs for the same peripheral, so copy code from a tutorial that matches your core version.

The usual surprise is the sample format. An INMP441 sends 24 bits left-justified in a 32-bit slot, and only on the channel its L/R pin selects, so a sketch reading 16-bit stereo gets silence or noise until the width and slot match the microphone.

Library Manager name | I2S (ESP32 core and ESP-IDF)

Author | Espressif

Include | #include <ESP_I2S.h>
#include <I2S.h>
#include <driver/i2s.h>
#include <driver/i2s_std.h>

Boards | ESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32

In the simulator | Runs, with a gap the note below names.

Parts it drives | I2S microphone, INMP441, I2S amplifier, MAX98357A

## Install it

It builds on ESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32.

In Mokxi it is built in: #include <ESP_I2S.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board it comes with the board package (Espressif), so the Arduino IDE has it as soon as the board is installed: there is nothing to add from the Library Manager.

ESP32-C3 · ESP32-C6 · Seeed XIAO ESP32-C3 · ESP32-C3 SuperMini · Seeed XIAO ESP32-C6 · ESP32 DevKit · ESP32-DevKitC V4 · LOLIN32 Lite · Wemos D1 R32 · Cheap Yellow Display · TTGO T-Display · ESP32-S3-DevKitC-1 · Seeed XIAO ESP32-S3 · Arduino Nano ESP32

## The calls you will use

In the order a sketch meets them.

### I2SClass i2s

The I2S port object from ESP_I2S.h, one per peripheral you use.

### i2s.setPins(26, 25, 22)

Sets BCLK, WS and the data out pin; a fourth argument is data in, for a microphone. Call it before begin().

### i2s.begin(I2S_MODE_STD, 16000, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO)

Starts standard (Philips) I2S at 16 kHz with 16-bit samples in stereo. Returns false if the settings or pins are rejected.

### i2s.write((uint8_t *)samples, sizeof(samples))

Sends a buffer of samples to the amplifier and returns how many bytes went out. Interleave left and right in stereo.

### i2s.readBytes((char *)buffer, sizeof(buffer))

Reads samples from the microphone into a buffer and returns the number of bytes read.

### i2s.end()

Stops the port and frees its pins.

## Example: ESP32 I2S sine player

On the ESP32 DevKit V1. It runs in the browser exactly as printed.

sketch.ino · ESP32 DevKit V1

Open it in the editor, wired and ready to run · ESP32 I2S sine player: the project page

## Common errors and how to fix them

### The microphone reads all zeros

Check that the INMP441’s L/R pin and the slot you read agree (L/R to GND is the left channel), that the data pin is set as the input in setPins(), and that you read 32-bit samples. A 16-bit read of a 32-bit slot often lands on the empty bits.

### The speaker plays loud static instead of the tone

The sample rate, bit width or byte order in the buffer does not match what begin() was told. For 16-bit stereo, write int16_t pairs, left then right, and pass the same rate you computed the wave for.

### 'I2SClass' does not name a type

ESP_I2S.h and I2SClass are Arduino-ESP32 3.x. On core 2.x use the I2S.h object or the ESP-IDF driver/i2s.h calls, or update the ESP32 core in the Boards Manager.

## Projects that use I2S (ESP32 core and ESP-IDF)

Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.

ESP32 I2S microphone level meter An INMP441 I2S microphone on an ESP32, read with the ESP-IDF I2S driver: the sound level in dBFS and a bar on the serial monitor.

## Keep going

### Parts

- I2S microphone, INMP441
- I2S amplifier, MAX98357A

### More communication libraries

- OneWire
- TinyGPS
- TinyGPSPlus
- MFRC522
- SoftwareSerial
- AltSoftSerial
- i2cdetect
- Keyboard

Every built-in library · Open the editor

## Try I2S (ESP32 core and ESP-IDF) without installing anything

Open the example, change the sketch, and keep your version in a free account.

Start building free

Open the editor
