Thursday, 5 November 2015

How to play soundtracks with Catalex Serial MP3 Player


It took me long time (few hours a day for few days) to find out how this mp3 works. Let me tell you what really happened. I just wanted to play some sound tracks on command in Arduino, so I thought let me buy a serial mp3 player and put all the sound tracks in a micro SD card and then play them on command which ever I want.
Ad

That's why I bought the mp3 player above, but unfortunately they didn't send an user manual with it. So I started searching on the net as usual, but couldn't find it, after looking for long time finally I've found one, but the manual was so difficult to understand that I thought what's the point making this kind of manual if people can't understand easily.

Then I turned to one of my Arduino expert friend and asked him for help, but he was kind of busy and was not replying my massage for few days. So I thought you know what let me just try myself and experiment with the code, and then let's see what happens. So after working hard I came up with this simple sketch with few useful options.

Let's see some specification:


What to do?
1. Format your SD card @ Fat16 or Fat32

2. Rename all the sound tracks as 001, 002, 003 and so on. If you want to remember which track is which, then write the name after the numbers, for example 001soundofbird, 002dogbark and so on.
One thing keep in mind that when you write the name, never leave a space between the words, for example 001 sound of bird002 dog bark and so on.

3. Copy  those sound tracks and paste on the card. Remember not to make any folder/directory in SD card. Please see my other post if you want to play songs from other folders.


What does the sketch below can do?
  • If you have uploaded the sketch successfully then it will play the first sound track when you first open your serial monitor or press the reset button.
  • Play  soundtrack on command . That means it will play which ever soundtrack you tell it to play.
  •  Pause, it can pause the playing soundtrack.
  • Play, it will play the soundtrack after pausing.
  • Play next soundtrack.
  • Play previous soundtrack.

Ad

The sketch
#include <SoftwareSerial.h>
#define ARDUINO_RX 5//should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 6//connect to RX of the module
SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);
static int8_t Send_buf[8] = {0} ;

#define CMD_SEL_DEV 0X09
#define DEV_TF 0X02
#define CMD_PLAY_W_VOL 0X22
#define CMD_PLAY 0X0D
#define CMD_PAUSE 0X0E
#define CMD_PREVIOUS 0X02
#define CMD_NEXT 0X01

void setup()
{
    mySerial.begin(9600);
          Serial.begin(9600);
      delay(500);//Wait chip initialization is complete
      sendCommand(CMD_SEL_DEV, DEV_TF);//select the TF card 
      delay(200);//wait for 200ms
      sendCommand(CMD_PLAY_W_VOL, 0X1E01);//play the first song with volume 30 class
}
String str;
void loop() {
if(Serial.available())
{
str = Serial.readStringUntil('\n');

if(str == "2")
{
sendCommand(CMD_PLAY_W_VOL, 0X1E02);//play the second track with volume 30 class
Serial.println("Second sound track.");
}
if(str == "3")
{
sendCommand(CMD_PLAY_W_VOL, 0X1E03);//play the third track with volume 30 class
Serial.println("Third sound track.");
}
if(str == "4")
{
sendCommand(CMD_PLAY_W_VOL, 0X1E04);//play the forth track with volume 30 class
Serial.println("Forth sound track.");
}
if(str == "ps")
{
sendCommand(CMD_PAUSE, 0X0E);//pause the playing track
Serial.println("Pause");
}
if(str == "pl")
{
sendCommand(CMD_PLAY, 0X0D);//play it again
Serial.println("Play");
}
if(str == "pr")
{
sendCommand(CMD_PREVIOUS, 0X02);//play previous track
Serial.println("Playing previous track.");
}
if(str == "nx")
{
sendCommand(CMD_NEXT, 0X01);//play next track
Serial.println("Playing next track.");
}
 }
}

void sendCommand(int8_t command, int16_t dat)
{
  delay(20);
  Send_buf[0] = 0x7e; //starting byte
  Send_buf[1] = 0xff; //version
  Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
  Send_buf[3] = command; //
  Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback
  Send_buf[5] = (int8_t)(dat >> 8);//datah
  Send_buf[6] = (int8_t)(dat); //datal
  Send_buf[7] = 0xef; //ending byte
  for(uint8_t i=0; i<8; i++)//
  {
    mySerial.write(Send_buf[i]) ;
  }
}

