DFPlayer Mini with Arduino - 아두이노로 MP3 플레이하기
Raspberry Pi & Arduino 2020. 11. 20. 15:06 |DFPlayer Mini MP3 Player For Arduino를 이용해 아두이노로 MP3 파일을 재생할 수 있다.
마이크로 SD 카드보다 약간 크다.
DFPlayer Mini Pin Map
Pin Description
배터리, 스피커, 푸시버튼을 연결해 아두이노 없이도 사용할 수 있다.
Specifications
supported sampling rates (kHz): 8/11.025/12/16/22.05/24/32/44.1/48
24 -bit DAC output, support for dynamic range 90dB, SNR support 85dB
fully supports FAT16, FAT32 file system, maximum support 32G of the TF card, support 32G of U disk, 64M bytes NORFLASH
a variety of control modes, I/O control mode, serial mode, AD button control mode
advertising sound waiting function, the music can be suspended. when advertising is over in the music continue to play
audio data sorted by folder supports up to 100 folders, every folder can hold up to 255 songs
30 level adjustable volume, 6 -level EQ adjustable
위 다이어그램과 같이 연결한다. MP3 파일 재생시 노이즈가 심하다면 RX에 1KΩ 저항을 연결한다.
아두이노IDE - Library manager에서 DFRobotDFPlayerMini를 검색하고 설치한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | /*************************************************** DFPlayer - A Mini MP3 Player For Arduino <https://www.dfrobot.com/product-1121.html> *************************************************** This example shows the basic function of library for DFPlayer. Created 2016-12-07 By [Angelo qiao](Angelo.qiao@dfrobot.com) GNU Lesser General Public License. See <http://www.gnu.org/licenses/> for details. All above must be included in any redistribution ****************************************************/ /***********Notice and Trouble shooting*************** 1.Connection and Diagram can be found here <https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299#Connection_Diagram> 2.This code is tested on Arduino Uno, Leonardo, Mega boards. ****************************************************/ #include "Arduino.h" #include "SoftwareSerial.h" #include "DFRobotDFPlayerMini.h" SoftwareSerial mySoftwareSerial(10, 11); // RX, TX DFRobotDFPlayerMini myDFPlayer; void printDetail(uint8_t type, int value); void setup() { mySoftwareSerial.begin(9600); Serial.begin(115200); Serial.println(); Serial.println(F("DFRobot DFPlayer Mini Demo")); Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)")); if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3. Serial.println(F("Unable to begin:")); Serial.println(F("1.Please recheck the connection!")); Serial.println(F("2.Please insert the SD card!")); while(true){ delay(0); // Code to compatible with ESP8266 watch dog. } } Serial.println(F("DFPlayer Mini online.")); myDFPlayer.volume(20); //Set volume value. From 0 to 30 myDFPlayer.play(1); //Play the first mp3 } void loop() { static unsigned long timer = millis(); if (millis() - timer > 30000) { timer = millis(); myDFPlayer.next(); //Play next mp3 every 30 second. } if (myDFPlayer.available()) { printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states. } } void printDetail(uint8_t type, int value){ switch (type) { case TimeOut: Serial.println(F("Time Out!")); break; case WrongStack: Serial.println(F("Stack Wrong!")); break; case DFPlayerCardInserted: Serial.println(F("Card Inserted!")); break; case DFPlayerCardRemoved: Serial.println(F("Card Removed!")); break; case DFPlayerCardOnline: Serial.println(F("Card Online!")); break; case DFPlayerUSBInserted: Serial.println("USB Inserted!"); break; case DFPlayerUSBRemoved: Serial.println("USB Removed!"); break; case DFPlayerPlayFinished: Serial.print(F("Number:")); Serial.print(value); Serial.println(F(" Play Finished!")); break; case DFPlayerError: Serial.print(F("DFPlayerError:")); switch (value) { case Busy: Serial.println(F("Card not found")); break; case Sleeping: Serial.println(F("Sleeping")); break; case SerialWrongStack: Serial.println(F("Get Wrong Stack")); break; case CheckSumNotMatch: Serial.println(F("Check Sum Not Match")); break; case FileIndexOut: Serial.println(F("File Index Out of Bound")); break; case FileMismatch: Serial.println(F("Cannot Find File")); break; case Advertise: Serial.println(F("In Advertise")); break; default: break; } break; default: break; } } |
위 소스를 컴파일하고 업로드하면 모든 노래가 30초씩 플레이된다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | bool begin(Stream& stream, bool isACK = true, bool doReset = true); bool waitAvailable(unsigned long duration = 0); bool available(); uint8_t readType(); uint16_t read(); void setTimeOut(unsigned long timeOutDuration); void next(); void previous(); void play(int fileNumber=1); void volumeUp(); void volumeDown(); void volume(uint8_t volume); void EQ(uint8_t eq); void loop(int fileNumber); void outputDevice(uint8_t device); void sleep(); void reset(); void start(); void pause(); void playFolder(uint8_t folderNumber, uint8_t fileNumber); void outputSetting(bool enable, uint8_t gain); void enableLoopAll(); void disableLoopAll(); void playMp3Folder(int fileNumber); void advertise(int fileNumber); void playLargeFolder(uint8_t folderNumber, uint16_t fileNumber); void stopAdvertise(); void stop(); void loopFolder(int folderNumber); void randomAll(); void enableLoop(); void disableLoop(); void enableDAC(); void disableDAC(); int readState(); int readVolume(); int readEQ(); int readFileCounts(uint8_t device); int readCurrentFileNumber(uint8_t device); int readFileCountsInFolder(int folderNumber); int readFileCounts(); int readFolderCounts(); int readCurrentFileNumber(); |
그 외 다른 기능은 DFRobotDFPlayerMini.h를 참고한다.
※ 제작사 홈페이지를 보면 아래와 같은 문구가 있다.
NOTE: The order you copy the mp3 into micro SD card will affect the order mp3 played, which means play(1) function will play the first mp3 copied into micro SD card.
파일명을 0001.mp3, 0002.mp3... 이런식으로 변경해서 SD카드에 넣어도 play(1) 함수 실행 시 0001.mp3가 아닌, 제일 먼저 복사된 파일이 첫 번째로 재생되는거 같다.
'Raspberry Pi & Arduino' 카테고리의 다른 글
Read and Write Radio-Frequency IDentification(RFID) cards with Arduino using RC522 (0) | 2020.12.16 |
---|---|
RF 433MHz Transmitter and Receiver with Arduino - 아두이노로 RF 433MHz 무선 통신 하기 (4) | 2020.11.24 |
ILI9341 TFT LCD with Arduino - 아두이노로 ILI9341 사용하기 (2) | 2020.11.19 |
MX1508 Motor Driver - MX1508 모터 드라이버 (0) | 2020.11.18 |
NodeMCU(ESP-12E/ESP8266) LED Control Server Programming (0) | 2020.11.15 |