Example Code for Arduino-Music Play
Last revision 2026/01/26
This article guides you through setting up an Arduino-based music player using FireBeetle ESP32, including necessary hardware, software, and example code for controlling audio playback.
Hardware Preparation
- FireBeetle ESP32 IoT Microcontroller(SKU: DFR0478) ×1
- Camera & Audio Media Board - FireBeetle Covers(SKU: DFR0498) ×1
- Micro SD Card (FAT32 File System) ×1
- USB Cable ×1
Software Preparation
- Download Arduino IDE: Click to Download Arduino IDE
- Download Arduino Library: Click to download DFRobot_IIS and refer to the guide: How to Install a Library?
Other Preparation Work
- This media board only supports FireBeetle ESP32 main board at present, for other main boards do not equipped with IIS interface.
- The flat ribbon cable of camera is relatively easy-to-break, please be careful and light in connection. The camera shot must be opposite to the SD card slot.
- Do not support video shooting and transmission.
Download programs as below to ESP32 main control board and plug a SD Card with test1.wav and test2.wav.
Sample Code
The Module would operate as follows when pressed: play test1.wav>>pause>>continue>>mute>>Volume:50>>stop>>play test2.wav.
/*!
* @file DFRobot_IIS.h
* @brief DFRobot's IIS Player Module
* @n IIS Module for how to begin to play a WAV file,how to excute orders pause,continue,stop and play the next
* Insert sd card with test1.wav and test2.wav.
* Call the function by pressing user key to control music player
* The Module would operate as follows when pressed: play test1.wav>>pause>>continue>>mute>>Volume:50>>stop>>play test2.wav
*/
#include <Wire.h>
#include "DFRobot_IIS.h"
DFRobot_IIS iis;
const int buttonPin = 16;
int i=0;
void setup() {
Serial.begin(115200);
iis.SDCardInit(); // Init SD card
iis.init(AUDIO); // Init Audio mode
iis.setHeadphonesVolume(50); // Set Headphones Volume from 0 to 99
iis.setSpeakersVolume(50); // Set Speakers Volume from 0 to 99
iis.initPlayer(); // Init Music player
Serial.println("Init player");
iis.playMusic("/test1.WAV"); // Choose music file
Serial.println("Ready to play");
delay(500);
}
void loop(){
static unsigned long timepoint = millis();
static byte count = 0;
if(millis()-timepoint > 200){
if((!digitalRead(buttonPin))&&(i==0||i==2)){
timepoint = millis();
iis.playerControl(PLAY); // Start or continue playing Music
Serial.println("play");
i++;
}else if((!digitalRead(buttonPin))&&i==1){
timepoint = millis();
iis.playerControl(PAUSE); // Pause playing
Serial.println("pause");
i++;
}else if((!digitalRead(buttonPin))&&i==3){
timepoint = millis();
iis.muteSpeakers(); // Mute mode
iis.muteHeadphones();
Serial.println("mute mode");
i++;
}else if((!digitalRead(buttonPin))&&i==4){
timepoint = millis();
iis.setHeadphonesVolume(50);
iis.setSpeakersVolume(50);
Serial.println("Volume=50");
i++;
}else if((!digitalRead(buttonPin))&&i==5){
timepoint = millis();
iis.playerControl(STOP); // Stop playing
Serial.println("Stop");
i++;
}else if((!digitalRead(buttonPin))&&i==6){
timepoint = millis();
iis.playMusic("/test2.WAV"); // Change music file
iis.playerControl(PLAY); // Play test2.wav
i=1;
}
delay(100);
}
}
Result
Button triggers audio control in order, Serial prints operation status.
Functions
- Initialize SD card in the beginning.
SDCardInit():
- Enter AUDIO mode, state its mode (AUDIO / CAMERA) in use when the SD card is initialized.
init(AUDIO):
- Set earphone volume and loudspeaker volume separately. They can be used at the same time and the volume range is 0~99. It should be set before the player initialization and can be used to change volumes in the play.
setHeadphonesVolume(50),setSpeakersVolume(0):
- Mute: enable loudspeaker and earphone to Mute, available in the play.
muteSpeakers(),muteHeadphones():
- Initialize the player: to prepare for calling the player function in AUDIO.
initPlayer():
- Select a music file (test1.wav at here) that stored in the SD card and call to play.
playMusic("/test1.WAV"):
- Player control: to play, pause and stop the music play in order.
playerControl(PLAY),playerControl(PAUSE),playerControl(STOP):
Was this article helpful?
