Skip to content

Microphone

Class: Mic_Class (from M5Unified)
Access: M5Cardputer.Mic

The microphone provides I2S-based audio recording with configurable sample rate and bit depth.


Lifecycle

bool begin();       // Initialize I2S and start background recording task
void end();         // Stop and release I2S
bool isRunning();   // true if background task is active
bool isEnabled();   // true if mic pin is configured

Recording

8-bit Unsigned

bool record(uint8_t* rec_data, size_t array_len);
bool record(uint8_t* rec_data, size_t array_len, uint32_t sample_rate, bool stereo = false);

16-bit Signed

bool record(int16_t* rec_data, size_t array_len);
bool record(int16_t* rec_data, size_t array_len, uint32_t sample_rate, bool stereo = false);
Parameter Type Default Description
rec_data uint8_t* or int16_t* Buffer to receive audio samples
array_len size_t Number of samples to record
sample_rate uint32_t from config Sample rate in Hz (default: 16000)
stereo bool false Stereo mode

Returns true if recording was queued successfully.


Configuration

void setSampleRate(uint32_t sample_rate);   // Change recording sample rate (Hz)

The sample rate can also be set via m5::Mic_Class::config_t during begin(), but setSampleRate() is the simplest way to adjust before calling record().


Recorder State

size_t isRecording() const;   // 0=idle, 1=recording (queue has room), 2=recording (queue full)

Quick Example

// Record 4096 samples at 8kHz
const int samples = 4096;
uint8_t buffer[samples];

M5Cardputer.Mic.setSampleRate(8000);
M5Cardputer.Mic.record(buffer, samples);

// Wait for recording to complete
while (M5Cardputer.Mic.isRecording()) {
    M5Cardputer.update();
    delay(1);
}

// Process recorded samples
for (int i = 0; i < samples; i++) {
    // buffer[i] contains 0-255 audio amplitude
    int32_t y = map(buffer[i], 0, 255, 240, 0);  // Map to screen coordinates
    M5Cardputer.Display.drawPixel(i / 4, y, TFT_GREEN);
}

Blocking Note

record() is non-blocking — it queues the request to the background I2S task. Use isRecording() to poll for completion.

Recording to WAV on SD Card

See the mic_wav_record example for a complete WAV recording pipeline with SD card support.