Raspberry Pi Pico - MP3 Player

This guide shows you how to make an MP3 player with a Raspberry Pi Pico, an MP3 player module, a Micro SD Card, and a speaker. The MP3 player plays music or audio files from the Micro SD Card. The Raspberry Pi Pico controls the MP3 player module to select and play music from the SD card, convert it to sound, and transmit it to the speaker. We will explain these steps thoroughly.

Then, you can modify the code to add a potentiometer or rotary encoder to control the volume.

Raspberry Pi Pico mp3 player

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×Serial MP3 Player Module
1×Micro SD Card
1×3.5mm Aux Speaker
1×Jumper Wires
1×Breadboard
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Serial MP3 Player Module and Speaker

Serial MP3 Player Module Pinout

A serial MP3 player module has three interfaces:

  • Raspberry Pi Pico Interface:
    • RX pin: This pin is for receiving data. Connect it to the TX pin on your Raspberry Pi Pico using a Serial connection.
    • TX pin: This pin is for sending data. Connect it to the RX pin on your Raspberry Pi Pico using a Serial connection.
    • VCC pin: This pin provides power. Connect it to a 3.3V pin of Raspberry Pi Pico.
    • GND pin: This is the ground pin. Connect it to a 0V source.
  • Speaker Interface:
    • A 3.5mm Aux output female jack.
  • Micro SD Card Interface:
    • The socket for the Micro SD Card is located at the back of the module.
    Serial MP3 Player Module Pinout
    image source: diyables.io

    Speaker Pinout

    A speaker usually has two connection points:

    • Audio connection: It has a 3.5mm Aux male connector for connecting to an MP3 player.
    • Power connection: It can connect to a USB, a 5V power adapter, or other power sources.

How It Works

What we should prepare:

  1. Put a list of songs or recordings on a micro SD card.
  2. Insert the micro SD card into the MP3 player module.
  3. Attach the MP3 player module to the Raspberry Pi Pico.
  4. Connect the MP3 player module to a speaker.
  5. Connect the speaker to a power source.

Each MP3 file on the Micro SD Card is numbered starting from 0, indicating the sequence of the songs.

We can configure the Raspberry Pi Pico to send commands to the MP3 player module. It supports these commands:

  • Begin Playing
  • Stop
  • Play Next
  • Play Previous
  • Change Volume

The MP3 player module plays the MP3 file stored on the micro SD card and converts it into an audio signal. It then sends this signal to the speaker using the 3.5mm Aux interface.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico MP3 player module

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico Code - Play Music

