[{"element_type":"alert","content":"\u003cp\u003eThe library for I2S input support relies on PIO, a feature only found within RP2xxx based microcontrollers.\u003c/p\u003e","metadata":{"class":"element alert-element build-alert alert-info","markdown":"The library for I2S input support relies on PIO, a feature only found within RP2xxx based microcontrollers.","alert_type":"info"}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003cp\u003eWorking with I2S audio input to your project can be daunting, but the \u003ca title=\"Read the documentation for CircuitPython_PIO_I2S\" href=\"https://circuitpython-pio-i2s.readthedocs.io/\" target=\"_blank\"\u003eCircuitPython_PIO_I2S\u003c/a\u003e library helps with the heavy lifting.\u003c/p\u003e\n\u003cp\u003eThis guide is limited to devices supporting a compatible processor RP2xxx such as the Raspberry Pi Pico and Raspberry Pi Pico 2. Python support via Adafruit_Blinka is not supported at this time.\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"product","content":"https://www.adafruit.com/product/3421","metadata":{}},{"element_type":"product","content":"https://www.adafruit.com/product/6049","metadata":{}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003ch2\u003eCircuitPython Microcontroller Wiring\u003c/h2\u003e\n\u003cp\u003eHere is how you'll wire your I2S MEMS Microphone breakout (in this case, \u003ca href=\"https://www.adafruit.com/product/3421\" target=\"_blank\"\u003eSPH0645LM4H\u003c/a\u003e) to a \u003ca href=\"https://www.adafruit.com/product/5525\" target=\"_blank\"\u003eRaspberry Pi Pico\u003c/a\u003e (RP2040):\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"user_image","content":"https://cdn-learn.adafruit.com/user_assets/assets/000/001/416/original/I2S_Microphone_Learn_Guide_bb.jpg?1732722091","metadata":{"caption":""}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003cul\u003e\n\u003cli\u003e\n\u003cstrong\u003ePico 3.3V\u003c/strong\u003e to \u003cstrong\u003ebreakout 3V (red wire)\u003c/strong\u003e\n\u003c/li\u003e\n\u003cli\u003e\n\u003cstrong\u003ePico GND\u003c/strong\u003e to \u003cstrong\u003ebreakout GND (black wire)\u003c/strong\u003e\n\u003c/li\u003e\n\u003cli\u003e\n\u003cstrong\u003ePico GP0\u003c/strong\u003e to \u003cstrong\u003ebreakout BCLK (yellow wire)\u003c/strong\u003e\n\u003c/li\u003e\n\u003cli\u003e\n\u003cstrong\u003ePico GP1\u003c/strong\u003e to \u003cstrong\u003ebreakout LRCL (purple wire)\u003c/strong\u003e\n\u003c/li\u003e\n\u003cli\u003e\n\u003cstrong\u003ePico GP2\u003c/strong\u003e to \u003cstrong\u003ebreakout DOUT (blue wire)\u003c/strong\u003e\n\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003eFor the I2S bus clock and data signals, you can use nearly any GPIO pins available on the Pico. The only limitations are that they may not be reserved for another purpose and LRCL (word select) must be next in line sequentially from the BCLK (bit clock) (ie: BCLK = GP3 \u0026amp; LRCL = GP4). If this is not configured correctly, the CircuitPython_PIO_I2S library will throw an exception.\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"text","content":"\n  \n  \n  \n        \u003ch2\u003eInstallation of CircuitPython Library\u003c/h2\u003e\n      \n\n\n","metadata":{}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003cp\u003eThe \u003cstrong\u003eCircuitPython_PIO_I2S\u003c/strong\u003e library can be manually installed to your device by copying \u003cstrong\u003epio_i2s.py\u003c/strong\u003e to \u003cstrong\u003elib\u003c/strong\u003e folder of your \u003cstrong\u003eCIRCUITPY\u003c/strong\u003e drive. You will also need the \u003cstrong\u003eAdafruit_CircuitPython_PIOASM\u003c/strong\u003e library which can be obtained within the \u003ca href=\"https://circuitpython.org/libraries\"\u003eAdafruit CircuitPython Bundle\u003c/a\u003e or directly within \u003ca href=\"https://github.com/adafruit/Adafruit_CircuitPython_PIOASM\"\u003eGitHub\u003c/a\u003e.\u003c/p\u003e\n\u003cp\u003eThe library and dependencies installation process can also be done automatically using \u003ca href=\"https://learn.adafruit.com/keep-your-circuitpython-libraries-on-devices-up-to-date-with-circup\"\u003ecircup\u003c/a\u003e with the following command:\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003e\u003ccode\u003ecircup install pio_i2s\u003c/code\u003e\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003eFor more information, refer to the \u003ca href=\"https://circuitpython-pio-i2s.readthedocs.io/en/latest/\" target=\"_blank\"\u003edocumentation\u003c/a\u003e of \u003cstrong\u003eCircuitPython_PIO_I2S\u003c/strong\u003e.\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003ch2\u003eExample Code\u003c/h2\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003cp\u003eOnce everything is saved to the \u003cstrong\u003eCIRCUITPY\u003c/strong\u003e drive and you have the microphone breakout wired up,\u0026nbsp;\u003ca href=\"https://learn.adafruit.com/welcome-to-circuitpython/kattni-connecting-to-the-serial-console\" target=\"_blank\"\u003econnect to the serial console\u003c/a\u003e to begin writing your script.\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"code","content":"# SPDX-FileCopyrightText: Copyright (c) 2024 Cooper Dalrymple\n#\n# SPDX-License-Identifier: Unlicense\n\nimport board\nimport ulab.numpy as np\n\nimport pio_i2s\n\n# The update rate per second which will determine the size of our audio buffer\nRATE = 10\n\nmic = pio_i2s.I2S(\n    bit_clock=board.GP0,  # word select is GP1\n    data_in=board.GP2,\n    channel_count=1,\n    sample_rate=48000,\n    bits_per_sample=16,  # ulab.numpy only works with integers up to 16-bits\n    samples_signed=True,\n)\n\n# Create an array which will contain one second of audio data\ndata = np.array([0] * int(mic.sample_rate * mic.channel_count / RATE), dtype=np.int16)\n\nwhile True:\n    \n    # Record one second of audio samples from the microphone into our array\n    mic.record(data)\n    \n    # Determine the level of the incoming audio using ulab.numpy\n    level = (np.max(data) - np.min(data)) / 65536  # should contain a value from 0.0 to 1.0\n    print(level * 100)  # scale to 0.0 to 100.0","metadata":{"language":"python","linenums":false}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003cp\u003eRun the script within your serial console (ie: Mu or Thonny). You should see a number from 0.0 to 100.0 printed out 10 times a second representing the relative volume of the sound picked up by the microphone. Try making noises to see how that affects the volume level. You can use the Serial Plotter of the Arduino IDE to plot a line chart of the data in real-time like the following:\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"user_image","content":"https://cdn-learn.adafruit.com/user_assets/assets/000/001/417/original/Screenshot_From_2024-11-27_10-07-29.png?1732723667","metadata":{"caption":""}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n        \u003cp\u003eYou can also perform FFT spectral analyzing on a set of audio samples from the microphone using the \u003ccode\u003eulab.utils.spectrogram\u003c/code\u003e method.\u003c/p\u003e\n      \n\n\n\n\n\n","metadata":{}},{"element_type":"code","content":"# SPDX-FileCopyrightText: Copyright (c) 2024 Cooper Dalrymple\n#\n# SPDX-License-Identifier: Unlicense\n\nimport board\nimport ulab.numpy as np\nimport ulab.utils\n\nimport pio_i2s\n\nFFT_SIZE = 32\n\nmic = pio_i2s.I2S(\n    bit_clock=board.GP0,  # word select is GP1\n    data_in=board.GP2,\n    channel_count=1,\n    sample_rate=48000,\n    bits_per_sample=16,  # ulab.numpy only works with integers up to 16-bits\n    samples_signed=True,\n    buffer_size=1024,  # increase this value to improve the resolution of the spectrogram\n)\n\nwhile True:\n    \n    # Grab a single buffer (1024 samples) from the microphone and convert it to an np.ndarray object\n    data = np.array(mic.read(block=True), dtype=np.int16)\n    \n    # Perform Fourier Fast Transform (FFT) algorithm on audio signal\n    data = ulab.utils.spectrogram(data)\n    \n    # Interpolate down to a smaller set of data\n    data = np.interp(\n        np.arange(0, len(data), len(data) / FFT_SIZE, dtype=np.float),\n        np.arange(0, len(data), 1, dtype=np.uint16),\n        data\n    )\n    \n    # Print out the spectrum\n    for i in range(len(data)):\n        print(data[i], end=\"\\t\")\n    print()","metadata":{"language":"auto","linenums":false}},{"element_type":"user_image","content":"https://cdn-learn.adafruit.com/user_assets/assets/000/001/419/original/Screenshot_From_2024-11-27_10-21-58.png?1732724534","metadata":{"caption":""}},{"element_type":"text","content":"\n        \u003cp\u003eIf you would like to learn more ways you can utilize your I2S Microphone Breakout in your project, check out the examples available in the \u003cstrong\u003eCircuitPython_PIO_I2S\u003c/strong\u003e \u003ca href=\"https://github.com/relic-se/CircuitPython_PIO_I2S/tree/master/examples\" target=\"_blank\"\u003erepository\u003c/a\u003e.\u003c/p\u003e\n      ","metadata":{}}]