2015/03/01

Arduino - DFPlayer Mini MP3 Module


Arduino - Test DFPlayer Mini MP3 Module



I found DFPlayer mini MP3 module on China auction site Taobao when I was looking for a MP3 player module for an Arduino projects.  This module is quite small and it only require few pins connections to Arduino and a speaker to work.  Looks really nice for my little project.

Actually, the module is quite capable and complicated, if you were interested, please visit this page.

This tutorial here is just to show you how to connect DFPlayer module with Arduino and speaker and the Arduino sketch provided here just to play few sample sounds I collected from Internet.

First, I recycled a 2W, 4Omh small speaker from this little music box.


Material Needed:
  1. DFPlayer Mini module 
  2. Speaker - DFPlayer module support to to 3W
  3. SD card - 2GB ~ 32GB formatted with FAT or FAT32
  4. MP3 / WAV - audio files
  5. Arduino mini Pro
  6. Few jump wires
As you can see the illustration below I made in Power Point, hardware connection is quite simple
(Forget about the cross lines...these lines are NOT supposed to be crossed.. :)
* Note: You have to separate DFPlayer Mini Module power from Arduino power or you will hear very loud noises (It took me two days to figure out this!)

***********
UPDATE:  Someone left a message in this blog below, please try it out!

Hello everybody! There is another solution for getting rid of noise. Just put down DC voltage to 4.0 - 4.3 Volts. Arduino is still able to operate and DFplayer recommends to use 4.2V, my experiments prove this.

***********

Here is a photo of my Arduino with DFPlayer Mini connection:



*Important note for audio files. Careful read notes below will save you a lot of precious time!
  • DFPlayer Mini read both MP3 and WAV files.
  • All audio files must be placed under /mp3 folder.
  • Audio file must be named 0001.mp3, 0002.mp3.....0255.mp3 (4 letters not 2 or 3 letters)
  • Audio file names can have any characters after 4 digits, such as 0001_Bruno_Mars_Count_On Me.mp3 
  • Tested few under 1 second WAV files and they did not work
  • If one of the audio file is removed from SD cared, DFPlayer still can find it and play it! Weird! In the end, I formatted the SD card to make DFPlayer to play the music I specified.
  • Delay(seconds); You have to specify number of seconds delay for the audio file to play completely or the next audio file might be started before current audio is over!
  • mp3_play (74); means play audio file 0074.mp3
The code below is from the vendor's website and it worked without much modification.

Programming Code here:


/*
 * Copyright: DFRobot
 * name:  DFPlayer_Mini_Mp3 sample code
 * Author:  lisper 
 * Date:  2014-05-30
 * Description: sample code for DFPlayer Mini, this code is test on Uno
 *   note: mp3 file must put into mp3 folder in your tf card
 */

#include <softwareserial.h>
#include <dfplayer_mini_mp3.h>

void setup () {
 Serial.begin (9600);
 mp3_set_serial (Serial); //set Serial for DFPlayer-mini mp3 module 
 mp3_set_volume (10);
}

void loop () {        

 mp3_play (1); //play 0001.mp3
 delay (10000); //10 sec, time delay to allow 0001.mp3 to finish playing

 mp3_play (2);
 delay (5000);

 mp3_play (5);
 delay (5000);

 mp3_play (20); //play 0020.mp3
 delay (9000);

 mp3_play (81);
 delay (6000);

 mp3_play (74); //play 0074 mp3
 delay (6000);

}
Here is the video:
Done.

Arduino - 測試 DFPlayer Mini MP3 播放模組

最近在大陸拍賣網上找到一個 Arduino MP3 播放模組(DFPlayer Mini),它支援最大32GB SD卡,又可以由 Arduino 下個  Play 的指令就能由SD 卡上播放 MP3 & WAV檔案。加上體積很小一片又只要連接幾支腳位到 Arduino 就能運作了,二話不說就淘了兩片來試試它的功能。

DFPlayer Mini 它的功能真的很多, 有興趣的朋友可以到這裡閱讀 DFPlayer Mini 的介紹!

這個測試主要是測試 DFPlayer Mini MP3 播放模組的功能,所以只需要下列的東西即可:

  1. Arduino mini Pro
  2. DFPlayer Mini 模組 
  3. Speaker - 最高支援到 3W
  4. SD card - 2GB ~ 32GB 以 FAT or FAT32 格式化
  5. 洞洞板 / 跳線
  6. MP3 / WAV 格式的音效 or 音樂檔案

手上剛好有一個壞掉的音樂播放器,我把喇叭拆下來看了一下,剛好是 2W的,可以拿來用。


下方是我自己在 Power Point 上畫的線路示意圖:
(Forget about the cross lines...these lines are NOT suppose to be crossed.. :)
* 注意: 不要直接把 Arduino 的 5V 給 DFPlayer Mini 使用,一定要分別供給 DFPlayer Mini 電源,不然喇叭會發出很大的雜音,我花了兩天才發現這個問題。

以下照片是我實際連接的樣子:

* 以下是使用 DFPlayer Mini Module 的注意事項,請仔細閱讀,可以幫你節省很多時間!

  • DFPlayer Mini 可以讀取 MP3 & WAV 檔案格式,但試過檔案長度1秒以下無法播放。
  • 所有音樂檔案一定要放在 /mp3 資料夾裡
  • 音樂檔案名稱一定要使用 4 個字,如 0001.mp3, 0002.mp3.....0255.mp3 
  • 音樂檔案超過4個字後則無限制,像是 0001_Bruno_Mars_Count_On Me.mp3 是沒問題的。
  • 如果你從 SD 卡上刪掉某個檔案,DFPlayer Mini 還是有辦法把它播出來,好怪!後來我從新格式化 SD 卡才成功播放我要的音樂檔案。
  • Delay(sec); 這個指是是告訴 Arduino 你要播放音樂的長度,如果你給的秒數不足,而下個指令又是要播另一首,則 Arduino 就會把目前播放中斷,而繼續播下一首。
  • mp3_play (74); 這個指令是播放 0074.mp3 這個音樂檔。

Arduino 程式碼請參考上面。

請看下方實際運作影片:
完成。

