Last Updated: 04/10/2021

Serial UART MP3 player

Components >> Serial UART MP3 player

Serial MP3 Player

Serial MP3 PLayer

The Serial MP3 player, often listed on places like Ebay as Seroal UART MP3/WAV player is a very simple way of being able to play WAV files from an Arduino

It is controlled by sending a series of codes via Serial to the unit.

In these examples I will be using an Arduino UNO so have used the SoftwareSerial library, however with other boards that have multiple Serial ports such as the Arduino Mega 2560 and Arduino Due they can be connected instead.

 

Serial MP3 Player Issues to consider

The board uses a mini SD card to store the music/sound files. The board does seem to have some issues with read speeds from the SD card, especially if files have been added, eleted and then more files added as the fragmetation seems to cause a slower read speed. I would therefore recommend foratting the card and then copying the files over ni one go.

The player does not recognise filenames so I tend to name my files 01.wav, 02.wav etc and then keep a list of what the files is as most of my files are just sounds that will play on a model railway.

The board plays mono files so if your file is stereo just mix it to mono using a program like Audacity (free download).

Simple Circuit Diagram

Make sure you have the TX and RX connected the right way round

 

Example 1: UARTSerialV1.ino


Click to Download code:UARTSerialV1.ino

The basics of how the commands work.

 

 
/* 28/09/2021
 * UARTSerialV1
 * 
 * Simple example of how to play a file with a UART Serial MP3 Player module
 * 
Serial MP3 Player A     |  Arduino UNO R3
              RX        |   3
              TX        |   4
              VCC       |   5V
              GND       |   GND
 */
#include 
#define ARDUINO_RX 4//should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 3//connect to RX of the module
SoftwareSerial myMP3(ARDUINO_RX, ARDUINO_TX);



void setup() {
  Serial.begin(9600);
  Serial.println("UARTSerialV1");
  myMP3.begin(9600);
  delay(500);//allow everything to settle down
  //first we need to select the TF Card
  //Manual says the code for this is 7E 03 35 01 EF
  myMP3.write(0x7E); //start of instruction
  myMP3.write(0x03); //number of bytes excluding start and end byte
  myMP3.write(0x35);  //Command
  myMP3.write(0x01);  //data..usually 1 byte
  myMP3.write(0xEF);  //end byte
  delay(20);
  Serial.println("Play sound 1");
  //Now lets Play the first song 7E 04 41 00 01 EF
  myMP3.write(0x7E);  //start of instruction
  myMP3.write(0x04);  //number of bytes excluding start and end byte
  myMP3.write(0x41);  //command
  //next line is the first byte of data 0x00 will mean file is in root directory
 myMP3.write((byte)0x00);//forces the value to be read as a byte, otherwise error occurs
  myMP3.write(0x01);//will be the first file on the card
  myMP3.write(0xEF); //end byte
  delay(3000);//wait a second
  Serial.println("Play sound 3");
  myMP3.write(0x7E);  //start of instruction
  myMP3.write(0x04);  //number of bytes excluding start and end byte
  myMP3.write(0x41);  //command
  //next line is the first byte of data 0x00 will mean file is in root directory
 myMP3.write((byte)0x00);//forces the value to be read as a byte, otherwise error occurs
  myMP3.write(0x03);//will be the 3rd file on the card
  myMP3.write(0xEF); //end byte
}

void loop() {
  // put your main code here, to run repeatedly:

}

Example 2: UARTSerialV2.ino

Click to Download code:UARTSerialV2.ino

This script creates some simple functions to control the player

It uses code from the following tutorials:

Lesson 4: Using "for loop" to control code

Lesson 6: Basic Numeric variables, boolean, byte, int, unsigned int, long, unsigned long

Lesson 9: Basic of creating and using functions

 
/* 28//09/2021
 * UARTSerialV2
 * 
 * Creating some useful functions
 * 
Serial MP3 Player A     |  Arduino UNO R3
              RX        |   3
              TX        |   4
              VCC       |   5V
              GND       |   GND
 */
#include 
#define ARDUINO_RX 4//should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 3//connect to RX of the module
SoftwareSerial myMP3(ARDUINO_RX, ARDUINO_TX);

byte sendBuffer[6]; //buffer that will be used to store commands before sending

//Manual says the code for this is 7E 03 35 01 EF so load into buffer array
void selectTFCard(){
  sendBuffer[0] = 0x7E;
  sendBuffer[1] = 0x03;
  sendBuffer[2] = 0x35;
  sendBuffer[3] = 0x01;
  sendBuffer[4] = 0xEF;
  sendUARTCommand();
}

void playSound(byte songNumber){
  sendBuffer[0] = 0x7E;
  sendBuffer[1] = 0x04;
  sendBuffer[2] = 0x41;
  sendBuffer[3] = 0x00;
  sendBuffer[4] = songNumber;
  sendBuffer[5] = 0xEF;
  sendUARTCommand();
}
//play a sound at a set volume, only seems to apply to root directory files
void playSoundAtVolume(byte volume,byte songNumber){
  sendBuffer[0] = 0x7E;
  sendBuffer[1] = 0x04;
  sendBuffer[2] = 0x31;
  sendBuffer[3] = volume;
  sendBuffer[4] = songNumber;
  sendBuffer[5] = 0xEF;
  sendUARTCommand();
}
//
//set the playback volume 7E 03 31 0F EF = set volume to 0x0F = 15
void setplayVolume(byte volume){
  sendBuffer[0] = 0x7E;
  sendBuffer[1] = 0x03;
  sendBuffer[2] = 0x31;
  sendBuffer[3] = volume;
  sendBuffer[4] = 0xEF;
  sendUARTCommand();
}

//stop the current track playing
void stopSound(){
  sendBuffer[0] = 0x7E;
  sendBuffer[1] = 0x02;
  sendBuffer[2] = 0x0E;
  sendBuffer[3] = 0xEF;
  sendUARTCommand();
}

void sendUARTCommand(){
  int q;
  for(q=0;q < sendBuffer[1] + 2;q++){
    myMP3.write(sendBuffer[q]);  
  }
  Serial.println("Commands Sent");
  for(q=0;q < sendBuffer[1] + 2;q++){
    Serial.println(sendBuffer[q],HEX);  
  }
  delay(25);//stops odd commands being missed
}

void setup() {
  Serial.begin(9600);
  Serial.println("UARTSerialV2");
  myMP3.begin(9600);
  delay(500);//allow everything to settle down
  //first we need to select the TF Card
  selectTFCard();
  
}

void loop() {
  int q;
  /*
  //play through the first 5 tracks on the card
  for(q=0;q<5;q++){
    playSound(q);
    delay(4000);
    stopSound();  
  }
  */
  /*
  //Change the volume and then play track 4
  for(q=0;q<30;q++){
    setplayVolume(q);//change volume
    playSound(4);//play track 4
    delay(2000);
    stopSound(); 
  }
*/

  ////Play track 4 with pre set volumes
  for(q=0;q<60;q +=10){//volume incremented in steps of 10
    playSoundAtVolume(q, 4);//play track 4 at increasing volumes (q)
    delay(2000);
    stopSound();  
  }
}

Example 3: UARTSerialV3.ino

Click to Download code:UARTSerialV3.ino

In this example we add a couple of buttons to trigger sounds.
It uses code from the following tutorials:

Lesson 3: Using "if else" to control code

Lesson 7: delay() v's millis(), controlling timing of programs

How To 1 : Debouncing buttons

 

1k Ohm resistor

Arduino UNO and Serial MP3 player with button control

 
/* 28//09/2021
 * UARTSerialV3
 * 
 * Sounds with buttons
 * 
Serial MP3 Player A     |  Arduino UNO R3
              RX        |   3
              TX        |   4
              VCC       |   5V
              GND       |   GND

              Buttons on digital pins 8 & 9
 */
