En esta entrada explicaré como reproducir MP3 con arduino y la placa YX5300 de Catalex:
Este módulo utiliza una tarjeta SD para almacenar las canciones en formato MP3 o WAV. La tarjeta debe estar formateada a FAT32 o FAT16.
Se recomienda guardar los audios en carpetas numeradas, aunque solo se tenga una carpeta, la manera más cómoda (por ejemplo si se quiere reproducir todas las canciones de una carpeta en loop) es seguir la siguiente estructura:
1 2 3 4 5 6 7 8 9 10 11 |
. ├── 01 │ ├── 001-blue-piano.mp3 │ └── 002-gettin-ready.mp3 ├── 02 │ ├── 001-ukulele-in-my-heart.mp3 │ ├── 002-inspiring-innovation.mp3 │ └── 003-thanks-for-the-memories.mp3 └── 03 ├── 001-a-hero-is-born.mp3 └── 002-lullaby.mp3 |
Para la conexión del cableado si utilizamos Arduino Uno o Nano será la siguiente:
Serial MP3 Player -> ARDUINO
VCC -> 5V o 3.3V
GND -> GND
TX-> D5
RX -> D6
Si utilizamos Arduino Mega se debe utilizar la siguiente conexión que utiliza el Serial3 (en el código también se tendrán que comentar/descomentar un par de líneas):
Serial MP3 Player -> ARDUINO
VCC -> 5V o 3.3V
GND -> GND
TX-> 15 RX3
RX -> 14 TX3
También deberemos conectar a la salida del Jack 3.5 unos altavoces/auriculares.
Después ya podemos cargar el siguiente código o descargarlo de Github:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
/***********************************************************/ // Demo for the Serial MP3 Player Catalex (YX5300 chip) // Hardware: Serial MP3 Player *1 // Board: Arduino UNO // http://www.dx.com/p/uart-control-serial-mp3-music-player-module-for-arduino-avr-arm-pic-blue-silver-342439#.VfHyobPh5z0 // // // // Uncomment SoftwareSerial for Arduino Uno or Nano. #include <SoftwareSerial.h> #define ARDUINO_RX 5 //should connect to TX of the Serial MP3 Player module #define ARDUINO_TX 6 //connect to RX of the module SoftwareSerial mp3(ARDUINO_RX, ARDUINO_TX); //#define mp3 Serial3 // Connect the MP3 Serial Player to the Arduino MEGA Serial3 (14 TX3 -> RX, 15 RX3 -> TX) static int8_t Send_buf[8] = {0}; // Buffer for Send commands. // BETTER LOCALLY static uint8_t ansbuf[10] = {0}; // Buffer for the answers. // BETTER LOCALLY String mp3Answer; // Answer from the MP3. boolean autoResume = true; /************ Command byte **************************/ #define CMD_NEXT_SONG 0X01 // Play next song. #define CMD_PREV_SONG 0X02 // Play previous song. #define CMD_PLAY_W_INDEX 0X03 #define CMD_VOLUME_UP 0X04 #define CMD_VOLUME_DOWN 0X05 #define CMD_SET_VOLUME 0X06 #define CMD_SNG_CYCL_PLAY 0X08 // Single Cycle Play. #define CMD_SEL_DEV 0X09 #define CMD_SLEEP_MODE 0X0A #define CMD_WAKE_UP 0X0B #define CMD_RESET 0X0C #define CMD_PLAY 0X0D #define CMD_PAUSE 0X0E #define CMD_PLAY_FOLDER_FILE 0X0F #define CMD_STOP_PLAY 0X16 #define CMD_FOLDER_CYCLE 0X17 #define CMD_SHUFFLE_PLAY 0x18 // #define CMD_SET_SNGL_CYCL 0X19 // Set single cycle. #define CMD_SET_DAC 0X1A #define DAC_ON 0X00 #define DAC_OFF 0X01 #define CMD_PLAY_W_VOL 0X22 #define CMD_PLAYING_N 0x4C #define CMD_QUERY_STATUS 0x42 #define CMD_QUERY_VOLUME 0x43 #define CMD_QUERY_FLDR_TRACKS 0x4e #define CMD_QUERY_TOT_TRACKS 0x48 #define CMD_QUERY_FLDR_COUNT 0x4f /************ Opitons **************************/ #define DEV_TF 0X02 /*********************************************************************/ void setup() { Serial.begin(9600); mp3.begin(9600); delay(500); sendCommand(CMD_SEL_DEV, DEV_TF); delay(500); } void loop() { char c = ' '; // If there a char on Serial call sendMP3Command to sendCommand if ( Serial.available() ) { c = Serial.read(); sendMP3Command(c); } // Check for the answer. if (mp3.available()) { Serial.println(decodeMP3Answer()); } delay(100); } /********************************************************************************/ /*Function sendMP3Command: seek for a 'c' command and send it to MP3 */ /*Parameter: c. Code for the MP3 Command, 'h' for help. */ /*Return: void */ void sendMP3Command(char c) { switch (c) { case '?': case 'h': Serial.println("HELP "); Serial.println(" p = Play"); Serial.println(" P = Pause"); Serial.println(" > = Next"); Serial.println(" < = Previous"); Serial.println(" + = Volume UP"); Serial.println(" - = Volume DOWN"); Serial.println(" c = Query current file"); Serial.println(" q = Query status"); Serial.println(" v = Query volume"); Serial.println(" x = Query folder count"); Serial.println(" t = Query total file count"); Serial.println(" 1 = Play folder 1"); Serial.println(" 2 = Play folder 2"); Serial.println(" 3 = Play folder 3"); Serial.println(" 4 = Play folder 4"); Serial.println(" 5 = Play folder 5"); Serial.println(" S = Sleep"); Serial.println(" W = Wake up"); Serial.println(" r = Reset"); break; case 'p': Serial.println("Play "); sendCommand(CMD_PLAY, 0); break; case 'P': Serial.println("Pause"); sendCommand(CMD_PAUSE, 0); break; case '>': Serial.println("Next"); sendCommand(CMD_NEXT_SONG, 0); sendCommand(CMD_PLAYING_N, 0x0000); // ask for the number of file is playing break; case '<': Serial.println("Previous"); sendCommand(CMD_PREV_SONG, 0); sendCommand(CMD_PLAYING_N, 0x0000); // ask for the number of file is playing break; case '+': Serial.println("Volume Up"); sendCommand(CMD_VOLUME_UP, 0); break; case '-': Serial.println("Volume Down"); sendCommand(CMD_VOLUME_DOWN, 0); break; case 'c': Serial.println("Query current file"); sendCommand(CMD_PLAYING_N, 0); break; case 'q': Serial.println("Query status"); sendCommand(CMD_QUERY_STATUS, 0); break; case 'v': Serial.println("Query volume"); sendCommand(CMD_QUERY_VOLUME, 0); break; case 'x': Serial.println("Query folder count"); sendCommand(CMD_QUERY_FLDR_COUNT, 0); break; case 't': Serial.println("Query total file count"); sendCommand(CMD_QUERY_TOT_TRACKS, 0); break; case '1': Serial.println("Play folder 1"); sendCommand(CMD_FOLDER_CYCLE, 0x0101); break; case '2': Serial.println("Play folder 2"); sendCommand(CMD_FOLDER_CYCLE, 0x0201); break; case '3': Serial.println("Play folder 3"); sendCommand(CMD_FOLDER_CYCLE, 0x0301); break; case '4': Serial.println("Play folder 4"); sendCommand(CMD_FOLDER_CYCLE, 0x0401); break; case '5': Serial.println("Play folder 5"); sendCommand(CMD_FOLDER_CYCLE, 0x0501); break; case 'S': Serial.println("Sleep"); sendCommand(CMD_SLEEP_MODE, 0x00); break; case 'W': Serial.println("Wake up"); sendCommand(CMD_WAKE_UP, 0x00); break; case 'r': Serial.println("Reset"); sendCommand(CMD_RESET, 0x00); break; } } /********************************************************************************/ /*Function decodeMP3Answer: Decode MP3 answer. */ /*Parameter:-void */ /*Return: The */ String decodeMP3Answer() { String decodedMP3Answer = ""; decodedMP3Answer += sanswer(); switch (ansbuf[3]) { case 0x3A: decodedMP3Answer += " -> Memory card inserted."; break; case 0x3D: decodedMP3Answer += " -> Completed play num " + String(ansbuf[6], DEC); break; case 0x40: decodedMP3Answer += " -> Error"; break; case 0x41: decodedMP3Answer += " -> Data recived correctly. "; break; case 0x42: decodedMP3Answer += " -> Status playing: " + String(ansbuf[6], DEC); break; case 0x48: decodedMP3Answer += " -> File count: " + String(ansbuf[6], DEC); break; case 0x4C: decodedMP3Answer += " -> Playing: " + String(ansbuf[6], DEC); break; case 0x4E: decodedMP3Answer += " -> Folder file count: " + String(ansbuf[6], DEC); break; case 0x4F: decodedMP3Answer += " -> Folder count: " + String(ansbuf[6], DEC); break; } return decodedMP3Answer; } /********************************************************************************/ /*Function: Send command to the MP3 */ /*Parameter:-int8_t command */ /*Parameter:-int16_ dat parameter for the command */ void sendCommand(int8_t command, int16_t dat) { delay(20); Send_buf[0] = 0x7e; // Send_buf[1] = 0xff; // Send_buf[2] = 0x06; // Len Send_buf[3] = command;// Send_buf[4] = 0x01; // 0x00 NO, 0x01 feedback Send_buf[5] = (int8_t)(dat >> 8); //datah Send_buf[6] = (int8_t)(dat); //datal Send_buf[7] = 0xef; // Serial.print("Sending: "); for (uint8_t i = 0; i < 8; i++) { mp3.write(Send_buf[i]) ; Serial.print(sbyte2hex(Send_buf[i])); } Serial.println(); } /********************************************************************************/ /*Function: sbyte2hex. Returns a byte data in HEX format. */ /*Parameter:- uint8_t b. Byte to convert to HEX. */ /*Return: String */ String sbyte2hex(uint8_t b) { String shex; shex = "0X"; if (b < 16) shex += "0"; shex += String(b, HEX); shex += " "; return shex; } /********************************************************************************/ /*Function: sanswer. Returns a String answer from mp3 UART module. */ /*Parameter:- uint8_t b. void. */ /*Return: String. If the answer is well formated answer. */ String sanswer(void) { uint8_t i = 0; String mp3answer = ""; // Get only 10 Bytes while (mp3.available() && (i < 10)) { uint8_t b = mp3.read(); ansbuf[i] = b; i++; mp3answer += sbyte2hex(b); } // if the answer format is correct. if ((ansbuf[0] == 0x7E) && (ansbuf[9] == 0xEF)) { return mp3answer; } return "???: " + mp3answer; } |
Si estamos trabajando con Arduino Mega se deberá comentar la línea 13
1 |
#include <SoftwareSerial.h> |
Comentar la línea 18 y descomentar la 19
1 2 |
SoftwareSerial mp3(ARDUINO_RX, ARDUINO_TX); //#define mp3 Serial3 // Connect the MP3 Serial Player to the Arduino MEGA Serial3 (14 TX3 -> RX, 15 RX3 -> TX) |
Ahora podremos manipular el reproductor a través del monitor serie del propio IDE de Arduino. Una vez subido el código al Arduino abrimos el monitor serie:
Si todo está correcto nos mostrará el texto:
Si introducimos la letra “h” nos mostrará el menu de ayuda con las diferentes opciones disponibles:
Si introducimos la letra “p” se ejecutará un audio y si introducimos “1” ejecutará todas las canciones de la carpeta 01.
Para más información sobre el funcionamiento del módulo puedes dirigirte a mi repositorio de Github donde se encuentra más documentación:
https://github.com/cefaloide/ArduinoSerialMP3Player
Referencias:
https://andrologiciels.wordpress.com/arduino/son-et-arduino/mp3/catalex-mp3-serie/
http://www.jarzebski.pl/arduino/komponenty/modul-mp3-z-ukladem-yx5300.html
quisiera saber quienes son, y cuando fue publicado este pagina web. fecha de publicacion. Deseo su referencia bibliografica para un trabajo.
Hola Baltazar,
soy el dueño del blog, me llamo Joan Rueda y esta entrada se publicó el 7 de Febrero del 2017.
Un saludo!
en que ciudad fue publicada
Barcelona
estimado, esta muy interesante tu articulo sobre el catalex hay muy poca información sobre este mp3 player, pero quiero hacerte una consulta, ojalá me respondas.
yo quiero agregar este mp3 a un pinball que estoy haciendo, y solo me gustaria que al momento de golpear algun switch este emita un audio, lo demás no es importante para mi, entonces como puedo hacerlo para que solo funciona la accion “Play”
tu definiste todas las acciones, yo pensaria solo en Play, stop y dejar un volumen fijo, sera posible hacer eso?
Saludos y gracias por las explicaciones sobre este mp3.
Mil gracias Joan, desde El Salvador… tu programa me ayudo mucho en un proyecto…
Hola Joan, esta entrada me ha servido mucho para trabajar con el módulo catalex. Gracias por eso.
Aprovecho de preguntarte si sabes como podría hacer que el módulo reproduzca mp3s de forma aleatoria (shuffle). He revisado el manual del módulo y no aparece el comando, pero puede que el documento que tengo sea muy viejo (2015). Desde ya te lo agradezco.
aqui tienes aleatorio
https://www.youtube.com/watch?v=m6QgmEs3_JE
Hola joan, esta entrada me ha servido mucho, pero quisiera saber como hago para darle play a un archivo en especifico de la SD a traves de la deteccion de una tarjeta RFID. te agradeceria mucho tu colaboracion y experiencia ya que llevo varios dias tratando de hacerlo sin obtener un buen resultado. Gracias de nuevo
Hola que tal , podrías ayudarme, me gustaría que al momento de presionar un botón este emita un audio y que al volverlo a presionar emita otro audio, únicamente la opción de play es la que me interesa, gracias por tu ayuda, ojala puedas ayudarme.
Hola,
Esta muy bien el tutorial y funciona bien.
Pero como puedo hacer para reproducir una solo música, una sola vez teniendo 20 audios diferentes.
Me explico mi programa tendrá 20 eventos deferentes y quiero que para cada evento reproducir un audio.
Estos son algunos eventos
-Alta temperatura
-Alta Humedad
et
intente algo como esto…
void sendMP3Command() {
switch (modo) {
case 1:
Serial.println(“Play “);
sendCommand(CMD_PLAY, 0);
break;
}
}
void lectura()
{
if (Serial.available() > 0)
{
String str = Serial.readStringUntil(‘\n’);
modo = str.toInt();
}
}
void loop()
{
sendMP3Command();
lectura();
Serial.println(modo);
// Check for the answer.
if (mp3.available())
{
Serial.println(decodeMP3Answer());
}
delay(100);
}
pero no funciona,
Hola Joan, muchas gracias por compartir la explicación del uso de este módulo, me ha valido para mi proyecto te pondré en las referencias del código cuando lo comparta.
Un saludo
hello, did you get query file count to work? I always get 0x41 (data received correctly) but not the number of containing files
thank you
Thomas
disculpa ,me podrias ayudar por favor con mi consulta . sabes si este modulo puede reproducir wav simultaneamente ,osea 2 o 3 archivos wav al mismo tiempo.grcs