Arduino nanoをソフトウェアシリアルにより接続したDFPlayer Proにより、音声メッセージを再生します。
DFPlayer Proとは
DFPlayer Proは、128 MBの内蔵ストレージを搭載したMP3プレイヤーで、Arduiono、ATコマンド、ボタン入力、ADkeyからの操作で再生できます。内蔵ストレージに保存した音楽を再生し、USB接続でPCからの音声を出力できます。DFPlayer Proの詳細については「DFPlayer Pro」に示します。
DFPlayer Proのピンアサインを次に示します。
音声メッセージの作成
読み上げたテキストをMP3やWAVで保存出来る便利なサイト「音読さん」を使って音声メッセージを作成します。作成した音声メッセージはMP3ファイルとしてダウンロードします。
DFPlayer Proへ音声メッセージの保存
PCとUSB接続すると通常のUSBメモリのようにファイルをDFPlayer Proに追加できので、作成した音声メッセージをドラッグ&コピーでDFPlayer Proに保存します。保存したファイルを次に示します。
Arduino nanoとDFPlayer Proの接続
Arduino nanoとDFPlayer ProをSoftwareSerialを使って次のように接続します。スピーカーは「Stereo Enclosed Speaker – 3W 8Ω」を使用します。SoftwareSerial機能は「ProMicroでSoftwareSerialを使ってESP8266と接続」でも使用しています。
| Arduino nano | DFPlayer Pro | Stereo Enclosed Speaker |
|---|---|---|
| GND | GND | |
| 5V | VIN | |
| D10 | RX | |
| D11 | TX | |
| L- | 赤線 | |
| L+ | 黒線 |
接続した画像を次に示します。
音声メッセージ再生ソフトの作成
音声メッセージ再生ソフトは、DFPlayer Proライブラリを使って、スケッチ例をベースに作成します。
メニューから「スケッチ」→「ライブラリをインクルード」→「ライブラリを管理」を開き、次のように「DFRobot_DF1201S」 をインストールします。
作成するスケッチは 、メニューから[ファイル | →「スケッチ例」→「DFRobot_DF1201S」 にある「play」 をベースにします。
スケッチ例をArduino nanoのSoftwareSerialのピン番号(10,11)に合わせます。変更したスケッチを次に示します。
FireBeetle_play.ino
/*!
*@file play.ino
*@brief Music Playing Example Program
*@details Experimental phenomenon: control MP3 play music, obtain song information
*@copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
*@license The MIT license (MIT)
*@author [fengli](li.feng@dfrobot.com)
*@version V1.1
*@date 2021-10-15
*@url https://github.com/DFRobot/DFRobot_DF1201S
*/
#include <DFRobot_DF1201S.h>
#include <SoftwareSerial.h>
SoftwareSerial DF1201SSerial(10, 11); //RX TX
DFRobot_DF1201S DF1201S;
void setup(void){
Serial.begin(115200);
DF1201SSerial.begin(115200);
while(!DF1201S.begin(DF1201SSerial)){
Serial.println("Init failed, please check the wire connection!");
delay(1000);
}
/*Set volume to 20*/
DF1201S.setVol(/*VOL = */20);
Serial.print("VOL:");
/*Get volume*/
Serial.println(DF1201S.getVol());
/*Enter music mode*/
DF1201S.switchFunction(DF1201S.MUSIC);
/*Wait for the end of the prompt tone */
delay(2000);
/*Set playback mode to "repeat all"*/
DF1201S.setPlayMode(DF1201S.ALLCYCLE);
Serial.print("PlayMode:");
/*Get playback mode*/
Serial.println(DF1201S.getPlayMode());
//Set baud rate to 115200(Need to power off and restart, power-down save)
//DF1201S.setBaudRate(115200);
//Turn on indicator LED (Power-down save)
//DF1201S.setLED(true);
//Turn on the prompt tone (Power-down save)
//DF1201S.setPrompt(true);
//Enable amplifier chip
//DF1201S.enableAMP();
//Disable amplifier chip
//DF1201S.disableAMP();
}
void loop(){
Serial.println("Start playing");
/*Start playing*/
DF1201S.start();
delay(3000);
Serial.println("Pause");
/*Pause*/
DF1201S.pause();
delay(3000);
Serial.println("Next");
/*Play the next song*/
DF1201S.next();
delay(3000);
Serial.println("Previous");
/*Play the previous song*/
DF1201S.last();
delay(3000);
Serial.println("Start playing");
//Fast forward 10S
DF1201S.fastForward(/*FF = */10);
//Fast Rewind 10S
//DF1201S.fastReverse(/*FR = */10);
//Start the song from the 10th second
//DF1201S.setPlayTime(/*Play Time = */10);
Serial.print("File number:");
//Get file number
Serial.println(DF1201S.getCurFileNumber());
Serial.print("The number of files available to play:");
//The number of files available to play
Serial.println(DF1201S.getTotalFile());
Serial.print("The time length the current song has played:");
//Get the time length the current song has played
Serial.println(DF1201S.getCurTime());
Serial.print("The total length of the currently-playing song: ");
//Get the total length of the currently-playing song
Serial.println(DF1201S.getTotalTime());
Serial.print("The name of the currently-playing file: ");
//Get the name of the playing file
Serial.println(DF1201S.getFileName());
delay(3000);
//Play the file No.1, the numbers are arranged according to the sequence of the files copied into the U-disk
DF1201S.playFileNum(/*File Number = */1);
//Play the test.mp3 file in test folder
//DF1201S.playSpecFile("/test/test.mp3");
while(1);
/*Delete the currently-playing file */
//DF1201S.delCurFile();
}
音声メッセージ再生ソフトの実行
作成した音声メッセージ再生ソフトを実行すると、作成した3種類の音声メッセージが順次スピーカーに再生され、シリアルモニタに次のように表示されます。






