Chain Mic

MicChain is the helper class for microphone devices on the Chain bus. It provides methods to read ADC values, configure threshold values, set trigger cycle, and monitor microphone trigger events.

Support the following products:

Chain MIC

UiFlow2 Example

Microphone sound detection

Open the m5core_chain_mic_basic_example.m5f2 project in UiFlow2.

This example demonstrates how to read ADC values from the Chain Mic sensor and monitor sound detection.

UiFlow2 Code Block:

m5core_chain_mic_basic_example.png

Example output:

None

MicroPython Example

Microphone sound detection

This example demonstrates how to read ADC values from the Chain Mic sensor and monitor sound detection.

MicroPython Code Block:

 1# SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from chain import MicChain
 9import time
10from chain import ChainBus
11
12
13title0 = None
14label_adc = None
15label_status = None
16bus2 = None
17chain_mic_0 = None
18last_trigger_time = None
19
20
21def chain_mic_0_low_to_high_event(args):
22    global title0, label_adc, label_status, bus2, chain_mic_0, last_trigger_time
23    last_trigger_time = time.ticks_ms()
24    label_status.setVisible(True)
25
26
27def chain_mic_0_high_to_low_event(args):
28    global title0, label_adc, label_status, bus2, chain_mic_0, last_trigger_time
29    last_trigger_time = time.ticks_ms()
30    label_status.setVisible(True)
31
32
33def setup():
34    global title0, label_adc, label_status, bus2, chain_mic_0, last_trigger_time
35
36    M5.begin()
37    Widgets.setRotation(1)
38    Widgets.fillScreen(0x000000)
39    title0 = Widgets.Title("Chain MIC Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu24)
40    label_adc = Widgets.Label("ADC: --", 95, 76, 1.0, 0xFFFFFF, 0x000000, Widgets.FONTS.DejaVu24)
41    label_status = Widgets.Label(
42        "voice trigger", 82, 158, 1.0, 0x4CEB18, 0x000000, Widgets.FONTS.DejaVu24
43    )
44    bus2 = ChainBus(2, tx=21, rx=22)
45    chain_mic_0 = MicChain(bus2, 1)
46    chain_mic_0.set_trigger_callback(MicChain.TRIGGER_LOW_TO_HIGH, chain_mic_0_low_to_high_event)
47    chain_mic_0.set_trigger_callback(MicChain.TRIGGER_HIGH_TO_LOW, chain_mic_0_high_to_low_event)
48    chain_mic_0.set_trigger(True)
49    chain_mic_0.set_rgb_color(0x000064)
50    label_status.setVisible(False)
51
52
53def loop():
54    global title0, label_adc, label_status, bus2, chain_mic_0, last_trigger_time
55    M5.update()
56    label_adc.setText(str((str("ADC: ") + str((chain_mic_0.get_adc12())))))
57    time.sleep_ms(100)
58    if (time.ticks_diff((time.ticks_ms()), last_trigger_time)) > 3000:
59        label_status.setVisible(False)
60
61
62if __name__ == "__main__":
63    try:
64        setup()
65        while True:
66            loop()
67    except (Exception, KeyboardInterrupt) as e:
68        try:
69            bus2.deinit()
70            from utility import print_error_msg
71
72            print_error_msg(e)
73        except ImportError:
74            print("please update to latest firmware")

Example output:

None

API

MicChain

class chain.mic.MicChain(bus, device_id)

Bases: KeyChain

Mic Chain class for interacting with microphone devices over Chain bus.

Parameters:
  • bus (ChainBus) – The Chain bus instance.

  • device_id (int) – The device ID of the microphone on the Chain bus.

UiFlow2 Code Block:

init.png

MicroPython Code Block:

from chain import ChainBus
from chain import MicChain

bus2 = ChainBus(2, tx=21, rx=22)
chain_mic_0 = MicChain(bus2, 1)

For other button and some general methods, please refer to the ChainKey class.

get_adc8()

Get the 8-bit ADC value of the microphone.

Returns:

8-bit ADC value (0-255), or None if failed.

Return type:

int

UiFlow2 Code Block:

get_adc8.png

MicroPython Code Block:

value = chain_mic_0.get_adc8()
get_adc12()

Get the 12-bit ADC value of the microphone.

Returns:

12-bit ADC value (0-4095), or None if failed.

Return type:

int

UiFlow2 Code Block:

get_adc12.png

MicroPython Code Block:

value = chain_mic_0.get_adc12()
set_trigger(enable)

Enable or disable threshold-triggered reporting.

Parameters:

enable (bool) – True to enable threshold-triggered reporting, False to disable it.

Returns:

True if the operation was successful, False otherwise.

Return type:

bool

UiFlow2 Code Block:

set_trigger.png

MicroPython Code Block:

success = chain_mic_0.set_trigger(True)
get_trigger()

Get whether threshold-triggered reporting is enabled.

Returns:

True if threshold-triggered reporting is enabled, False if disabled. Returns False if failed.

Return type:

bool

UiFlow2 Code Block:

get_trigger.png

MicroPython Code Block:

enabled = chain_mic_0.get_trigger()
set_trigger_thresh(threshold, save=False)

Set the microphone trigger threshold.

Parameters:
  • threshold (int) – Threshold value (0-4095).

  • save (bool) – Whether to save the threshold to flash. Default: False.

Returns:

True if the operation was successful, False otherwise.

Return type:

bool

UiFlow2 Code Block:

set_trigger_thresh.png

MicroPython Code Block:

success = chain_mic_0.set_trigger_thresh(2000, True)
get_trigger_thresh()

Get the microphone trigger threshold.

Returns:

Threshold value (0-4095), or None if failed.

Return type:

int

UiFlow2 Code Block:

get_trigger_thresh.png

MicroPython Code Block:

threshold = chain_mic_0.get_trigger_thresh()
set_trigger_interval(interval_ms)

Set the minimum trigger interval (debounce time).

Parameters:

interval_ms (int) – Minimum time interval between triggers in milliseconds. Range: 300-1000.

Returns:

True if the operation was successful, False otherwise.

Return type:

bool

UiFlow2 Code Block:

set_trigger_interval.png

MicroPython Code Block:

success = chain_mic_0.set_trigger_interval(500)  # Set 500ms minimum interval between triggers
get_trigger_interval()

Get the minimum trigger interval (debounce time).

Returns:

Minimum trigger interval in milliseconds, or None if failed.

Return type:

int

UiFlow2 Code Block:

get_trigger_interval.png

MicroPython Code Block:

interval = chain_mic_0.get_trigger_interval()
set_trigger_callback(trigger_type, callback)

Set callback for microphone trigger events.

Parameters:
  • trigger_type (int) – Trigger type to listen for. Use MicChain.TRIGGER_LOW_TO_HIGH (0) or MicChain.TRIGGER_HIGH_TO_LOW (1).

  • callback – Callback function that will be called when microphone triggers.

Return type:

None

Note

Chain related methods cannot be called in the callback function.

UiFlow2 Code Block:

set_trigger_callback.png

MicroPython Code Block:

def mic_trigger_callback():
    print("Sound detected")

# Listen for low-to-high trigger only
chain_mic_0.set_trigger_callback(MicChain.TRIGGER_LOW_TO_HIGH, mic_trigger_callback)

# Listen for high-to-low trigger only
chain_mic_0.set_trigger_callback(MicChain.TRIGGER_HIGH_TO_LOW, mic_trigger_callback)