Kode Dot
Microphone
Record sound and measure its level with the Kode Dot's microphone.
What it is
Your app records with the microphone to measure how loud something is, draw the
waveform or save the sound to the microSD card. For that it uses
kode_audio_record_samples, which leaves the samples in a buffer your app provides,
or kode_audio_record_file, which saves them to a WAV. The microphone is a small analogue
MEMS capsule, the ZTS6211E.

Each recording lasts 30 s at most, and the speaker cannot play while you record.
How it is connected
The microphone goes to the audio codec, the ES8311 that Speaker explains. The codec turns its sound into samples and passes them to the P4 over I2S, on GPIO33 and with the same clock as the speaker. Its settings, such as the gain, reach it over the shared I2C bus. The microphone has no address of its own, so it does not show up when you scan the I2C bus.

The full circuit is in the main board schematic.
Specifications
kode_audio_set and "mic_gain", in 6 dB steps, until the next reset.kode_audio_record_file saves the WAV in sd/... or in flash/..., inside a folder that already exists.kode_audio_record_start records while your app goes on. To check on it, stop it and save it, see the SDK API.The calls to record are kode_audio_record_samples,
kode_audio_record_file and kode_audio_record_start, in kode.h.
Gotchas
kode_audio_play_stop.ms you ask for or when the buffer is full, whichever comes first. At the end, the codec may also deliver a few fewer frames. Use the count it returns in frames.Apps that use it
RecorderVoice notes in your pocket. Tap Record, talk, tap Stop: every take goes to the microSD as a WAV you can copy to any computer. - Swipe or use the pad to switch between Record and your Recordings - Tap a recording to play it back, with the progress running around the screen - Up to 2 minutes per take Needs a microSD card in the Dot.
IntercomA walkie-talkie between two Dots. No router and no internet: one Dot opens the line, the other joins it, and you talk. - Hold TOP to talk, let go to listen - A double beep when the other Dot is on the line - Pad up/down sets the volume Needs two Dots with Intercom.Example code
Run the program and, while it is quiet, you see a small number that goes up as soon as you speak or tap the case. That number, from 0 to 32767, comes out every half second and is the highest sample the program heard in that time, so it tells how loud the sound is. The first line can come out high, from a pop the codec makes as it starts to record (kode-sdk#286).
/* SPDX-License-Identifier: GPL-3.0-or-later */
/**
* @file mic_level.c
* @brief Prints how loud the room is, twice a second.
*
* Each line is the largest sample of the last half second, from 0 to 32767.
* Speak or tap the front of the case and the number jumps.
*
* from Kode with love
*/
#include "kode.h"
#define MIC_WINDOW_FRAMES 4800U /**< Samples in one window: 100 ms at 48 kHz. */
#define MIC_WINDOW_MS 100U /**< Length of one window. */
#define MIC_WINDOWS 5U /**< Windows per line: one line every half second. */
#define MIC_RETRY_MS 1000U /**< Wait after a failed recording. */
/** @brief One window of samples. */
static int16_t s_buf[MIC_WINDOW_FRAMES];
/** @brief Record five windows, keep the loudest sample, print it, repeat. */
void app_main(void)
{
kode_init();
kode_print("Running: speak or tap the case");
while ( true )
{
int32_t peak = 0;
bool ok = true;
for ( uint32_t w = 0U; (MIC_WINDOWS > w) && ok; ++w )
{
uint32_t got = 0U;
ok = kode_audio_record_samples(s_buf, MIC_WINDOW_FRAMES, MIC_WINDOW_MS, &got);
for ( uint32_t i = 0U; ok && (i < got); ++i )
{
int32_t level = s_buf[i];
if ( 0 > level )
{
level = -level;
}
if ( level > peak )
{
peak = level;
}
}
}
if ( !ok )
{
kode_print("no recording: %s", kode_error());
kode_delay(MIC_RETRY_MS);
continue;
}
kode_print("level %d / 32767", (int)peak);
}
}If you would rather build it yourself, follow the steps shared by every example.
In the SDK
kdrv/audio: the ES8311 driver inkode-sdk, both directions, described in the ES8311 driver.examples/audio: an SDK example for sound out and in.