What are the  commands in this sketch?
  • If you send 2, 3 or 4 via the serial monitor then it will play the second sound track, third sound track or forth sound track. What ever number you choose it will play that track and the text will appear on monitor that which track is playing. For example if you send 3 then it will play the 3rd track and you will see "Third sound track." on the serial monitor.
  • ps is for pause. If you want to pause any sound track which playing right now, then just send ps and it will pause straight way.
  • pl is for play. If you want to play it again after pausing then send pl it will start playing the same track again from where you left from.
  • pr is to play the previous track.
  • nx is to play next track.

Which Arduino board does it support?
Well, in the manual it says "Compatible with Arduino UNO / Leonardo / Mega2560 / DUE" but I have tried with Arduino Nano and it's works perfectly, so I hope it will work with other board too.

Places to edit:
* If you don't want to hear the first sound track automatically, when ever you open the serial monitor or press the reset button, then delete this (sendCommand(CMD_PLAY_W_VOL, 0X1E01);//play the first song with volume 30 class) line from the sketch.

*
if(str == "2")
{
sendCommand(CMD_PLAY_W_VOL, 0X1E02);//play the second track with volume 30 class
Serial.println("Second sound track.");
}
As you can see number 2 in "2" which you can change to somthing else to play second sound track. For example "track 2" or "my love" in which ever way it will play the same soundtrack and that is soundtrack number two. You can change the serial print "Second sound track." to what ever you want.

Note: You can do this to other functions too. For example ps, pl, pr, nx and so on.
 
* Choosing sound track to play.  For this simply change the number at th end of this (0X1E02).

* Play with volume. Unfortunately I only know two level of volume in this 15 and 30 the code is below.
sendCommand(CMD_PLAY_W_VOL, 0X0F01);//play the first song with volume 15 class
sendCommand(CMD_PLAY_W_VOL, 0X1E01);//play the first song with volume 30 class

Can you send commands by using mobile phone? 
Of course you can. There is an app called BlueSerial Beta which is very useful, I use a lot.

That's all for now, if I find out more options I will try to add on and in the meantime you can let me know if you didn't understand anything in this post or if you have found out some new function code. Thanks a lot. :)
Ad



