Arduino MicroPython MP3 Player

This guide shows you how to build an MP3 player using an Arduino running MicroPython, an MP3 player module, a Micro SD Card, and a speaker. The MP3 player will play songs or audio files stored on the Micro SD Card. The Arduino communicates with the MP3 player module to select and play tracks from the SD card, convert them into audio, and send the output to the speaker. In this guide, you will learn:

You can modify the code to add a potentiometer or rotary encoder to control the volume.

Arduino MicroPython mp3 player

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-C
1×Serial MP3 Player Module
1×Micro SD Card
1×3.5mm Aux Speaker
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×(Recommended) Breadboard Shield For Arduino Uno/Mega/Giga
1×(Recommended) Enclosure For Arduino Giga

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 main interfaces:

  • Arduino Interface:
    • RX pin: Receives data from the Arduino. Connect this to the TX pin on your Arduino for serial communication.
    • TX pin: Sends data to the Arduino. Connect this to the RX pin on your Arduino for serial communication.
    • VCC pin: Provides power to the module. Connect this to the 3.3V pin on your Arduino.
    • GND pin: Ground pin. Connect this to the Arduino's ground (0V).
  • Speaker Interface:
    • Features a 3.5mm Aux output female jack for audio output.
  • Micro SD Card Interface:
    • Includes a Micro SD Card slot located on the back of the MP3 module for storing audio files.
    Serial MP3 Player Module Pinout
    image source: diyables.io

    Speaker Pinout

    A speaker typically has two types of connections:

    • Audio Connection: Equipped with a 3.5mm Aux male connector to connect to an MP3 player.
    • Power Connection: Can be powered through a USB, a 5V power adapter, or other compatible power sources.

    Alternatively, you can use earphones with a 3.5mm Aux male connector.

How It Works

Here’s what you need to do:

  1. Load a selection of songs or recordings onto a Micro SD card.
  2. Insert the Micro SD card into the MP3 player module.
  3. Connect the MP3 player module to the Arduino.
  4. Connect the MP3 player module to a speaker.
  5. Plug the speaker into a power source. If you're using headphones or earphones, you can skip this step.

Each MP3 file on the Micro SD card is numbered starting from 0, which determines the playback order. We will write a MicroPython script for the Arduino to send various commands to the MP3 player module. Supported commands include:

  • Start Playing
  • Stop
  • Play Next Track
  • Play Previous Track
  • Adjust Volume

Upon receiving a command, the MP3 player module plays the files stored on the Micro SD card, converts them into audio signals, and sends these signals to the speaker via the 3.5mm Aux jack.

Wiring Diagram

  • The Wiring Diagram between Arduino MicroPython MP3, player module, and speaker
The wiring diagram between Arduino MicroPython MP3 player module

This image is created using Fritzing. Click to enlarge image

  • The Wiring Diagram between Arduino MicroPython MP3, player module, and earphone
The wiring diagram between Arduino MicroPython MP3 player module

This image is created using Fritzing. Click to enlarge image

Arduino MicroPython Code - Play Music

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

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-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 # Initialize UART with the correct pins and baud rate mp3 = UART(6, baudrate=9600) 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

As shown in the wiring diagram, the MP3 module is connected to the RX3 and TX3 pins on the Arduino Giga. You might expect the code to use mp3 = UART(3, baudrate=9600), but it actually uses mp3 = UART(6, baudrate=9600). This might seem confusing, but it's correct. The TX3 and RX3 pins are associated with UART6 on the MicroPython firmware for Arduino Giga.

Detailed Instructions

Here’s instructions on how to run the above MicroPython code on Arduino with Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Make sure MicroPython firmware is installed on your Arduino board.
  • If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
  • Connect the Arduino board to the MP3 player module according to the provided diagram.
  • Connect the Arduino board to your computer with a USB cable.
  • Open Thonny IDE and go to Tools Options.
  • Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
  • Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided Arduino MicroPython code and paste it into Thonny's editor.
  • Save the MicroPython code to your Arduino by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device and name the file main.py.
  • Click the green Run button (or press F5) to execute the code.
  • Enjoy the music

Arduino MicroPython Code - Play Music with control buttons

The wiring diagram between Arduino MicroPython 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 Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-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 # Initialize UART with the correct pins and baud rate mp3 = UART(6, baudrate=9600) # Initialize buttons using the Button library button_play = Button('D2') # The Arduino Giga WiFi pin D2 connected to the play button button_pause = Button('D3') # The Arduino Giga WiFi pin D3 connected to the pause button button_next = Button('D4') # The Arduino Giga WiFi pin D4 connected to the next button button_prev = Button('D5') # The Arduino Giga WiFi pin D5 connected to the previous button # 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.
Arduino MicroPython Button library
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Arduino board.
  • 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!