Last Updated: 09/02/2024
#64 DY-SV5W MP3 Player ESP32 ans ESP8266 D1 Mini
Components >> #64 DY-SV5W MP3 Player ESP32 ans ESP8266 D1 Mini
DY-SV5W MP3 Player
This is an add on for the tutorial on the DY-SV5W MP3 player using an UNO and Mega 2560
This link is to the The technical specs for DY-SV5W and has the other codes for creating functions.
In this version I have changed the code to enable it to work with an ESP32 Dev Module and thge ESP8266 D1 Mini.
The main changes from the original versions are the way that the lowByte and HIghBytes are calculated and the CheckSum data type.
These are mainly precautions
as they are 32 bit boards.
Both board are using Serial Communication, the ESP32 uses Serial2, the ESP8266 uses Serial1 (A single pin as the sketch only requires the transmit).
NOTE: The DY-SV5W draws too much power to take it's 5V from the ESP32 or the ESP8266 so make sure it has it's own 5v power supply.
Connections
The Selectors on the Red switchbox should be set to:
1 = OFF
2 = OFF
3 = ON
ESP 32 Dev Module |
|
TX2 | 17 |
RX2 | 16 |
busyPin | 15 (Change as required) |
Gnd | GND |
ESP8266 D1 Mini |
|
TX1 | 2 (Marked D4) |
busyPin | 4 (Marked D2) |
GND | GND |
This is a tutorial will deal with the basics of serial communication with the DY-SV5W MP3 player.
The DY-SV5W MP3 player has advantages over the DFPlayer and the Serial MP3 player as it has a built in amplifier that gives better sound quality.
It laos has the
advantage of being able to load the files directly to the SD Card through the build in USB port.
The only disadvantage I have found is it is physically bigger.
Example 1: ESP32: ESP32DYSV5Wv7.ino
Click to Download code: ESP32DYSV5Wv7.ino
Written to run onESP32 Dev Module Using Serial2
I have only done the version 7 sketches from the previous tutorial and have left a lot of comments in so you can follow on the Serial monitor.
I have written a few functions based on the information in the data sheet. If you want more functions you can use the ones created as a basis.
/* ESP32DYSV5Wv7
Just plays track 5 with 2 sec gap after playing
ESP32 Dev Module Version
Runs off Serial2
I have left Serial Print statements running so you can track what the sketch is doing, remove for production
Player on Serial 2 (pins 16 & 17)
busyPin = 15 //receives busy signal from DY-SV5W
*/
//Serial2
#define RXD2 16
#define TXD2 17
//variables always required to work DY-SV5W
const int busyPin = 15;//change to any suitable digital pin
byte commandLength;
byte command[6];
int16_t checkSum = 0;
//my variables for demo sketch
int trackNum = 1;
int playStatus; //0 playing 1 stopped 2 waiting to start next track
unsigned long lastCheckTime;
unsigned long currentMillis;
int busyPinstate;
void playTrack(int soundTrack) {
//select track
Serial.print("soundTrack: ");
Serial.println(soundTrack);
command[0] = 0xAA;//first byte says it's a command
command[1] = 0x07;
command[2] = 0x02;
command[3] = (soundTrack>>8) & 0xFF;//snh...track HIGH bit
command[4] = soundTrack & 0xFF;//SNL... track low bit
checkSum = 0;
for (int q = 0; q < 5; q++) {
checkSum += command[q];
//Serial.print(command[q]);
//Serial.print(",");
}
//Serial.println(" ");
//Serial.println(checkSum,BIN);
//Serial.println(checkSum);
//command[5] = lowByte(checkSum);//SM check bit... low bit of the sum of all previous values
command[5] = (checkSum & 0xFF);//SM check bit... low bit of the sum of all previous values
// Serial.println(command[5],HEX);
// Serial.println(command[5],BIN);
// Serial.println(command[5]);
commandLength = 6;
sendCommand();
}
//plays whatever track has been paused or 1st track if nothing selected
//May need to be selected after putting into random mode
void play() {
command[0] = 0xAA;//first byte says it's a command
command[1] = 0x02;
command[2] = 0x00;
command[3] = 0xAC;
commandLength = 4;
sendCommand();
}
//selects random mode
void randomMode() {
command[0] = 0xAA;//first byte says it's a command
command[1] = 0x18;
command[2] = 0x01;
command[3] = 0x03;//random
checkSum = 0;
for (int q = 0; q < 4; q++) {
checkSum += command[q];
}
command[4] = (checkSum & 0xFF);//SM check bit... low bit of the sum of all previous values
commandLength = 5;
sendCommand();
//play() needs to be selected if you want the random tracks to start playing instantly
play();
}
//sets the device volume...0 - 30
void playbackVolume(int vol) {
if (vol > 30) { //check within limits
vol = 30;
}
command[0] = 0xAA;//first byte says it's a command
command[1] = 0x13;
command[2] = 0x01;
command[3] = vol;//volume
checkSum = 0;
for (int q = 0; q < 4; q++) {
checkSum += command[q];
}
command[4] = (checkSum & 0xFF);//SM check bit... low bit of the sum of all previous values
commandLength = 5;
sendCommand();
}
//sends the command
void sendCommand() {
int q;
for (q = 0; q < commandLength; q++) {
Serial2.write(command[q]);
Serial.print(" ");
Serial.print(command[q], HEX);
}
Serial.println(" End");
}
void setup() {
Serial.begin(115200);
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
Serial.println("ESP32DYSV5Wv7");
pinMode(busyPin, INPUT);//pin to read from DY-SV5W buyPin
playbackVolume(17);//sets volume to lvl 17
}
void loop() {
currentMillis = millis();//stores time in millis() for non blocking timings
busyPinstate = digitalRead(busyPin);
if (busyPinstate > 0) { //nothing playing
if (playStatus < 1) {
Serial.print(busyPinstate);
playStatus = 1;
lastCheckTime = currentMillis;//set system ready for 2 sec delay
}
}
switch (playStatus) {
Serial.print(" P: ");
Serial.println(playStatus);
case 1:
if (currentMillis - lastCheckTime >= 2000) { //2000 = 2 seconds of silence between tracks
playStatus = 2;
//Just selecting a new track
//this could have been something like
//trackNum = random(1,5);
// trackNum = 1;//fixed track number
Serial.print("trackNum: ");
Serial.println(trackNum);
playTrack(trackNum);
trackNum++;
if(trackNum > 4){
trackNum = 1;
}
}
break;
case 2:
if(busyPinstate < 1){
playStatus = 0;
Serial.print(" P: ");
Serial.println(playStatus);
}
break;
default:
break;
}
}
Example 2:ESP8266 ESP8266DYSV5Wv7.ino
Click to Download code: ESP8266DYSV5Wv7.ino
Written to run on ESP8266 D1 Mini Using Serial1 (Single Pin)
I have only done the version 7 sketches from the previous tutorial and have left a lot of comments in so you can follow on the Serial monitor.
I have written a few functions based on the information in the data sheet. If you want more functions you can use the ones created as a basis.
/* ESP8266DYSV5Wv7
Just plays track 5 with 2 sec gap after playing
ESP8266 D1 mini
Runs off Serial1 as only transmit is needed for sketch
I have left Serial Print statements running so you can track what the sketch is doing, remove for production
Player on Serial 1 (pin 2)
busyPin = 4 //receives busy signal from DY-SV5W
*/
//Serial1
#define TXD2 2
//variables always required to work DY-SV5W
const int busyPin = 4;//change to any suitable digital pin
byte commandLength;
byte command[6];
int16_t checkSum = 0;
//my variables for demo sketch
int trackNum = 1;
int playStatus; //0 playing 1 stopped 2 waiting to start next track
unsigned long lastCheckTime;
unsigned long currentMillis;
int busyPinstate;
void playTrack(int soundTrack) {
//select track
Serial.print("soundTrack: ");
Serial.println(soundTrack);
command[0] = 0xAA;//first byte says it's a command
command[1] = 0x07;
command[2] = 0x02;
command[3] = (soundTrack>>8) & 0xFF;//snh...track HIGH bit
command[4] = soundTrack & 0xFF;//SNL... track low bit
checkSum = 0;
for (int q = 0; q < 5; q++) {
checkSum += command[q];
//Serial.print(command[q]);
//Serial.print(",");
}
//Serial.println(" ");
//Serial.println(checkSum,BIN);
//Serial.println(checkSum);
//command[5] = lowByte(checkSum);//SM check bit... low bit of the sum of all previous values
command[5] = (checkSum & 0xFF);//SM check bit... low bit of the sum of all previous values
// Serial.println(command[5],HEX);
// Serial.println(command[5],BIN);
// Serial.println(command[5]);
commandLength = 6;
sendCommand();
}
//plays whatever track has been paused or 1st track if nothing selected
//May need to be selected after putting into random mode
void play() {
command[0] = 0xAA;//first byte says it's a command
command[1] = 0x02;
command[2] = 0x00;
command[3] = 0xAC;
commandLength = 4;
sendCommand();
}
//selects random mode
void randomMode() {
command[0] = 0xAA;//first byte says it's a command
command[1] = 0x18;
command[2] = 0x01;
command[3] = 0x03;//random
checkSum = 0;
for (int q = 0; q < 4; q++) {
checkSum += command[q];
}
command[4] = (checkSum & 0xFF);//SM check bit... low bit of the sum of all previous values
commandLength = 5;
sendCommand();
//play() needs to be selected if you want the random tracks to start playing instantly
play();
}
//sets the device volume...0 - 30
void playbackVolume(int vol) {
if (vol > 30) { //check within limits
vol = 30;
}
command[0] = 0xAA;//first byte says it's a command
command[1] = 0x13;
command[2] = 0x01;
command[3] = vol;//volume
checkSum = 0;
for (int q = 0; q < 4; q++) {
checkSum += command[q];
}
command[4] = (checkSum & 0xFF);//SM check bit... low bit of the sum of all previous values
commandLength = 5;
sendCommand();
}
//sends the command
void sendCommand() {
int q;
for (q = 0; q < commandLength; q++) {
Serial1.write(command[q]);
Serial.print(" ");
Serial.print(command[q], HEX);
}
Serial.println(" End");
}
void setup() {
Serial.begin(115200);
Serial1.begin(9600);
Serial.println("ESP8266DYSV5Wv7");
pinMode(busyPin, INPUT);//pin to read from DY-SV5W buyPin
playbackVolume(17);//sets volume to lvl 17
}
void loop() {
currentMillis = millis();//stores time in millis() for non blocking timings
busyPinstate = digitalRead(busyPin);
if (busyPinstate > 0) { //nothing playing
if (playStatus < 1) {
Serial.print(busyPinstate);
playStatus = 1;
lastCheckTime = currentMillis;//set system ready for 2 sec delay
}
}
switch (playStatus) {
Serial.print(" P: ");
Serial.println(playStatus);
case 1:
if (currentMillis - lastCheckTime >= 2000) { //2000 = 2 seconds of silence between tracks
playStatus = 2;
//Just selecting a new track
//this could have been something like
//trackNum = random(1,5);
// trackNum = 1;//fixed track number
Serial.print("trackNum: ");
Serial.println(trackNum);
playTrack(trackNum);
trackNum++;
if(trackNum > 4){
trackNum = 1;
}
}
break;
case 2:
if(busyPinstate < 1){
playStatus = 0;
Serial.print(" P: ");
Serial.println(playStatus);
}
break;
default:
break;
}
}
Additional Resource Links
The technical specs for DY-SV5W.
DY-SV5W MP3 player using an UNO and Mega 2560
Comments
This site has been designed to be child friendly, this means that comments cannot be added to directly to the site. To add a comment or ask a question please email the address in this image: and use #64 DY-SV5W MP3 Player ESP32 ans ESP8266 D1 Mini as a reference.