Sketch ici
La puce est une YX5300
Led Verte : allumée si la carte est en pause ou prête à lire les mp3. En mode lecture elle clignote.
Support d’échantillonnage (kHz): 8 / 11,025 / 12/16 / 22,05 / 24/32 / 44,1 / 48 MP3 / WAV
carte Micro SD, Micro SDHC FAT 16 ou FAT 32
la vitesse de transmission est 9600 bps
alimentation peut être 3.2 ~ 5.2VDC
Carte->Arduino
Rx-> digital pin avec Software Serie ou TX
TX-> digital pin avec Software Serie ou RX
GND->GND
VCC-> 5v ou 3.3v
Fonctionnement :
La carte doit recevoir des commandes structurées de la manière suivante :
Byte | Description |
0x7E | Code de début |
0xFF | Code version |
0xxx | Nombre de bytes de la commande |
0xxx | Code de la commande |
0xxx | 0x00 = not feedback, 0x01 = feedback |
… | 0x00 ou Données associées à la commande |
… | 0x00 ou Données associées à la commande |
0xEF | Code de fin |
Ainsi pour la commande Next Song [0x01] on aura :
Byte | Valeur |
0x7E | 0x7E |
0xFF | 0xFF |
0xxx | 0x06 |
0xxx | 0x01 |
0xxx | 0x00 |
Byte1 | 0x00 (pas de donnée associée) |
Byte1 | 0x00 (pas de donnée associée) |
0xEF | 0xEF |
Liste des commandes :
Fonction pour envoyer des commandes à la carte :
void sendCommand(int8_t command, int16_t dat)
{
delay(20);
Send_buf[0] = 0x7e; //starting byte
Send_buf[1] = 0xff; //version
Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
Send_buf[3] = command; //
Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback
Send_buf[5] = (int8_t)(dat >> 8);//datah
Send_buf[6] = (int8_t)(dat); //datal
Send_buf[7] = 0xef; //ending byte
for (uint8_t i = 0; i < 8; i++) //
{
mySerial.write(Send_buf[i]) ;
}
}
Appel de la fonction avec envoi d’une commande :
sendCommand(CMD_PLAY_W_VOL, 0X0F01);//play the first song with volume 15 class sendCommand(CMD_SET_VOLUME, 30); //30 maxi
Il est possible de structurer la carde SD avec des répertoires « 01 », « 02 »,… Par exemple pour répartir les styles de musique…
Lecture du statut de la carte :
CMD | Réponse | Description |
– | 0x7E 0xFF 0x06 0x41 0x00 0x00 0x00 0xFE 0xBA 0xEF | Les données reçues correctement |
– | 0x7E 0xFF 0x06 0x3A 0x00 0x00 0x02 0xFE 0xBF 0xEF | Carte mémoire insérée TF |
– | 0x7E 0xFF 0x06 0x3B 0x00 0x00 0x02 0xFE 0xBE 0xEF | Suppression de support de stockage TF |
– | 0x7E 0xFF 0x06 0x40 0x00 0x00 0x06 0xB5 0xFE 0xEF | Erreur – Fichier introuvable |
– | 0x7E 0xFF 0x06 0x3D 0x00 0x00 0x04 0xFE 0xBA 0xEF | terminé le fichier 4 |
0x42 | 0x7E 0xFF 0x06 0x42 0x00 0x02 0x00 0xB6 0xFE 0xE | player – arrêté ( arrêt ) |
0x42 | 0x7E 0xFF 0x06 0x42 0x00 0x02 0x01 0xB6 0xFE 0xE | player – joue( play) |
0x42 | 0x7E 0xFF 0x06 0x42 0x00 0x02 0x02 0xB6 0xFE 0xE | player – en attente ( pause ) |
0x48 | 0x7E 0xFF 0x06 0x48 0x00 0x00 0x07 0xFE 0xAC 0xEF | Trouvé 7 fichiers |
0x4C | 0x7E 0x06 0xFF 0x4C 0x00 0x00 0x04 0xFE 0xAB 0xEF | Joue actuellement le fichier 4 |
0x4F | 0x7E 0xFF 0x06 0x4F 0x00 0x00 0x02 0xFE 0xAA 0xEF | Trouvé 2 dossiers |
0x43 | 0x7E 0xFF 0x06 0x43 0x00 0x00 0x1E 0x99 0xFE 0xEF | Le niveau actuel du volume de 30 = 1F |
Exemple de lecture de codes retour :
#include
SoftwareSerial mp3(5, 6);
static uint8_t cmdbuf[8] = {0}; //-- Buffer envoi
static uint8_t ansbuf[10] = {0}; //-- Buffer réception
unsigned long int last = 0; //-- Pour timer
/////////////////////////////////////////
// Envoi Commandes
////////////////////////////////////////
void command(int8_t cmd, int16_t dat)
{
delay(20);
cmdbuf[0] = 0x7e; // Début
cmdbuf[1] = 0xFF; // Version
cmdbuf[2] = 0x06; // Nombre octets
cmdbuf[3] = cmd; // Commande
cmdbuf[4] = 0x00; // 0x00 = no feedback, 0x01 = feedback
cmdbuf[5] = (int8_t)(dat >> 8); // DAT1
cmdbuf[6] = (int8_t)(dat); // DAT2
cmdbuf[7] = 0xef; // Fin
for (uint8_t i = 0; i < 8; i++)
{
mp3.write(cmdbuf[i]);
}
}
//////////////////////////////////////////
// Traitement réponse
////////////////////////////////////////
//-- Affichage des codes Hexa
void byte2hex(uint8_t b)
{
Serial.print("0x");
if (b < 16) Serial.print("0");
Serial.print(b, HEX);
Serial.print(" ");
}
//-- Récupération réponse
boolean answer(void)
{
uint8_t i = 0;
//-- Lecture de 10 Octets
while (mp3.available() && (i < 10)) { uint8_t b = mp3.read(); ansbuf[i] = b; i++; byte2hex(b); } Serial.println(); //-- Test si la réponse est correcte if ((ansbuf[0] == 0x7E) && (ansbuf[9] == 0xEF)) { return true; } return false; } /////////////////////// // SETUP /////////////////////////// void setup() { Serial.begin(9600); mp3.begin(9600); delay(500); command(0x09, 0x0002); //-- Choix carte SD delay(200); command(0x17, 0x0100); //-- Sélection dossier 01/ last = millis(); } ///////////////////////////// // BOUCLE ///////////////////////////// void loop() { if ((millis() - last) > 5000) //-- Demande toute les 5s
{
last = millis();
command(0x4C, 0x0000); //-- Demande numéro du fichier en cours de lecture
}
if (mp3.available())
{
if (answer())
{
if (ansbuf[3] == 0x4C) //-- Si un fichier est en cours de lecture
{
Serial.print("Actuellement, je joue le fichier:");
Serial.println(ansbuf[6]);
}
}
}
}
0x7E 0xFF 0x06 0x3A 0x00 0x00 0x02 0xFE 0xBF 0xEF Carte SD Ok 0x7E 0xFF 0x06 0x48 0x00 0x00 0x24 0xFE 0x8F 0xEF Nb de fichiers:37 0x7E 0xFF 0x06 0x4C 0x00 0x00 0x03 0xFE 0xAC 0xEF Actuellement, je joue le fichier:3
Références :
https://github.com/cefaloide/ArduinoSerialMP3Player
Exemples : http://www.jarzebski.pl/arduino/komponenty/modul-mp3-z-ukladem-yx5300.html
Hi there, nice work, but how to play Files in random order (ShufflePlay) ?!
Please Help.
Thanks
If you want shuffleplay you have to code it in the Arduino sktech.