ESP32 - RFID MP3 Player

In this tutorial, we will explore the process of creating an RFID-based MP3 player using ESP32, an RC522 RFID reader, and an MP3 player module. The MP3 player module is equipped with a micro SD card where multiple songs are stored. Each RFID card represents for a song, and the number of RFID cards matches the number of songs.

By swiping an RFID card in front of the RFID reader, the ESP32 plays the corresponding song associated with that specific RFID card.

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
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×(Recommended) ESP32 Screw Terminal Adapter

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to RFID/NFC RC522 Module and MP3 Player

Unfamiliar with RFID/NFC RC522 Module and MP3 player, including their pinouts, functionality, and programming? Explore comprehensive tutorials on these topics below:

Wiring Diagram

ESP32 RFID RC522 MP3 player wiring diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

※ 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 ESP32
  • Connect the speaker to the MP3 player module to a
  • Connect the speaker to a power source.
  • Connect the RFID reader to the ESP32.

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:

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Copy the below code and open with Arduino IDE
/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-rfid-mp3-player */ #include <SPI.h> #include <MFRC522.h> #define SS_PIN 5 // ESP32 pin GPIO5 connected to the SS of the RFID reader #define RST_PIN 27 // ESP32 pin GPIO27 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 ESP32
  • 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 0xB1, 0xCD, 0x5F, 0xA1 0x1A, 0x7C, 0x72, 0x1D 0xA2, 0x1C, 0x9D, 0xA1
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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

ESP32 Code - RFID Mp3 Player

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-rfid-mp3-player */ #include <SPI.h> #include <MFRC522.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 SS_PIN 5 // ESP32 pin GPIO5 connected to the SS of the RFID reader #define RST_PIN 27 // ESP32 pin GPIO27 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); byte RFID_UIDs[SONG_NUM][4] = { { 0xB1, 0xCD, 0x5F, 0xA1 }, // UPDATE THIS VALUE FROM PREPARATION STEP { 0x1A, 0x7C, 0x72, 0x1D }, // UPDATE THIS VALUE FROM PREPARATION STEP { 0xA2, 0x1C, 0x9D, 0xA1 } // UPDATE THIS VALUE FROM PREPARATION STEP // ADD MORE IF NEEDED }; void setup() { Serial.begin(9600); Serial2.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++) { Serial2.write(frame[i]); } }

Quick Instructions

  • 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 ESP32
  • 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

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES