This circuit integrates an RFID reader (RFID-RC522), a DFPlayer Mini MP3 player, a loudspeaker, and an Arduino Nano microcontroller. The RFID reader detects RFID cards and triggers the DFPlayer Mini to play specific audio tracks through the loudspeaker based on the detected card. The Arduino Nano serves as the central controller, managing the RFID reader and the DFPlayer Mini.
RFID-RC522
DFPlayer MINI
Loudspeaker
Arduino Nano
#include <SPI.h>
#include <MFRC522.h>
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
// RFID RC522 設定
#define RST_PIN 9 // RST 引腳
#define SS_PIN 10 // SDA 引腳
MFRC522 mfrc522(SS_PIN, RST_PIN);
// DFPlayer 設定
SoftwareSerial mySerial(5, 6); // RX 引腳, TX 引腳
DFRobotDFPlayerMini myDFPlayer;
// 預設的卡片 UID
byte card1[] = {0x63, 0x2D, 0x20, 0x28}; // 卡片 1 的 UID
byte card2[] = {0x33, 0x52, 0xDF, 0x26}; // 卡片 2 的 UID
// 狀態變數
bool isPlaying = false; // 是否正在播放音樂
bool isPaused = false; // 是否暫停播放
bool cardDetected = false; // 是否有卡片被偵測到
unsigned long lastCheckTime = 0; // 記錄最後檢查時間
const unsigned long checkInterval = 250; // 檢查間隔(毫秒)
const unsigned long stableDelay = 200; // 卡片移開後等待時間(毫秒)
unsigned long lastCardRemoveTime = 0; // 記錄卡片移開的時間
byte lastUID[10]; // 儲存最後一次偵測到的 UID
byte lastUIDSize = 0; // 儲存最後一次偵測到的 UID 大小
bool compareUID(byte *uid1, byte *uid2, byte size); // 比較兩個 UID 是否相同
void copyUID(byte *source, byte *destination, byte size); // 複製 UID
void setup() {
Serial.begin(9600); // 初始化序列通信
SPI.begin(); // 啟用 SPI 接口
mfrc522.PCD_Init(); // 初始化 RC522 模組
Serial.println("RFID OK");
mySerial.begin(9600); // 設定軟體串口通信速率
if (!myDFPlayer.begin(mySerial)) {
Serial.println("DFPlayer 無法初始化!");
while (true); // 若 DFPlayer 初始化失敗,則停滯程序
}
Serial.println("DFPlayer OK");
myDFPlayer.volume(10); // 設置音量(0-30)
Serial.println("音量設置完成");
}
void loop() {
unsigned long time = millis();
// 檢查 DFPlayer 狀態
if (myDFPlayer.available()) {
if (myDFPlayer.readType() == DFPlayerPlayFinished) {
Serial.println("音樂播放完成");
isPlaying = false; // 重置播放狀態
isPaused = false; // 重置暫停狀態
}
}
// 每隔一段時間檢查卡片是否仍在範圍內
if (time - lastCheckTime >= checkInterval)
{
lastCheckTime = time; // 更新最後檢查時間
// 檢查是否有新卡片被偵測到
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial())
{
byte *uid = mfrc522.uid.uidByte; // 讀取卡片 UID
cardDetected = true; // 設置卡片偵測標誌為 true
Serial.print("卡片 UID: ");
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(uid[i], HEX);
Serial.print(" ");
}
Serial.println();
// 如果是相同卡片且音樂暫停,恢復播放
if (isPaused && compareUID(uid, lastUID, mfrc522.uid.size)) {
Serial.println("相同卡片恢復播放");
myDFPlayer.start(); // 恢復播放
isPlaying = true; // 設置播放狀態為 true
isPaused = false; // 重置暫停狀態
return;
}
// 比對 UID 並播放對應音樂
if (compareUID(uid, card1, mfrc522.uid.size)) {
if (!isPlaying) { // 如果未播放音樂
Serial.println("播放音樂 1");
myDFPlayer.play(1); // 播放第 1 首音樂
isPlaying = true; // 設置播放狀態為 true
copyUID(uid, lastUID, mfrc522.uid.size); // 儲存當前 UID
lastUIDSize = mfrc522.uid.size; // 儲存當前 UID 大小
}
} else if (compareUID(uid, card2, mfrc522.uid.size)) {
if (!isPlaying) { // 如果未播放音樂
Serial.println("播放音樂 2");
myDFPlayer.play(2); // 播放第 2 首音樂
isPlaying = true; // 設置播放狀態為 true
copyUID(uid, lastUID, mfrc522.uid.size); // 儲存當前 UID
lastUIDSize = mfrc522.uid.size; // 儲存當前 UID 大小
}
} else {
Serial.println("未知卡片");
}
// 更新卡片被偵測的時間
lastCardRemoveTime = time;
}
// 如果卡片沒有被偵測到並且已經過了一段時間
else if (cardDetected && (time - lastCardRemoveTime >= stableDelay))
{
Serial.println("卡片移開,停止