46 comments:

  1. Hello Stones!

    Nice work, I like the case you build, especially the input console. Actually I'm currently working on almost the same project, except that I'm using an additional LCD display.

    However, your blogpost is the only "real project" page I found, so there is my question: Did you manage to tell the DFPlayer to play the whole folder? I've added the method "void mp3_playback_mode (uint16_t mode)" to the library (no effect). I only get the player to repeat the first track. Adding delays with the calculated length of the tracks and invoke "play" every time seems to me not very user friendly.

    Best regards,
    Matthias (Germany)

    ReplyDelete
    Replies
    1. I think that's the limitation of this DFPlayer Mini module. I've written an email to ask DFRobot on what command can I use to tell an audio play length, but no reply yet.

      Delete
    2. Ah, thank you for the info. I'm gonna try to use the status commands, but no results yet. I hope that I can determine somehow if the player is playing so I can invoke next() depending on the state.

      Delete
    3. +heymannstrasse7, if.you.ever.found the solution, please share it with us, thank you!

      Delete
    4. This comment has been removed by the author.

      Delete
    5. Hey Stonez,

      I've not testet it yet, but I found a very promising (and obvious) solution here:

      http://www.trainelectronics.com/Arduino/MP3Sound/TalkingTemperature/

      The guy uses the BUSY pin of the DFPlayer. He reads the digital state of it with a delay of 20 milliseconds.
      According to the website, the state changes once the mp3 playback has finished, so you can manually increase the playback counter afterwards.

      I have to rewire my player for this and will check it out the next days...

      Delete
    6. Ok, I got it working. I connected the BUSY pin with an analog pin of my Arduino and added the following check in my loop:

      void checkPlayState() {
      if(playing) {
      delay(20);
      int playState = digitalRead(PLAY_BUSY_PIN);
      //is not playing?
      if(playState == 1) {
      Serial.println("Playing next...");
      playNext();
      //give him some time to update the busy state
      delay(200);
      }
      }
      }

      I hope that works for you too.

      Best regards,
      Matthias

      Delete
    7. +heymannstrasse7 Thanks for this! I will give it a try over the weekend!

      Delete
    8. hi i tested this on my uno with an analog pin wich uses analogRead() and i got 1023 when there was nothing played. so every time i saw that and the variable (in your case 'playing') was true i would continue playining the next one also tested it on a pwm pin but didnt get the right results. Hope this helped any one in any way.

      Delete
  2. Hi Stones,
    你的blog寫的非常用心, 非常有幫助. 我也在用FDPlayer mini做project, 但是當我load你的code的時候(或者官方的examples的時候), IDE告訴我"error: 'mp3_set_serial' was not declared in this scope.", 請問你有遇到過這個問題么, 或者有知道這是怎麼回事嗎?
    非常感謝,
    天宇

    ReplyDelete
    Replies
    1. 補充: 我的IDE的Board中選的是UNO.
      全部的error是:

      Build options changed, rebuilding all
      sketch_mar28b.ino: In function 'void setup()':
      sketch_mar28b.ino:66:9: error: 'Serial1' was not declared in this scope
      sketch_mar28b.ino: In function 'void print_info()':
      sketch_mar28b.ino:80:37: error: 'Serial1' was not declared in this scope
      sketch_mar28b.ino: In function 'void loop()':
      sketch_mar28b.ino:90:28: error: 'Serial1' was not declared in this scope
      Error compiling.



      再次感謝

      Delete
    2. oh never mind, 我在http://www.dfrobot.com/forum/viewtopic.php?f=8&t=975找到解決方法了, 就像他說的"I had loaded the DFPlayer library into the Arduino (core) library folder (as I have done with others). But I moved it to the Arduino (user docs) folder and compilation errors on Examples went away. Don't know why, but it works..."

      還是謝謝

      Delete
    3. Sorry, not check message very often..Yeah, sometimes the location of the library could cause the compiling error. I'm glad you found answers!

      Delete
    4. //DFMiniMp3 mp3(Serial1);

      // Some arduino boards only have one hardware serial port, so a software serial port is needed instead.
      // comment out the above definition and uncomment these lines
      SoftwareSerial secondarySerial(10, 11); // RX, TX
      DFMiniMp3 mp3(secondarySerial);

      为什么不尝试把hardware Serial注释掉,用Software Serial?
      那样问题就解决了。

      Delete
  3. Hi there,

    i am using the DFPlayer as a „extension“ of my door bell

    to isolate the power supplys, I am using a optocoupler to trigger the sound on DFPlayer (ADkey1 – “shorting” the segment 1-switch – only one MP3 on TF-Card)

    the problem is: a short push on the door bell button start’s the sound – running once = everything OK 
    but: if the “postman” is pushing the button for a longer time, the DFPlayer is going to looping the sound for ever

    any idea, how to set the DFPlayer in “single play mode” – one shoot only, independent from button time ? – without pressing other buttons, like “K1” or “stop”

    THX for feedback

    ReplyDelete
    Replies
    1. Hi Michael, I haven't try this myself yet, but I suggest you look at the "software debounce" from Arduino.cc here: http://playground.arduino.cc/Learning/SoftwareDebounce

      You should be able to filter out the longer push by debounce.
      Let me know, if you were able to pull it off! :)

      Delete
  4. Hi Stonez,
    and THX for your reply.
    there is no Arduino in the game. i am using the DFPlayer "stand alone". so, no way for "software debouncing"

    ReplyDelete
    Replies
    1. Hi Michael,
      I am not good at electronic hardware, but you might want to search "hardware debounce" in many online tutorials. Few said, it only required a capacitor and two resistors.

      Let me know if really make it work! :)

      Delete
    2. Try to connect your button trough capacitor, it will produce small impulse for every click but not for long press. Maybe 1uF will work. Probably you will need to use transistor to amplify impulse from resistor-switch-capacitor, but it is very easy to produce impulse. Otherwise, you can use an 74HCxx to produce impulse instead of long press.

      Delete
  5. Can i use it with Pic/AVR

    ReplyDelete
    Replies
    1. Sorry, I can answer this question as this is beyond my knowledge....

      Delete
  6. Can the DFPlayer and the Arduino use the same power supply - as long as I'm not powering the player through the Arduino?

    ReplyDelete
    Replies
    1. Someone left a message in the blog: Try it out:

      There is another solution for getting rid of noise. Just put down DC voltage to 4.0 - 4.3 Volts. Arduino is still able to operate and DFplayer recommends to use 4.2V, my experiments prove this.

      Delete
    2. I actually broke an arduino nano by powering the mp3player from it

      Delete
  7. Nice tutorial and many many thanks for this .

    I've a problem , you said "You have to separate DFPlayer Mini Module power from Arduino power or you will hear very loud noises " , but I want to operate this mp3 module by arduino with single battery . How to separate ground in this case ?

    ReplyDelete
    Replies
    1. Someone left a message in the blog: Try it out:

      There is another solution for getting rid of noise. Just put down DC voltage to 4.0 - 4.3 Volts. Arduino is still able to operate and DFplayer recommends to use 4.2V, my experiments prove this.

      Delete
  8. Hello everybody! There is another solution for getting rid of noise. Just put down DC voltage to 4.0 - 4.3 Volts. Arduino is still able to operate and DFplayer recommends to use 4.2V, my experiments prove this.

    ReplyDelete
    Replies
    1. Hi, thanks for this solution!

      Delete
  9. Hi, I am having an issue getting mine to work at all. The weird thing is, when I don't connect the power to the unit and no resistors I get a buzzing sound. When I connect the 1k resistors the sound stops but nothing plays (didn't play during buzzing either). If I connect the power it won't play or buzz...odd...

    can you suggest what i am doing wrong?

    https://www.dropbox.com/s/gbzmzfsmis2dnd6/IMG_2741.MOV?dl=0

    ReplyDelete
  10. Hi!!
    I've tried uploading your codes to my uno board but it says theres an error : mp3codes:23: error: 'mp3_play' was not declared in this scope

    please let me know what I've done wrong here
    I'm very new to arduino
    I'm sorry if my question is stupid :(
    joanne

    ReplyDelete
    Replies
    1. Hi,
      Usually, this kind of issues are due to the compiler couldn't find the .h files.

      Please ensure the library "dfplayer_mini_mp3.h" is located in the right directory.

      Stoenz

      Delete
  11. hi i have found out that if you use rx and tx for the comands then you dont need to include software serial library. I wil also post any other things i find in here sinds this seems to be the only place for information aside from the main information page.

    ReplyDelete
    Replies
    1. this guy found all the commands there are some very interesting ones 2: http://forum.banggood.com/forum-topic-59997.html
      i hope the links work for you

      Delete
  12. Dear Stonez,
    感謝你的用心的文章,對我使用這塊模組有非常大的幫助。

    我使用時遇到一個非常困擾的問題,當使用行動電源提供模組5V電壓時,
    模組上的指示燈與音樂時有時無,以為是電源問題,但換使用Power Supply仍還是這樣。

    偶爾插拔SD card或是等待一段時間,模組指示燈又亮,音樂也開始播放了。

    猜測是電路接觸不良,但後來換別個也是如此,想請問各位在使用時是否有這樣問題,感謝!


    Thank you for your awesome article.

    I have a problem when I use this module. I use 5V power bank or power supply as DFPlayer external power. The LED on the module and speaker don't work all the times.

    I wire my arduino nano and DFPLAYER like the diagram in your article. And I use a 8 ohm, 0.5W speaker.

    Thank all of you for your help.

    ReplyDelete
    Replies
    1. Yes, I did encountered similar issues with what you've described during the testing.
      Sorry, I do not know why, but do you want to solder wires to the Arduino module to prevent the pin contact issues first?

      Delete
    2. Thank you and sorry for my late reply.

      To avoid the serial port disturbing when arduino connect with PC, I used SoftwareSerial.h to modify the demo code to use pin D8, D9 for RX, TX instead of the default RX, TX pin. And I add a 1k ohm resister between arduino's RX, TX and DFPlayer's Tx,Rx.

      It really works for one of my DFPlayers. But I broke the other one in the same way, it comes to smoke LOL

      I'll solder wires in the near future.

      Delete
  13. This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

    Love from Pprogramming

    ReplyDelete
    Replies
    1. Thanks for your compliments! By writing these tutorials, I hope Arduino beginners can have something to start with! :-)

      Delete
  14. how to connect dfplayer with amplifier

    ReplyDelete
  15. Nice blog. As it is the discussion of speakers I want to say something about klipsch speakers which are of high quality and provide great sound quality. And recently I came to know about branhamsound.com which is a great dealer of it. One week ago I contacted with them for one speaker. They not only provide these speakers but also help them to install at an affordable price.

    ReplyDelete
    Replies
    1. Thank you for your information!

      Delete
  16. не рабочий скейч или библиотека. Пишет,ошибка компиляции.

    ReplyDelete
    Replies
    1. Please double check and be very careful where you place the library files or this tutorial will not work. Good Luck!

      Delete
  17. This comment has been removed by a blog administrator.

    ReplyDelete
  18. hi guys,

    I bought 5pcs of DF player from TAOBAO but the led dint light on.
    But my previous DF player i bought from other seller it can work.
    So i would like to ask any other way to test the df player still can work or not ?
    Sorry for my bad english.

    ReplyDelete
    Replies
    1. It's really hard to tell, but I suggest you try the simplest connection to check whether DFPlayer is functioning.

      Delete