Send commands and receive data from MP3 player via TX/RX

Hi there,

I am trying to find a library or code for the following MP3 Music player to interact by UART (using IO0, IO1 pins).

Unfortunately, I couldn't find any code related to this particular player. As I am new to Arduino programming, I couldn't find a way to generate the command with the specifications they have given on the product page.

Also, I am not sure about the 3 switches found on the red box on the board.

Any help or guidance is much appreciated. Thanks in advance.

Hello,

Here is a library GitHub - SnijderC/dyplayer: Abstracton for DY-XXXX mp3 player modules over UART.

Here is the datasheet if you want to write your own code https://grobotronics.com/images/companies/1/datasheets/DY-SV5W%20Voice%20Playback%20ModuleDatasheet.pdf

Surely, the fundamental Arduino code you require is
Serial.print(variable); and that is about all. You can't possibly be complaining about lack of documentation. Just make sure you connect your wires properly Tx>RX and Rx<Tx.

Hi @guix @Nick_pyner,

Many thanks for your help guys. I tried the library mentioned by guix and it worked perfectly fine. Also, I took into consideration of Nick_pyner and wrote my own code for various functions for the player. Honestly, it was fun and still, I want to develop the full functionality of the player myself. Anyhow, I thought of sharing my code here. If you find any better improvements to the code, please let me know.

CardozaDYPlayer.h

#ifndef CDYPlayer
#define CDYPlayer

#include "Arduino.h"

class CardozaDYPlayer {
    private:
        Stream* _serial_connection;
        byte _start_byte=0xAA;
        int _volume=15;
        int _current_song;
        int _number_of_songs;
        int _equalizer;
        void _sendCommand(byte command, byte data_length=0x00, byte data=0x00);
    public:
        CardozaDYPlayer(Stream& serial_connection);
        void play();
        void pause();
        void stop();
        void previous();
        void next();
        void setVolume(int volume);
        int getVolume();
        void increaseVolume();
        void decreaseVolume();
        int getCurrentSong();
        int getNumberOfSongs();
        void setEqualizer(int equalizer);
};

#endif

CardozaDYPlayer.cpp

#include "CardozaDYPlayer.h"

CardozaDYPlayer::CardozaDYPlayer(Stream &serial_connection){
    _serial_connection = &serial_connection;
    CardozaDYPlayer::setVolume(_volume);
}

void CardozaDYPlayer::_sendCommand(byte command, byte data_length, byte data){
    if(data > 0){
        unsigned int checksum = _start_byte + command + data_length + data;
        uint8_t command_buffer[] = {_start_byte, command, data_length, data, checksum};
        for(int i=0; i<sizeof(command_buffer); i++){
            _serial_connection->write(command_buffer[i]);
        }
    }
    else{
        unsigned int checksum = _start_byte + command + data_length;
        uint8_t command_buffer[] = {_start_byte, command, data_length, checksum};
        for(int i=0; i<sizeof(command_buffer); i++){
            _serial_connection->write(command_buffer[i]);
        }
    }
    byte serial_in="";
    if(command == 12){
        while(serial_in == 170){
            serial_in = _serial_connection->read();
        }
        for(int i=1;i<=6;i++){
            if(i == 5) _number_of_songs = _serial_connection->read();
            else _serial_connection->read();
        }
    }
    if(command == 13){
        while(serial_in == 170){
            serial_in = _serial_connection->read();
        }
        for(int i=1;i<=6;i++){
            if(i == 5) _current_song = _serial_connection->read();
            else _serial_connection->read();
        }
    }
}

void CardozaDYPlayer::play(){
    CardozaDYPlayer::_sendCommand(0x02);
}

void CardozaDYPlayer::pause(){
    CardozaDYPlayer::_sendCommand(0x03);
}

void CardozaDYPlayer::stop(){
    CardozaDYPlayer::_sendCommand(0x04);
}

void CardozaDYPlayer::previous(){
    CardozaDYPlayer::_sendCommand(0x05);
}

void CardozaDYPlayer::next(){
    CardozaDYPlayer::_sendCommand(0x06);
}

void CardozaDYPlayer::setVolume(int volume){
    _volume = volume;
    CardozaDYPlayer::_sendCommand(0x13, 1, volume);
}

int CardozaDYPlayer::getVolume(){
    return _volume;
}

void CardozaDYPlayer::increaseVolume(){
    if(_volume < 30) _volume++;
    CardozaDYPlayer::setVolume(_volume);
}

void CardozaDYPlayer::decreaseVolume(){
    if(_volume > 0) _volume--;
    CardozaDYPlayer::setVolume(_volume);
}

int CardozaDYPlayer::getCurrentSong(){
    CardozaDYPlayer::_sendCommand(0x0D);
    return _current_song;
}

int CardozaDYPlayer::getNumberOfSongs(){
    CardozaDYPlayer::_sendCommand(0x12);
    return _current_song;
}

void CardozaDYPlayer::setEqualizer(int equalizer){
    _equalizer = equalizer;
    CardozaDYPlayer::_sendCommand(0x1A, 1, _equalizer);
}

Application

#include <SoftwareSerial.h>
#include <CardozaDYPlayer.h>

SoftwareSerial mp3Serial(10, 11); // Using custom serial pins on the Arduino board

CardozaDYPlayer mp3Player(mp3Serial);

void setup(){
  mp3Serial.begin(9600);
  Serial.begin(9600);
  
  mp3Player.play();
  mp3Player.next();
  mp3Player.setVolume(20);
}

void loop(){
  delay(5000);
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.