CircuitPython has released a stable version for the Raspberry Pi Pico which is version 6.2.0. The latest firmware has a built-in library for audiomp3. So for this tutorial, I would like to play MP3 files using Maker Pi Pico. Ok let’s try!
This video shows how to play MP3 files on Raspberry Pi Pico / Maker Pi Pico.
This is the list of items used in the video.
This is CircuitPython sample code to play MP3 file on GP18 when button GP20 is pressed. Please replace with your MP3 file name and duration. Refer to the video for more detail.
|from audiomp3 import MP3Decoder|
|from audioio import AudioOut|
|from audiopwmio import PWMAudioOut as AudioOut|
|pass # not always supported by every board!|
|button1 = digitalio.DigitalInOut(board.GP20)|
|# Replace with your mp3files name|
|mp3files = ["Selamat.mp3"]|
|# You have to specify some mp3 file when creating the decoder|
|mp3 = open(mp3files, "rb")|
|decoder = MP3Decoder(mp3)|
|audio = AudioOut(board.GP18)|
|if not button1.value:|
|decoder.file = open(mp3files, "rb")|
|t = time.monotonic()|
|while time.monotonic() – t < 3:|
- CircuitPython Essentials – CircuitPython MP3 Audio
Thanks for reading this tutorial. If you have any technical inquiries, please post at Cytron Technical Forum.
“Please be reminded, this tutorial is prepared for you to try and learn.
You are encouraged to improve the code for better application.“