#include 
#define ARDUINO_RX 4//should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 3//connect to RX of the module
SoftwareSerial myMP3(ARDUINO_RX, ARDUINO_TX);

const int buttonOne = 8;
const int buttonTwo = 9;
unsigned long buttonDebounce;

byte sendBuffer[6]; //buffer that will be used to store commands before sending

//Manual says the code for this is 7E 03 35 01 EF so load into buffer array
void selectTFCard(){
  sendBuffer[0] = 0x7E;
  sendBuffer[1] = 0x03;
  sendBuffer[2] = 0x35;
  sendBuffer[3] = 0x01;
  sendBuffer[4] = 0xEF;
  sendUARTCommand();
}

void playSound(byte songNumber){
  sendBuffer[0] = 0x7E;
  sendBuffer[1] = 0x04;
  sendBuffer[2] = 0x41;
  sendBuffer[3] = 0x00;
  sendBuffer[4] = songNumber;
  sendBuffer[5] = 0xEF;
  sendUARTCommand();
}
//play a sound at a set volume, only seems to apply to root directory files
void playSoundAtVolume(byte volume,byte songNumber){
  sendBuffer[0] = 0x7E;
  sendBuffer[1] = 0x04;
  sendBuffer[2] = 0x31;
  sendBuffer[3] = volume;
  sendBuffer[4] = songNumber;
  sendBuffer[5] = 0xEF;
  sendUARTCommand();
}
//
//set the playback volume 7E 03 31 0F EF = set volume to 0x0F = 15
void setplayVolume(byte volume){
  sendBuffer[0] = 0x7E;
  sendBuffer[1] = 0x03;
  sendBuffer[2] = 0x31;
  sendBuffer[3] = volume;
  sendBuffer[4] = 0xEF;
  sendUARTCommand();
}

//stop the current track playing
void stopSound(){
  sendBuffer[0] = 0x7E;
  sendBuffer[1] = 0x02;
  sendBuffer[2] = 0x0E;
  sendBuffer[3] = 0xEF;
  sendUARTCommand();
}

void sendUARTCommand(){
  int q;
  for(q=0;q < sendBuffer[1] + 2;q++){
    myMP3.write(sendBuffer[q]);  
  }
  Serial.println("Commands Sent");
  for(q=0;q < sendBuffer[1] + 2;q++){
    Serial.println(sendBuffer[q],HEX);  
  }
  delay(25);//stops odd commands being missed
}

void setup() {
  Serial.begin(9600);
  Serial.println("UARTSerialV3");
  myMP3.begin(9600);
  delay(500);//allow everything to settle down
  //first we need to select the TF Card
  selectTFCard();
  pinMode(buttonOne,INPUT);
  pinMode(buttonTwo,INPUT);
}

void loop() {
  int buttonPressed;
  if(millis() > buttonDebounce){
    buttonPressed = digitalRead(buttonOne);
    if(buttonPressed > 0){
      buttonDebounce = millis() + 200;
      stopSound();//stop any existing sounds
      playSoundAtVolume(15, 1);//play sound at volume 15, track 1...drill
    }
  }
  if(millis() > buttonDebounce){
    buttonPressed = digitalRead(buttonTwo);
    if(buttonPressed > 0){
      buttonDebounce = millis() + 200;
      stopSound();//stop any existing sounds
      playSoundAtVolume(25, 3);//play sound at volume 25, track 3...welder
    }
  }
  
}

Example 4: UARTSerialV5.ino

This version has an extra function repeatSound() that loops the same track until another track is played

Click to Download code:UARTSerialV5.ino

 
/* 30/09/2021
 * UARTSerialV5
 * 
 * Added an extra function repeatSound() that loops a trackCreating some useful functions
 * 
Serial MP3 Player A     |  Arduino UNO R3
              RX        |   3
              TX        |   4
              VCC       |   5V
              GND       |   GND
 */
