The PSOC™ Edge E84 AI Kit provides an easy way to prototype edge AI and embedded applications directly on hardware. One of the simplest ways to explore its capabilities is by using the onboard PDM microphones together with MicroPython.
In this ProTip, we will demonstrate how to quickly access the microphones on the board using MicroPython and read real-time audio levels.
HardwareFor this example, the only required hardware is the PSOC™ Edge E84 AI Kit.
The board integrates two digital PDM microphones, which are located on both sides of the development kit. These microphones capture environmental sound and stream it to the microcontroller through a PDM interface.
This built-in hardware makes it possible to immediately experiment with audio sensing without any additional external components.
Micropython
To follow this protip, you will need:
- PSOC™ Edge E84 AI Kit
- MicroPython firmware installed on the board
- A MicroPython IDE such as Thonny
If you have not installed MicroPython on the board, you can follow the setup guide here!
Once MicroPython is running on the device, we can start interacting with the microphones using the PDM_PCM interface.
Reading Audio Data from the Microphone
The following code initializes the onboard PDM microphone interface, reads audio samples into a buffer, and calculates a simple sound level value.
1. Import required libraries
from machine import PDM_PCM, Pin
import array
import time2. Create an audio buffer
# buffer for audio samples
buf = array.array('h', [0] * 256)
This buffer stores 256 audio samples captured from the microphone.The 'h' type represents 16-bit signed integers, which match the microphone’s output format.
3. Configure the microphone interface
# microphone interface
mic = PDM_PCM(
sck=Pin("P8_5"), # PDM clock
data=Pin("P8_6"), # PDM data
sample_rate=16000,
bits=16,
format=PDM_PCM.MONO
)Here we configure the PDM microphone driver.
Key parameters:
sck → PDM clock signal
data → microphone data stream
sample_rate = 16000 → common audio sampling rate
bits = 16 → 16-bit audio samples
MONO format → single channel audio
4. Read audio samples
print("Microphone demo running...")
while True:
mic.readinto(buf)This command fills the buffer with new audio samples captured from the microphone.
5. Compute sound level
level = sum(abs(x) for x in buf) // len(buf)
print(level)
time.sleep(0.05)This calculates the average absolute amplitude of the captured audio samples.
The result provides a simple representation of sound intensity as seen in the plotter here.
Level Up: From Level Meter to Audio Recording
So, we’ve got our audio levels dancing on the screen. But wouldn’t it be cooler if we could actually capture that sound? Imagine building a voice-activated switch, a sound-logger for your garden, or just recording a quick memo. With the PSOC™ Edge E84 AI Kit, moving from reading data to storing it is surprisingly easy.
We’re going to take our simple level meter and upgrade it into a fully functional audio recorder. We’ll write a script that records audio directly to the board’s internal memory, saves it as a file, and lets you download and listen to it on your PC. Let’s turn those sound waves into data!
The Recording Script
Instead of just printing numbers, our new script initializes the PDM microphone, captures a stream of audio samples for a set duration, and writes them to a file named audio.raw.
from machine import PDM_PCM, Pin
import uio
import time
def record_audio(filename, time_sec):
rx_buf = bytearray([0] * 128)
with uio.open(filename, "wb") as f:
start_time = time.time()
while time.time() - start_time < time_sec:
num_read = pdm_pcm.readinto(rx_buf)
f.write(rx_buf[:num_read])
########################################
#### Audio configuration parameters ####
########################################
pdm_pcm_config = {
"sample_rate": 16000,
"bits_per_sample": 16,
"format": PDM_PCM.STEREO,
"audio_file": "audio.raw",
"time_sec": 5,
}
print("########## Audio configuration ##########")
print(f"format: {pdm_pcm_config['format']}")
print(f"sample_rate: {pdm_pcm_config['sample_rate']}")
print(f"bits_per_sample: {pdm_pcm_config['bits_per_sample']}")
print(f"audio_file: {pdm_pcm_config['audio_file']}")
print(f"time_sec: {pdm_pcm_config['time_sec']}")
print("########################################")
########################################
print("-- Starting audio recording...")
pdm_pcm = PDM_PCM(
sck=Pin("P8_5"),
data=Pin("P8_6"),
sample_rate=pdm_pcm_config["sample_rate"],
bits=pdm_pcm_config["bits_per_sample"],
format=pdm_pcm_config["format"],
)
record_audio(pdm_pcm_config["audio_file"], pdm_pcm_config["time_sec"])
pdm_pcm.deinit()
print("-- Audio recorded")How It Works
Let’s break down what’s happening under the hood:
- Configuration: We define our audio settings in a simple dictionary. We’re using 16, 000 Hz (ideal for voice), 16-bit resolution, and STEREO format to utilize both onboard microphones.
- The RecordingFunction:
record_audio()opens a file in binary write mode("wb"). It creates a small buffer(rx_buf)and enters a loop that runs until the specified time is reached. - Capture & Save: Inside the loop,
pdm_pcm.readinto(rx_buf)fills the buffer with fresh audio samples. The script then writes only the newly captured bytes to the file usingf.write(rx_buf[:num_read]). - Setup & Cleanup: We initialize the PDM interface on pins
P8_5(clock) andP8_6(data) with our config parameters. After recording finishes,pdm_pcm.deinit()cleanly releases the hardware resources.
Let’s Record!
Ready to capture some sound? Follow these steps:
- Connect & Configure: Make sure your PSOC™ Edge E84 AI Kit is connected to your laptop. In Thonny, verify that the correct serial port is selected in the bottom right corner.
- Run the Script: Paste the code above into your IDE, save it (e.g., as record.py), and hit the Run button.
- Speak Up! The Shell will print
-- Starting audio recording.... Now is your time to shine: Talk, whistle, or clap for 5 seconds. Go on, we’re listening! - Finished: Once the time is up, you’ll see
-- Audio recorded successfully!in the Shell.
Retrieving and Playing Your Audio
The audio is now safely stored on the board, but how do we hear it? It’s stored as a raw PCM file, which standard media players might struggle with. Here is the best way to listen to your creation:
- View Files: In Thonny, go to View > Files. You should see a file named
audio.rawin the list. - Download: Right-click on
audio.rawand select Download to local host. Save it to your desktop.
- Play Back with Audacity: For the best experience, we recommend using the free audio editor Audacity:
- Open Audacity.
- Go to File > Import > Raw Data.
- Select your downloaded
audio.rawfile. - In the import dialog, ensure the settings match our script:
- Encoding: Signed 16-bit PCM
- Byte Order: Little-endian
- Channels: Stereo
- Sample Rate: 16000 Hz
- Click Import and hit play!
Tip: If the audio sounds like chipmunks or a slow-motion monster, double-check that your Sample Rate in Audacity matches the 16, 000 Hz we used in the script!
FarewellThere you have it! We’ve covered the fundamentals of audio sensing on the PSOC™ Edge E84 AI Kit using MicroPython, from visualizing real-time sound levels to capturing, storing, and playing back full audio recordings. You now have a solid foundation to build smarter, event-driven audio projects, whether that’s a threshold-triggered noise logger, a voice-activated switch, or the stepping stone to on-device AI.
The possibilities are endless, so go ahead, capture the sounds around you, and turn that signal into something creative! We had a blast walking through these examples, and we’re genuinely excited to see what you build next. Feel free to share your projects in the comments or tag us on social media. Happy making! :)









Comments