How do I access the mic on the m5stickc?



  • The posts are very confusing! Half say that it is i2s, and the Arduino code seems to support that. But the other half say that it is just an adc on port 34, and the micropython examples seem to support that!
    I'm interested in using micropython.



  • Hello @dclaar

    looking at the specifications for M5StickC the microphone is listed as SPM1423. Search results for SPM1423 indicate that it is has a PDM encoding output (CLK/DAT) which seems to be a special I2S case.

    I have found this github project, which add the I2S-PDM mode to micropython.

    Here you can find a description of the differences between I2S and PDM.

    Thanks
    Felix



  • Thanks! Going off that, I found this post, which points to the M5GO blob, which has

    from machine import I2S
    

    Going into REPL:

     from machine import I2S
    >>> dir(I2S)
    ['__class__', '__name__', 'read', 'start', 'stop', 'write', '__bases__', '__dict__', 'CHANNEL_ALL_LEFT', 'CHANNEL_ALL_RIGHT', 'CHANNEL_ONLY_LEFT', 'CHANNEL_ONLY_RIGHT', 'CHANNEL_RIGHT_LEFT', 'DAC_BOTH_EN', 'DAC_DISABLE', 'DAC_LEFT_EN', 'DAC_RIGHT_EN', 'FORMAT_I2S', 'FORMAT_I2S_LSB', 'FORMAT_I2S_MSB', 'FORMAT_PCM', 'FORMAT_PCM_LONG', 'FORMAT_PCM_SHORT', 'I2S_NUM_0', 'I2S_NUM_1', 'MODE_ADC_BUILT_IN', 'MODE_DAC_BUILT_IN', 'MODE_MASTER', 'MODE_PDM', 'MODE_RX', 'MODE_SLAVE', 'MODE_TX', 'adc_enable', 'bits', 'deinit', 'init', 'nchannels', 'sample_rate', 'set_adc_pin', 'set_dac_mode', 'set_pin', 'volume']
    >>> import wave
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: no module named 'wave'
    

    So at least part of it is there! I found a listing of the class, but no documentation.

    Then I found some examples, but they say Micropython after June 2021, so may not apply??

    That talks about a "WS" pin, which I found described here.

    The schematic shows only 2 pins however: SCL & SDA. The Unit unit :) appears to have WS tied to ground. Mouser indicates that the pin is tied low internally, and that is the left channel, but the arduino code sets it to I2S_CHANNEL_FMT_ALL_RIGHT.
    Here's an I2S tutorial.
    So, time to play I guess!



  • Thinking about how to test it: the M5StickC has no speaker.
    OK, I have the Core Gray: It has a speaker! But no mic.
    Maybe I could hook both of them together...
    This is getting way too complicated for the idea I had in mind, which was some responsive blinky lights for Halloween. (Yes, it's next week. Your point?) :) I think I'll use the accelerometer instead.



  • Hi is spent a long time to find how use PDM microphone in m5stickC and finally i pass. I find some code thats should work, but its not working.

    from machine import I2S, Pin
    
    mic = I2S(I2S.NUM0, ws=Pin(0), sdin=Pin(34), mode=I2S.MASTER_PDW, dataformat=I2S.B16, channelformat=I2S.ONLY_RIGHT, samplerate=16000, dmacount=16, dmalen=256)
    
    buffer = bytearray(7168)
    
    while True:
        # read data into buffer
        mic.readinto(buffer)
        # do something with data stored in buffer
        #
    mic.deinit()
    


  • @robalstona pin 0 is the clock, not ws, according to the sticker on the back. Don't know if that helps.



  • I was successful in recording something like a wave form with the following code:

    from machine import I2S, Pin
    import array
    
    mic = I2S(I2S.NUM0, ws=Pin(0), sdin=Pin(34), mode=I2S.MASTER_PDM, dataformat=I2S.B16, channelformat=I2S.ONLY_RIGHT, samplerate=16000, dmacount=16, dmalen=256)
    
    b2 = array.array("h", 4096 * [0])
    mic.readinto(b2)
    

    However, there seems to be a systematic offset around ~+1000. Not sure what is going on here. Could it be related to PDM encoding and the I2S clock? Also, I experience values that are larger than the 12bit precision documented somewere

    def eval():
        mic.readinto(b2)
        print(min(b2), max(b2), sum(b2)/len(b2))
    
    for i in range(1000): eval()
    ... 
    48 1963 1017.062
    811 1237 1027.727
    927 1122 1021.952
    849 1286 1023.281
    763 1719 1029.73
    102 1966 1015.185
    -903 2878 1012.832
    -788 2683 984.9133
    -1524 2761 982.3828
    -815 3075 986.0596
    -864 3044 980.6567
    -713 2904 993.0678
    -669 2615 1014.595
    -138 2182 1022.794
    -73 1954 1018.482
    ...