The code below begins to play the first song stored on the Micro SD Card.

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-mp3-player """ from machine import UART, Pin import time # Define constants CMD_PLAY_NEXT = 0x01 CMD_PLAY_PREV = 0x02 CMD_PLAY_W_INDEX = 0x03 CMD_SET_VOLUME = 0x06 CMD_SEL_DEV = 0x09 CMD_PLAY_W_VOL = 0x22 CMD_PLAY = 0x0D CMD_PAUSE = 0x0E CMD_SINGLE_CYCLE = 0x19 DEV_TF = 0x02 SINGLE_CYCLE_ON = 0x00 SINGLE_CYCLE_OFF = 0x01 PICO_RX = 9 # The Raspberry Pi Pico pin GP9 connected to the TX of the Serial MP3 Player module PICO_TX = 8 # The Raspberry Pi Pico pin GP8 connected to the RX of the Serial MP3 Player module # Initialize UART with the correct pins and baud rate mp3 = UART(1, baudrate=9600, tx=Pin(PICO_TX), rx=Pin(PICO_RX)) def mp3_command(command, dat): frame = bytearray(8) frame[0] = 0x7e # starting byte frame[1] = 0xff # version frame[2] = 0x06 # the number of bytes of the command without starting byte and ending byte frame[3] = command # frame[4] = 0x00 # 0x00 = no feedback, 0x01 = feedback frame[5] = (dat >> 8) & 0xFF # data high byte frame[6] = dat & 0xFF # data low byte frame[7] = 0xef # ending byte mp3.write(frame) # Wait for the chip initialization to complete time.sleep(0.5) # Select the TF card mp3_command(CMD_SEL_DEV, DEV_TF) time.sleep(0.2) # wait for 200ms # Play MP3 mp3_command(CMD_PLAY, 0x0000) # mp3_command(CMD_PAUSE, 0x0000) # Pause mp3 # mp3_command(CMD_PLAY_NEXT, 0x0000) # Play next mp3 # mp3_command(CMD_PLAY_PREV, 0x0000) # Play previous mp3 # mp3_command(CMD_SET_VOLUME, 30) # Change volume to 30 # Main loop to keep the script running while True: time.sleep(0.1) # Prevent tight loop

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Connect the Raspberry Pi Pico to the MP3 player module according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Enjoy the music

Raspberry Pi Pico Code - Play Music with control buttons

The wiring diagram between Raspberry Pi and Pico MP3 player speaker

This image is created using Fritzing. Click to enlarge image

The following code is an updated version of the previous one. It features four buttons that help you manage the MP3 player.

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-mp3-player """ from machine import UART, Pin import time from DIYables_MicroPython_Button import Button # Define constants CMD_PLAY_NEXT = 0x01 CMD_PLAY_PREV = 0x02 CMD_PLAY_W_INDEX = 0x03 CMD_SET_VOLUME = 0x06 CMD_SEL_DEV = 0x09 CMD_PLAY_W_VOL = 0x22 CMD_PLAY = 0x0D CMD_PAUSE = 0x0E CMD_SINGLE_CYCLE = 0x19 DEV_TF = 0x02 SINGLE_CYCLE_ON = 0x00 SINGLE_CYCLE_OFF = 0x01 PICO_RX = 9 # The Raspberry Pi Pico pin GP9 connected to the TX of the Serial MP3 Player module PICO_TX = 8 # The Raspberry Pi Pico pin GP8 connected to the RX of the Serial MP3 Player module # Initialize UART with the correct pins and baud rate mp3 = UART(1, baudrate=9600, tx=Pin(PICO_TX), rx=Pin(PICO_RX)) # Initialize buttons using the Button library button_play = Button(2) # The Raspberry Pi Pico pin GP2 for play button_pause = Button(3) # The Raspberry Pi Pico pin GP3 for pause button_next = Button(4) # The Raspberry Pi Pico pin GP4 for next button_prev = Button(5) # The Raspberry Pi Pico pin GP5 for previous # Set debounce time to 50 milliseconds for each button button_play.set_debounce_time(50) button_pause.set_debounce_time(50) button_next.set_debounce_time(50) button_prev.set_debounce_time(50) def mp3_command(command, dat): frame = bytearray(8) frame[0] = 0x7e # starting byte frame[1] = 0xff # version frame[2] = 0x06 # the number of bytes of the command without starting byte and ending byte frame[3] = command # frame[4] = 0x00 # 0x00 = no feedback, 0x01 = feedback frame[5] = (dat >> 8) & 0xFF # data high byte frame[6] = dat & 0xFF # data low byte frame[7] = 0xef # ending byte mp3.write(frame) # Wait for the chip initialization to complete time.sleep(0.5) # Select the TF card mp3_command(CMD_SEL_DEV, DEV_TF) time.sleep(0.2) # wait for 200ms # Main loop to check buttons and send commands while True: # Update the state of each button button_play.loop() button_pause.loop() button_next.loop() button_prev.loop() # Handle play button press if button_play.is_pressed(): print("Play mp3") mp3_command(CMD_PLAY, 0x0000) # Handle pause button press if button_pause.is_pressed(): print("Pause mp3") mp3_command(CMD_PAUSE, 0x0000) # Handle next button press if button_next.is_pressed(): print("Play next mp3") mp3_command(CMD_PLAY_NEXT, 0x0000) # Handle previous button press if button_prev.is_pressed(): print("Play previous mp3") mp3_command(CMD_PLAY_PREV, 0x0000) time.sleep(0.01) # Small delay to be gentle on the CPU

Detailed Instructions

  • On Thonny IDE, navigate to the Tools Manage packages on the Thonny IDE.
  • Search “DIYables-MicroPython-Button”, then find the Button library created by DIYables.
  • Click on DIYables-MicroPython-Button, then click Install button to install Button library.
Raspberry Pi Pico Button library
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico.
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Press the buttons one by one.
  • Enjoy the music

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!