PDM Unit

This is the driver library of PDM Unit, which is provides a set of methods to control the PDM microphone. Through the I2S interface, the module can record audio data and save it as WAV files.

Support the following products:

pdm

UiFlow2 Example

record voice and play voice

Open the pdm_cores3_example.m5f2 project in UiFlow2.

This example records voice and plays voice.

UiFlow2 Code Block:

example.png

Example output:

None

MicroPython Example

record voice and play voice

This example records voice and plays voice.

MicroPython Code Block:

 1import os, sys, io
 2import M5
 3from M5 import *
 4from unit import PDMUnit
 5import time
 6
 7
 8title0 = None
 9label0 = None
10label1 = None
11label2 = None
12label3 = None
13pdm_0 = None
14
15
16rec_data = None
17
18
19def setup():
20    global title0, label0, label1, label2, label3, pdm_0, rec_data
21
22    M5.begin()
23    Widgets.fillScreen(0x222222)
24    title0 = Widgets.Title("PDMUnit CoreS3 Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18)
25    label0 = Widgets.Label("Is Start:", 20, 54, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
26    label1 = Widgets.Label("Is Done:", 20, 119, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
27    label2 = Widgets.Label("label2", 131, 52, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
28    label3 = Widgets.Label("label3", 133, 121, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
29
30    pdm_0 = PDMUnit((1, 2), i2s_port=2, sample_rate=44100)
31    Speaker.begin()
32    Speaker.setVolumePercentage(1)
33    Speaker.end()
34    pdm_0.begin()
35    label2.setText(str("waiting..."))
36    time.sleep(2)
37    rec_data = bytearray(44100 * 15)
38    pdm_0.record(rec_data, 44100, False)
39    time.sleep_ms(150)
40    while pdm_0.isRecording():
41        label2.setText(str("recording..."))
42        time.sleep_ms(100)
43    pdm_0.end()
44    label2.setText(str("ending..."))
45    Speaker.begin()
46    label3.setText(str("playing..."))
47    Speaker.playRaw(rec_data, 44100)
48    while Speaker.isPlaying():
49        time.sleep_ms(150)
50    label3.setText(str("done"))
51
52
53def loop():
54    global title0, label0, label1, label2, label3, pdm_0, rec_data
55    M5.update()
56
57
58if __name__ == "__main__":
59    try:
60        setup()
61        while True:
62            loop()
63    except (Exception, KeyboardInterrupt) as e:
64        try:
65            from utility import print_error_msg
66
67            print_error_msg(e)
68        except ImportError:
69            print("please update to latest firmware")

Example output:

None

API

PDMUnit

class unit.pdm.PDMUnit(*args, **kwargs)

Bases: object

PDM Unit class.

Parameters:
  • port (list | tuple) – Connect to the PDM Unit.

  • i2s_port (int) – I2S port number(0 or 1, 2 is automatic select of available ports).

  • sample_rate (int) – Sample rate.

UiFlow2 Code Block:

init.png

MicroPython Code Block:

from unit import PDMUnit

pdm_0 = PDMUnit((1, 2), i2s_port=0, sample_rate=44100)

PDMUnit class inherits Mic class, See hardware.Mic for more details.