Home Coming Music
keyhold
Home Coming Music
keyhold
Music can be very confort. I want to know how to drive the sound output by Arduino, DFPlayer Mini and NFC, then bring rhythm to living place.
A key holder can play music, when it detect the NFC on the key chain, the holder could play music. The potentiometer could adjust the volume of sound.
Images of final creative/exploratory output
Final image for the Internal Circuit
Final video for the Internal Circuit
Process images from development towards creative/exploratory output
The ~10 is not for DFPlayer's software serial.
Adding the 1k resistor, could highly improve the sound quality and reduce the noise
Process and reflection:
We trying to solve the pin ~10 problem for very long time. If connect pin ~10 with the DFPlayer, even the code is correct, the device is unable to operation. Finally we find that there was a hardware conflict on pin 10, which is part of the spi bus. once we used a different pin for the DFPlayer's software serial, it immediately started working.
List the pin number and usage as column in begin of the Arduiro is very useful.
The usage and impact of resistor in reduce the noise is more impactful than I think.
Technical details
[[Electrical schematic of Music Key holder]
/*
adding the NCF function to MP3 player
pin mapping
Arduino pin | role | description
------------|-------|--------------
3 output MP3 player TX pin (DFPlayer's RX pin)
5 input MP3 player RX pin (DFPlayer's TX pin)
6 output RFID reader SDA pin
9 output RFID reader reset pin
11 output RFID reader MOSI pin
12 output RFID reader MISO pin
13 output RFID reader SCK pin
A0 input potentiometer (volume control)
*/
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
#include <SPI.h>
#include <MFRC522.h>
// 软件串口:DFPlayer TX → D5, RX → D3
SoftwareSerial softSerial(5, 3); // (Arduino RX=5, TX=3)
DFRobotDFPlayerMini myDFPlayer;
const int potPin = A0; // 电位器输入引脚
int volumeLevel = 20; // 初始音量
unsigned long lastVolumeUpdate = 0;
#define SS_PIN 6
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
// Init array that will store new NUID
byte nuidPICC[4];
void setup() {
Serial.begin(9600);
softSerial.begin(9600);
// 初始化 DFPlayer
if (!myDFPlayer.begin(softSerial)) {
Serial.println(F("DFPlayer Mini not detected!"));
while (true)
;
}
Serial.println(F("DFPlayer Mini online."));
// 设置初始音量
myDFPlayer.volume(volumeLevel);
Serial.print(F("Initial volume: "));
Serial.println(volumeLevel);
// 自动播放 0001.mp3(只播放一次)
//myDFPlayer.play(1);
//Serial.println(F("Playing track 1..."));
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
}
void loop() {
// ----- 读取电位器并调节音量 -----
int potValue = analogRead(potPin); // 0~1023
int newVolume = map(potValue, 0, 1023, 0, 30); // 映射到 0~30
newVolume = constrain(newVolume, 0, 30);
if (rfid.PICC_IsNewCardPresent()) {
Serial.println("new card read");
delay(50);
myDFPlayer.play(1);
Serial.println(F("Playing track 1..."));
}
unsigned long now = millis();
if (now - lastVolumeUpdate > 500) { // 每 500ms 更新一次
if (newVolume != volumeLevel) {
volumeLevel = newVolume;
myDFPlayer.volume(volumeLevel);
Serial.print(F("Volume changed to: "));
Serial.println(volumeLevel);
}
lastVolumeUpdate = now;
}
// 无循环播放,不做任何操作
}