ESP8266 - RFID MP3 Player

This tutorial instructs you how to make an RFID MP3 player by using an ESP8266, an RC522 RFID reader, and an MP3 player module. The MP3 player module comes with a small SD card that stores various songs. Each RFID card is linked to a specific song, and the number of RFID cards corresponds to the number of songs.

When you pass an RFID card in front of the RFID reader, the ESP8266 will play the song that is linked to that particular RFID card.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×RFID/NFC RC522 Kit (reader + tags)
1×RFID Key Fob
1×Serial MP3 Player Module
1×Micro SD Card
1×3.5mm Aux Speaker
1×Jumper Wires
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of RFID/NFC RC522 Module and MP3 Player

If you do not know about RFID/NFC RC522 Module and MP3 player (pinout, how it works, how to program ...), learn about them in the following tutorials:

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and RFID RC522 MP3 player

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

※ NOTE THAT:

The order of pins can vary according to manufacturers. ALWAYS use the labels printed on the module. The above image shows the pinout of the modules from DIYables brand.

Preparation

  • Pre-store a list of songs that you want to play to micro SD Card.
  • Insert the micro SD Card to the MP3 player module
  • Connect the MP3 player module to ESP8266
  • Connect the speaker to the MP3 player module to a
  • Connect the speaker to a power source.
  • Connect the RFID reader to the ESP8266.

Because UID is usually not printed on RFID Tag, The first step we need to do is to find out the tags' UID. This can be done by:

  • Copy the below code and open with Arduino IDE
/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-rfid-mp3-player */ #include <SPI.h> #include <MFRC522.h> #define SS_PIN D8 // The ESP8266 pin connected to the SS of the RFID reader #define RST_PIN D3 // The ESP8266 pin connected to the RST of the RFID reader MFRC522 rfid(SS_PIN, RST_PIN); void setup() { Serial.begin(9600); SPI.begin(); // init SPI bus rfid.PCD_Init(); // init MFRC522 Serial.println("Tap RFID Tag on reader"); } void loop() { if (rfid.PICC_IsNewCardPresent()) { // new tag is available if (rfid.PICC_ReadCardSerial()) { // NUID has been readed // print NUID in Serial Monitor in the hex format for (int i = 0; i < rfid.uid.size; i++) { Serial.print("0x"); Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "); Serial.print(rfid.uid.uidByte[i], HEX); if (i < (rfid.uid.size - 1)) Serial.print(", "); } Serial.println(); rfid.PICC_HaltA(); // halt PICC rfid.PCD_StopCrypto1(); // stop encryption on PCD } } }
  • Click Upload button on Arduino IDE to upload code to ESP8266
  • Open Serial Monitor
  • Tap RFID card/keyfob one by one on RFID-RC522 module
  • Take note of UIDs on Serial Monitor, it looks like below:
COM6
Send
Tap RFID Tag on reader 0x51, 0x3D, 0xC1, 0xAC 0x6A, 0x4C, 0x12, 0x6D 0xB0, 0x1F, 0x92, 0x11
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

We will use these RFID UIDs to update the ESP8266 code below

ESP8266 Code - RFID Mp3 Player

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-rfid-mp3-player */ #include <SPI.h> #include <MFRC522.h> #include <SoftwareSerial.h> #define CMD_PLAY_NEXT 0x01 #define CMD_PLAY_PREV 0x02 #define CMD_PLAY_W_INDEX 0x03 #define CMD_SET_VOLUME 0x06 #define CMD_SEL_DEV 0x09 #define CMD_PLAY_W_VOL 0x22 #define CMD_PLAY 0x0D #define CMD_PAUSE 0x0E #define CMD_SINGLE_CYCLE 0x19 #define DEV_TF 0x02 #define SINGLE_CYCLE_ON 0x00 #define SINGLE_CYCLE_OFF 0x01 #define ESP8266_RX D1 // The ESP8266 pin connected to the TX of the Serial MP3 Player module #define ESP8266_TX D2 // The ESP8266 pin connected to the RX of the Serial MP3 Player module #define SS_PIN D8 // The ESP8266 pin connected to the SS of the RFID reader #define RST_PIN D3 // The ESP8266 pin connected to the RST of the RFID reader #define SONG_NUM 3 // 3 songs + 3 RFID cards, change it as your need MFRC522 rfid(SS_PIN, RST_PIN); SoftwareSerial mp3(ESP8266_RX, ESP8266_TX); byte RFID_UIDs[SONG_NUM][4] = { { 0x51, 0x3D, 0xC1, 0xAC }, // UPDATE THIS VALUE FROM PREPARATION STEP { 0x6A, 0x4C, 0x12, 0x6D }, // UPDATE THIS VALUE FROM PREPARATION STEP { 0xB0, 0x1F, 0x92, 0x11 } // UPDATE THIS VALUE FROM PREPARATION STEP // ADD MORE IF NEEDED }; void setup() { Serial.begin(9600); mp3.begin(9600); delay(500); // wait chip initialization is complete mp3_command(CMD_SEL_DEV, DEV_TF); // select the TF card delay(200); // wait for 200ms SPI.begin(); // init SPI bus rfid.PCD_Init(); // init MFRC522 Serial.println("Tap RFID Tag on reader"); } void loop() { if (rfid.PICC_IsNewCardPresent()) { // new tag is available if (rfid.PICC_ReadCardSerial()) { // NUID has been readed Serial.print("Tag UID:"); for (int i = 0; i < rfid.uid.size; i++) { Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "); Serial.print(rfid.uid.uidByte[i], HEX); } Serial.println(); for (int index = 0; index < SONG_NUM; index++) { if (rfid.uid.uidByte[0] == RFID_UIDs[index][0] && rfid.uid.uidByte[1] == RFID_UIDs[index][1] && rfid.uid.uidByte[2] == RFID_UIDs[index][2] && rfid.uid.uidByte[3] == RFID_UIDs[index][3]) { Serial.print("Playing song "); Serial.println(index); mp3_command(CMD_PLAY_W_INDEX, index); // Play mp3 } } rfid.PICC_HaltA(); // halt PICC rfid.PCD_StopCrypto1(); // stop encryption on PCD } } } void mp3_command(int8_t command, int16_t dat) { int8_t frame[8] = { 0 }; frame[0] = 0x7e; // starting byte frame[1] = 0xff; // version frame[2] = 0x06; // The number of bytes of the command without starting byte and ending byte frame[3] = command; // frame[4] = 0x00; // 0x00 = no feedback, 0x01 = feedback frame[5] = (int8_t)(dat >> 8); // data high byte frame[6] = (int8_t)(dat); // data low byte frame[7] = 0xef; // ending byte for (uint8_t i = 0; i < 8; i++) { mp3.write(frame[i]); } }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the above code and open with Arduino IDE
  • Update UIDs you obtained in the preperation step to the above code.
  • Upload the code to ESP8266
  • Tap an RFID Tag on RFID-RC522 module one by one
  • Check out the sound from MP3 Player
  • If the everything run smoothly, each RFID card will be associlated with a song.
  • You can mark the name of the song on each RFID card.

Video Tutorial