Circuit Documentation
Summary
This circuit integrates an ESP32 Devkit V1 microcontroller with a DFPlayer Mini MP3 player module, an RFID-RC522 reader, a piezo speaker, and two arcade buttons (one white and one red). The ESP32 is responsible for controlling the MP3 playback and interfacing with the RFID reader to trigger specific actions based on RFID tag reads. The DFPlayer Mini is used for audio output, and the RFID-RC522 is used for scanning RFID tags. The arcade buttons are used for user input to control playback and volume. A 4 x AAA battery mount provides power to the circuit, and a rocker switch is used to control the power supply.
Component List
ESP32 Devkit V1
- Microcontroller with WiFi and Bluetooth capabilities.
- It has multiple GPIO pins for interfacing with other components.
DFPlayer Mini
- A small and low-cost MP3 module that can directly play MP3 files stored on a microSD card.
- It has pins for serial communication and direct speaker output.
RFID-RC522
- An RFID reader/writer module for 13.56 MHz cards.
- It uses SPI for communication with the microcontroller.
4 x AAA Battery Mount
- A battery holder for four AAA batteries, providing a power source for the circuit.
Piezo Speaker
- An electronic device that can produce sound when an electrical signal is applied.
- Typically used for generating beeps, tones, and alerts.
Rocker Switch
- A simple on-off switch to control the power flow in the circuit.
Arcade Button (White)
- A push-button switch that can be used for user input.
Arcade Button (Red)
- Another push-button switch similar to the white one, used for user input.
Wiring Details
ESP32 Devkit V1
3V3connected to DFPlayer MiniVCCand RFID-RC522VCC (3.3V)GNDconnected to DFPlayer MiniGND, RFID-RC522GND, both Arcade Buttons, and Rocker Switch2D4connected to Arcade Button (White)RX2connected to DFPlayer MiniTXTX2connected to DFPlayer MiniRXD5connected to Arcade Button (Red)D18connected to RFID-RC522SCKD19connected to RFID-RC522MISOD21connected to RFID-RC522SDAD22connected to RFID-RC522RSTD23connected to RFID-RC522MOSIVINconnected to 4 x AAA Battery Mount+
DFPlayer Mini
SPK1connected to Piezo Speakerpin2SPK2connected to Piezo Speakerpin1
Rocker Switch
1connected to 4 x AAA Battery Mount-
Documented Code
The code provided is for the ESP32 Devkit V1 microcontroller. It includes libraries for the DFPlayer Mini, RFID-RC522, and button debouncing. The code initializes the MP3 player and RFID reader, and sets up button callbacks for play/pause and volume control. The RFID reader scans for tags and triggers MP3 playback based on the tag's data.
#include "DFPlayerMini_Fast.h"
#include <MFRC522.h>
#include <SPI.h>
#include <NfcAdapter.h>
#include <Regexp.h>
#include <ButtonDebounce.h>
#define RXD2 16
#define TXD2 17
#define SIZE_BUFFER 18
#define MAX_SIZE_BLOCK 16
#define SS_PIN 21
#define RST_PIN 22
#define NDEF_USE_SERIAL true
int RFID_SDA = 21;
int RFID_RST = 22;
int RFID_SCK = 18;
int RFID_MOSI = 23;
int RFID_MISO = 19;
int bd = 115200;
int serial2_baud = 9600;
int playPauseButtonPin = 2;
int volumeButtonPin = 4;
int isPaused = 0;
int volume = 10;
MFRC522 mfrc522(RFID_SDA, RFID_RST);
NfcAdapter nfc = NfcAdapter(&mfrc522);
DFPlayerMini_Fast myMP3;
MatchState ms;
ButtonDebounce playPauseButton(playPauseButtonPin, 250);
ButtonDebounce volumeButton(volumeButtonPin, 250);
void setup() {
Serial.begin(bd);
pinMode(playPauseButtonPin, INPUT_PULLUP);
pinMode(volumeButtonPin, INPUT);
while(!Serial);
initMp3Player();
initNfcReader();
}
void loop() {
buttonsLoop();
nfcLoop();
delay(100);
}
void buttonsLoop() {
playPauseButton.update();
volumeButton.update();
playPauseButton.setCallback(handlePlayPauseButton);
volumeButton.setCallback(handleVolumeButton);
}
void handlePlayPauseButton(const int state) {
if (state == HIGH) {
if (isPaused) {
myMP3.resume();
isPaused = 0;
} else {
myMP3.pause();
isPaused = 1;
}
}
}
void handleVolumeButton(const int state) {
if (state == HIGH) {
if (volume == 30) {
volume = 10;
} else {
volume = volume + 10;
}
myMP3.volume(volume);
}
}
void initNfcReader() {
SPI.begin(RFID_SCK, RFID_MISO, RFID_MOSI, RFID_SDA);
mfrc522.PCD_Init();
nfc.begin(true);
delay(4);
Serial.println("RFID Ready");
}
void initMp3Player() {
Serial2.begin(serial2_baud, SERIAL_8N1, RXD2, TXD2);
while(!Serial2);
myMP3.begin(Serial2);
myMP3.volume(volume);
Serial.println("Mp3 ready");
delay(100);
myMP3.playFromMP3Folder(1);
}
void nfcLoop() {
if (nfc.tagPresent()) {
// extract the NDEF text
NfcTag tag = nfc.read();
NdefMessage msg = tag.getNdefMessage();
NdefRecord record = msg.getRecord(0);
char* ndefText = (char*)record.getPayload();
// extract the track number from the raw ndef text
ms.Target(ndefText);
char result = ms.Match("(%d+)");
char buf [100];
if (result == REGEXP_MATCHED) {
String trackNumber = ms.GetMatch(buf);
Serial.println("Playing: " + trackNumber);
myMP3.playFromMP3Folder(trackNumber.toInt());
}
}
}
This code is designed to be uploaded to the ESP32 Devkit V1 microcontroller. It is responsible for handling the logic of the circuit, including reading RFID tags, controlling MP3 playback, and responding to button presses.
