Example Code for Arduino-Playback

Last revision 2025/12/19

The article provides example code for Arduino playback, enabling users to control music files by playing, pausing, and navigating tracks, perfect for those learning Arduino programming.

Hardware Preparation

Software Preparation

Wiring Diagram

Connection

Other Preparation Work

It is recommended to use hardware serial to drive this module, since it may be unstable when using software serial, which may result in unknown errors.It is suggested to reduce the module's baud rate to 9600 when using software serial driver. Delay 2s for the BT401 to start before initializing.

Sample Code

/*!
 * @file playMusic.ino.ino
 * @brief Play song in TF card 
 * @n Experiment Phenomenon:play music and do the related actions 
 * @copyright  Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [Eddard]([email protected])
 * @version  V1.0
 * @date  2020-12-29
 * @get from https://www.dfrobot.com
 * @url https://github.com/cdjq/DFRobot_ID809
*/

#include <DFRobot_BT401.h>
#include <SoftwareSerial.h>

SoftwareSerial btSerial(2, 3);  //RX TX

DFRobot_BT401 bt;

void setup(){
  btSerial.begin(115200);
  /*Delay 2s for the BT401 to start*/
  delay(2000);
  while(!bt.begin(btSerial)){
    Serial.println("Init failed, please check wire connection!");
    delay(1000);
  }
  bt.setVOl(30);
  bt.switchFunction(bt.eTFCard);
  bt.setPlayMode(bt.eDeviceCycle);
}

void loop(){
  //Play
  bt.playControl(bt.ePlay);
  delay(3000);
  //Pause 
  bt.playControl(bt.ePause);
  delay(3000);
  //Next 
  bt.next();
  delay(3000);
  //Last 
  bt.last();
  delay(3000);
  //Play song in the first file
  bt.playFileNum(1);
  while(1);
  /*Delete the currently playing file*/
  //bt.delCurFile();
}

Result

The module will enter music mode on power-up, play the last recorded file, pause after 3 seconds, play the next song after another 3 seconds, play the previous song after 3 seconds, and finally play the first file copied into the device.

Additional Information

Ensure the TF card has music files and is properly inserted before powering on the module.

Was this article helpful?

TOP