43 comments:

  1. Thank you for the sketch. The sketch works well on both the Arduino UNO and the WeMos D1 R2 as long as you change the RX=5 to RX=D5 and TX=6 to TX=D6, but how can you list file names from the Catalex SD MP3 serial card?

    ReplyDelete
    Replies
    1. Thank you very much for leting us know it works on Arduino UNO and WeMos, but I'm really sorry I couldn't get your question properly about "list file names from the Catalex SD MP3 serial card". Could you please tell me in detail if you don't mind.

      Delete
  2. A SD fileInfo/file dump/directory of mp3 files listed on the SD card embedded in the Catalex Serial MP3 Player. Just like to do a folder directory or even a root directory. Example dir c:\*.mp3.
    I've looked everywhere for this information and either I'm overlooking something or there doesn't seem to be any information as to how to list the files(e.g. song.mp3) on the SD card.

    ReplyDelete
    Replies
    1. Sorry my friend, it might have option for that but I didn't try it. I just tried one thing one thig only put all the fies togather in one folder but if you want to drill down more into it then you can download the user menual from Google. just search for "Serial MP3 Player v1.0 Manual.pdf" I hope you will find it.

      Delete
  3. Been there, did that, but thank you again, especially for responding so quickly! If I locate a solution, I'll post it since there doesn't seem to be an answer even when using google's search.

    ReplyDelete
    Replies
    1. Lol, no worries my friend. If you really need it then I can look into it again, but it might take some time though because I don't get much of free time.

      Delete
    2. @John Turcott, Ok my friend I'm not sure you will come back to see this or not but today I've managed to play sounds from different folders but the problem is that it plays the sounds very quitely. I'll still going to write a new post about it so that people can understand easily.

      Delete
  4. Thank you very much. I am sure this has saved me many hours of head banging.
    Everything worked exactly as described.

    ReplyDelete
    Replies
    1. No worries my friend, I'm glad to hear that it helped you. :)

      Delete
  5. I'm still interested and would love to see what you come up with. As far as playing complete songs(*.mp3 files), I've had no issues, however, back to the original question; it amazes me that every programmer using either the Catalex or a variation like the DFPlayer serial audio players hasn't asked about listing wav and/or mp3 files in each directory. Renaming file names to 001xxxxSoundofSilence.mp3 002xxxxblablabla.mp3 from their original names, seems like a poor method. Also reorganizing the files or file table of contents also seems primitive at best. There just doesn't seem to be a command like dir or list for these. Odd.

    ReplyDelete
    Replies
    1. I'll try to answer some of your questions in the new post, it might take some space, time and pictures so new post will be better for that I think. :)

      Delete
  6. Abul, sorry but my question wasn't how to play mp3 files because, as mentioned above, I've done all that. It was how to list the files that are located on the Catalex or DFPlayer SD serial mp3 player?

    ReplyDelete
    Replies
    1. I'm really sorry for not getting your question properly. By listing the file you meant renaming the files like birdsong to 001birdsong Then as I've already explained that that's how they made it so we can't change that function. But if you meant any function that can list your songs randomly that you choose after uploading the sketch then I'm sorry I didn't find any option but they have got cycle function which can play all the files in one folder you can try that if you want. And if you meant list the song in the sketch and play it by pushing one button then you can do that by lining up all the codes of the sound files and giving the delay time in between so that it can play the sound for that amount of time and then start another sound file.

      Delete
  7. Abul, your sketches works great and both are very clear in what they do. I'll try my question one more time, then not bother you with this again. Let's say I have qty 2 mp3 files on the Catalex or DFPlay SD card, one is named 001.mp3 and the other is 002.mp3. All I want is when I start either the ESP8266 or Arduino, that is attached to either mp3 play, is list the files that are on the SD memory card. A simple directory listing of each file name (001.mp3, 002.mp3).

    ReplyDelete
    Replies
    1. Forgive me my frind, I'm so stupid that after so many attepmts I still didn't get your question right. If I don't get your question right this time then I'm extremely sorry about that but you can ask me as many times you want I won't mind it at all rather I'll be more than hapy if someone can get benefited by me.
      So could you please let me know what's your target? Do you want to list the files only or you want to play it?

      Delete
    2. A sketch or command that list the files on the serial MP3 player.

      Delete
    3. Well, my friend what would you like to do with the list then? Actually I think you write and list something on the card not on MP3 player because this kind of MP3 players don't have memory at all (beside for the actual program maybe). I'm sorry my friend I didn't try to write anything on the SD card so I don't really know how to do it. I say things clearly if I don't know something. I hope you won't mind.

      Delete
  8. Abul, no problem, I haven't found the solution anywhere, including Youtube Google etc.

    All I really wanted a sketch to do is list the MP3 files that are on the serial MP3 player, like Serial.println(MP3[1]); etc., and there doesn't seem to be a command/method/function to do this anywhere.

    I still like and will visit your site again. Thank you for trying to resolve this issue.

    ReplyDelete
    Replies
    1. No worries my friend, you are most welcome at any time. :)

      Delete
  9. Hi ,
    Fantastic work with the Catalex sketch code and commands . Works perfectly on the Uno clone .

    Is it possible to use a PIR to trigger the "play soundtrack on command" ?

    Thanks ,
    Tom

    ReplyDelete
    Replies
    1. Hi, tom sorry for the late reply. Did you mean motion sensor by "PIR"? If yes then I've already wrote a post for that please visit my other post
      Plant asks for water when it needs and somebody around it. Thanks

      Delete
    2. Hello again ,

      Thank you for taking the time to answer my question .I looked at your plant watering thread and it has code that works for my project . Now I have another question ,if its not too much trouble ? I am using the PIR (motion sensor)to trigger the mp3 track and H Bridge to DC motor ( left to right turns with delay ) . The problem is the H Bridge delays override the MP3 time so I only get a piece of the mp3 playing until PIR senses no motion , then I get the full mp3 track . Is there a way to keep the delay times independent of each other ?

      Tom

      Delete
    3. Hi, Tom

      Well, of course you can. After detecting the motion then you can do what ever you want to. Delay, turn on/off or what ever. But I'm not aware of your project so I can't say what exactly you need to do.

      Delete
  10. For different volume, in command , replace 0x1E(which is hexadecimal and it is equal to 30 in decimal) with
    0X00 for volume=0
    0x01 for volume=1
    .
    .
    .
    0X09 for volume =9
    0X0A for volume =10
    0X0B for volume =11
    .
    .
    0X0f for volume = 15
    0X10 for volume = 16
    .
    .
    0X1E for volume =30

    ReplyDelete
  11. I would like to give you a tip.
    I followed Abul's very useful tips and instructions in the pdf manual ... but when I wanted to play 002.mp3, for example, I played 004: it did not match the file list number! After a day of testing I discovered that the files hidden by the operating system (WIN or MAC OS) are seen from the catalex and they break everything! When you made your list of files displayed in windows HID FILES and DELETE ALL !!! I hope my advice serves someone. Good luck

    ReplyDelete
    Replies
    1. Thanks for the advice I hope many people will get benefit from it, really appreciated. :)

      Delete
  12. Hi! Thanks for the sketch, It works great. Do you know how can I play files in shuffle?

    ReplyDelete
    Replies
    1. I think in the manual book (which you can find online in pdf format) you will find a command code which very easy to put in the sketch just like above. I'd have tried to find out but unfortunately I don't have much time. :( You can read All Arduino Post for more. thanks

      Delete
  13. To control volume, add these to the defines section:
    #define CMD_VOLUP 0X04
    #define CMD_VOLDOWN 0X05

    And then I stuck this code block between the tests for strings "4" and "ps":

    if(str == "d")
    {
    sendCommand(CMD_VOLDOWN, 0X05);//play the forth track with volume 30 class
    Serial.println("Volume Down.");
    }
    if(str == "u")
    {
    sendCommand(CMD_VOLUP, 0X04);//play the forth track with volume 30 class
    Serial.println("Volume Up.");
    }

    Incremental volume control. Up is "u", down is "d".

    ReplyDelete
  14. I've connected mine exactly as per the instructions and it doesn't work. I've tried two different catalex mp3 players, an arduino mega, an arduino nano and a USB to serial converter FTDI232, all without success. Nothing happens when I send the code to the catalex mp3 player. Any idea how I could debug this?

    ReplyDelete
    Replies
    1. I've been using this code since 2015 without any problem and made many cool stuffs with it. Are you using Arduino's built in serial pins? This sketch has its own serial pins which are RX for pin 5 and TX for pin 6. Read the sketch carefully again. I hope that helps you.

      Delete
  15. Yes I'm using pins 5 and 6. I'm starting to think that maybe the mp3 player that I bought is not the same as the Catalax one. It doesn't actually say Catalex anywhere, it just says "Open smart mp3 player". It's this one here:

    https://www.ebay.com.au/itm/PCB-Alloy-Plastic-UART-Serial-MP3-Music-Player-Module-w-1W-Speaker-for-Arduino/132387835323?_trkparms=aid%3D111001%26algo%3DREC.SEED%26ao%3D1%26asc%3D20160908105057%26meid%3Db56f8e45dad24cddaede4e390ce8bf20%26pid%3D100675%26rk%3D7%26rkt%3D15%26sd%3D182783476201&_trksid=p2481888.c100675.m4236&_trkparms=pageci%253A3874b8eb-c936-11e7-812a-74dbd18051fd%257Cparentrq%253Aba795ddf15f0aadc3598cb60fffe0561%257Ciid%253A1

    ReplyDelete
    Replies
    1. You got the wrong on mate and paid too much for it. Here is one you can get it from CHINA YX5300 UART Control Serial MP3 Music Player Module For Arduino/AVR/ARM/PIC

      Delete
  16. Whoops, my fault for assuming it was a Catalex. Thanks, I've purchased some of the Catalex ones now!

    ReplyDelete
    Replies
    1. For those people who also purchase the Open-Smart one like me, I've worked out how to use it. It's actually very similar to the Catalex one except the programming commands are a bit different. You can find the instructions here: www.baaqii.com/promanage/manual/AA2822.rar

      Delete
  17. I would like to solder this board to a breadboard. How are people doing this? Are you using the 4 holes on the edges to screw the mp3 player into place?

    ReplyDelete
    Replies
    1. No, my friend I didn't use any screw to hold in place, it was tight enough already.

      Delete