pdf-icon

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

AtomS3U Mic

AtomS3U microphone example.

Example

Compilation Requirements

  • M5Stack Board Manager version >= 3.2.6
  • Development board option = M5AtomS3
  • M5Unified library version >= 0.2.13
  • FastLED library version >= 3.5.0
Note
Adjust the NOISE_FLOOR and MAX_LEVEL values according to your environment to obtain better audio responsiveness.
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#include <M5Unified.h>
#include <FastLED.h>

#define PIN_LED     35          // Onboard RGB LED pin
#define NUM_LEDS    1           // Number of LEDs (onboard = 1)

static constexpr size_t record_length     = 320;   // Number of samples per audio frame

CRGB leds[NUM_LEDS] = {CRGB::Green};  // LED array, initialized to green

static int16_t rec_data[record_length]; // Audio sample buffer

// ================= Tunable parameters =================
#define NOISE_FLOOR   300    // Noise floor threshold (environment-dependent)
#define MAX_LEVEL     5000   // Maximum expected audio level

uint8_t hue = 0;             // Current HSV hue (0–255, wraps automatically)

void setup() {
    auto cfg = M5.config();
    cfg.internal_mic = true;     // Enable onboard microphone
    M5.begin(cfg);

    Serial.begin(115200);
    Serial.println("AtomS3U Sound Reactive RGB LED");

    // Start microphone
    M5.Mic.begin();

    // ================= LED initialization =================
    FastLED.addLeds<SK6812, PIN_LED, GRB>(leds, NUM_LEDS); 
    FastLED.setBrightness(255);                            
    FastLED.show();                                  
    delay(500); 
}

void loop() {
    M5.update();
;
    
    if (!M5.Mic.isEnabled()) {
        leds[0] = CRGB::Red;// Show red LED as error/status indicator
        FastLED.show();
        return;
    }

    // Record one frame of audio samples
    if (M5.Mic.record(rec_data, record_length)) {

        // ================= Audio level calculation =================
        uint32_t sum = 0;
        for (size_t i = 0; i < record_length; i++) {
            sum += abs(rec_data[i]); // Accumulate absolute sample values
        }

        // Calculate average audio amplitude
        uint32_t level = sum / record_length;

        // Apply noise floor (prevent very low values)
        if (level < NOISE_FLOOR) level = NOISE_FLOOR;

        // ================= Audio-to-LED mapping =================
        // Map audio level to LED brightness (1–255)
        uint8_t brightness = map(level, NOISE_FLOOR, MAX_LEVEL, 1, 255);
        brightness = constrain(brightness, 1, 255);

        // Increase hue based on audio level (louder sound = faster color change)
        hue += map(level, NOISE_FLOOR, MAX_LEVEL, 1, 5);

        // Set LED color using HSV model
        leds[0] = CHSV(hue, 255, brightness);

        // Update LED output
        FastLED.show();

        // Debug output
        Serial.printf("Level:%d Bright:%d Hue:%d\n", level, brightness, hue);
    }
}

When powered on, the RGB LED will change color and brightness according to ambient sound level. Louder sounds make the LED brighter and cycle colors faster; quieter sounds result in dimmer, slower color changes. Test in an environment with sufficient sound to observe the effect.

Driver Library

API

The Mic of the AtomS3U uses the Mic_Class from the M5Unified library. For more related APIs, refer to the following documentation:

On This Page