# Microphone

Sound into a Kode Dot: a MEMS microphone on the codec's analogue input, read as samples over I2S, and the code to watch its level.

## What it is

The microphone is a **ZTS6211E**, a small analogue MEMS capsule on the
front of the board. It does not speak any bus of its own: its output goes
straight into the codec's microphone input, and the codec's ADC turns it
into samples the P4 reads over I2S. So a bus scan will never show a
microphone, and the codec's gain setting is the microphone's volume.

What you get is a mono stream of 16-bit samples at the codec's sample rate.
From there it is a level meter, a waveform on the screen, a recording to
the microSD card, or the input side of the USB sound card the Kode Dot can
present to a computer.

## How it is connected

```mermaid
flowchart LR
  mic["ZTS6211E"] -- "MIC1P / MIC1N" --> codec["ES8311 ADC"]
  codec -- "I2S, GPIO36" --> p4["ESP32-P4"]
```

*Analogue into the codec, samples out of it over I2S*

The capsule is powered from the 3.3 V rail with no gate of its own, and
wired to the codec's `MIC1P` and `MIC1N` pins. The codec sends samples to
the P4 on `GPIO36`, clocked by the same `GPIO33`, `GPIO34` and `GPIO37` the
speaker uses, and takes its gain over I2C at `0x18`.

## Specifications

| Thing | Value |
| --- | --- |
| Microphone | ZTS6211E, analogue MEMS, always powered |
| Path | Codec's mic input → ADC → I2S to the P4 on `GPIO36`. Mono, 16-bit |
| Gain | Set in the codec over I2C. Kode's validation echo runs at 18 dB |
| Sample rate | 48 kHz, the same clock as the speaker; the two directions share one I2S channel |
| Kode driver | `kode_audio_envelope`: one 50 ms window reduced to 32 peak values, 0–1000. Enough to draw a waveform, not enough to reconstruct the sound, on purpose |
| USB sound card | The P4 can present itself to a computer as a USB audio device, mono in and out, 96 kHz. It takes the USB-C port, so the serial console goes away while it runs |
| Validated | `Kode-Dot-Val/05-AUDIO`: echo on the board and recording through the USB sound card from a Mac |

## Gotchas

> **WARNING**
>
> **The USB sound card takes the port your console is on.** Presenting the
> Kode Dot as a USB audio device uses the USB-C connector, the same one that
> carries USB-Serial-JTAG. The serial console disappears the moment that
> firmware starts, so plan to re-flash by holding BOOT rather than by asking
> the running app to reboot.

> **NOTE**
>
> **Recording is the way to test the codec without the speaker.** The
> microphone path never reaches the amplifier, so it works with any C5
> firmware. If a recording comes through and the speaker is silent, the codec
> and the I2S link are fine and what is missing is the C5's enable.

## Apps that use it

On the marketplace, apps that put this part to work. Install one from
`start/first-app` and see it before you write a line.

- https://apps.kode.diy/app/3dc95a93-1b79-4bd2-bded-67b4d616bd9a
- https://apps.kode.diy/app/328951aa-5eb7-426c-8b29-1e3b0ac57c49
- https://apps.kode.diy/app/102d6357-d51f-4911-b75a-243c2c1a3093
- https://apps.kode.diy/app/b33fa9d4-e0b9-4522-b5cd-65c565e06d43
- https://apps.kode.diy/app/f11ba199-e040-4403-ada1-135c21f189c7
- https://apps.kode.diy/app/f2f10f07-aa43-4f5b-86ce-34bbb6f11925
- https://apps.kode.diy/app/8a8ba702-aba2-43ee-9ce2-23520afca103
- https://apps.kode.diy/app/b3c74f9b-6250-459b-835c-5e352546f535

## Example code

Read the microphone's level ten times a second and log it. Silence logs a
small number; tap the front of the Kode Dot and it jumps.

```c
/*
 * mic_level.c - 2026-09-18 - from Kode with love
 *
 * Watches the Kode Dot's microphone. Brings the board and the codec up, then
 * reads one 50 ms window of audio at a time as a 32-bin envelope and logs
 * the loudest bin, 0 to 1000. Silence logs a few tens; a tap on the case or
 * a word spoken at it jumps into the hundreds.
 */
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "kode.h"

/* Board, codec, then one envelope per loop. */
void app_main(void)
{
    ESP_ERROR_CHECK(kode_bsp_init());
    ESP_ERROR_CHECK(kode_audio_init());

    while (1) {
        uint16_t bins[KODE_AUDIO_ENVELOPE_BINS];
        ESP_ERROR_CHECK(kode_audio_envelope(bins, KODE_AUDIO_ENVELOPE_BINS));   /* blocks ~50 ms */

        uint16_t peak = 0;
        for (int i = 0; i < KODE_AUDIO_ENVELOPE_BINS; i++) {
            if (bins[i] > peak) peak = bins[i];
        }
        ESP_LOGI("mic", "level %4u / 1000", peak);
        vTaskDelay(pdMS_TO_TICKS(50));
    }
}
```

**TBD**: compiled against the `kode` component; a run on a V1 board.

## In the SDK

- [`kdrv_audio`](https://github.com/kodediy/kode-sdk/tree/main/components/kdrv/kdrv_audio): the ES8311 driver in `kode-sdk`, both directions.