#include 
#define ARDUINO_RX 4//should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 3//connect to RX of the module
SoftwareSerial myMP3(ARDUINO_RX, ARDUINO_TX);

byte sendBuffer[6]; //buffer that will be used to store commands before sending

//Manual says the code for this is 7E 03 35 01 EF so load into buffer array
void selectTFCard(){
  sendBuffer[0] = 0x7E;
  sendBuffer[1] = 0x03;
  sendBuffer[2] = 0x35;
  sendBuffer[3] = 0x01;
  sendBuffer[4] = 0xEF;
  sendUARTCommand();
}

void playSound(byte songNumber){
  sendBuffer[0] = 0x7E;
  sendBuffer[1] = 0x04;
  sendBuffer[2] = 0x41;
  sendBuffer[3] = 0x00;
  sendBuffer[4] = songNumber;
  sendBuffer[5] = 0xEF;
  sendUARTCommand();
}
//play a sound at a set volume, only seems to apply to root directory files
void playSoundAtVolume(byte volume,byte songNumber){
  sendBuffer[0] = 0x7E;
  sendBuffer[1] = 0x04;
  sendBuffer[2] = 0x31;
  sendBuffer[3] = volume;
  sendBuffer[4] = songNumber;
  sendBuffer[5] = 0xEF;
  sendUARTCommand();
}
//loop sound 7E 04 33 00 01 EF
void repeatSound(byte songNumber){
  sendBuffer[0] = 0x7E;
  sendBuffer[1] = 0x04;
  sendBuffer[2] = 0x33;
  sendBuffer[3] = 0x00;
  sendBuffer[4] = songNumber;
  sendBuffer[5] = 0xEF;
  sendUARTCommand();
}


//set the playback volume 7E 03 31 0F EF = set volume to 0x0F = 15
void setplayVolume(byte volume){
  sendBuffer[0] = 0x7E;
  sendBuffer[1] = 0x03;
  sendBuffer[2] = 0x31;
  sendBuffer[3] = volume;
  sendBuffer[4] = 0xEF;
  sendUARTCommand();
}

//stop the current track playing
void stopSound(){
  sendBuffer[0] = 0x7E;
  sendBuffer[1] = 0x02;
  sendBuffer[2] = 0x0E;
  sendBuffer[3] = 0xEF;
  sendUARTCommand();
}

void sendUARTCommand(){
  int q;
  for(q=0;q < sendBuffer[1] + 2;q++){
    myMP3.write(sendBuffer[q]);  
  }
  Serial.println("Commands Sent");
  for(q=0;q < sendBuffer[1] + 2;q++){
    Serial.println(sendBuffer[q],HEX);  
  }
  delay(25);//stops odd commands being missed
}

void setup() {
  Serial.begin(9600);
  Serial.println("UARTSerialV5");
  myMP3.begin(9600);
  delay(500);//allow everything to settle down
  //first we need to select the TF Card
  selectTFCard();
  delay(100);
  //new function to repeat the same track
  repeatSound(12);//repeat sound 12
  
}

void loop() {
 // int q;

  
  /*
  //play through the first 5 tracks on the card
  for(q=0;q<15;q++){
    playSound(q);
    delay(4000);
    stopSound();  
  }
  */
  /*
  //Change the volume and then play track 4
  for(q=0;q<30;q++){
    setplayVolume(q);//change volume
    playSound(4);//play track 4
    delay(2000);
    stopSound(); 
  }
*/
/*
  ////Play track 4 with pre set volumes
  for(q=0;q<60;q +=10){//volume incremented in steps of 10
    playSoundAtVolume(q, 4);//play track 4 at increasing volumes (q)
    delay(2000);
    stopSound();  
  }
  */
}

Additional Resource Links

Alternate Sample Serial MP3 sketch

Serial MP3 player manual with all functions listed (PDF format).

Audacity (free download).

Comments


This site has been designed to be child friendly, this means that comments cannot be added to videos or directly to the site.
To add a comment or ask a question please email the address in this image: and use Serial UART MP3 player as a reference.