반응형
이 튜토리얼에서는 ESP32 마이크로컨트롤러와 DFPlayer Mini 모듈을 사용하여 MP3 파일을 재생하는 방법을 보여드립니다. 구성 요소를 배선하고, MP3 파일을 DFPlayer Mini에 업로드하고, 재생을 제어하는 코드를 작성하는 방법을 배웁니다. 이 단계별 가이드는 초보자와 숙련된 제작자 모두에게 적합합니다.
사용 부품:
1-ESP32
2-디에프플레이어 미니
3-스피커
4-점퍼 와이어
연결

소스 코드
#include "Arduino.h" // Include the core Arduino library
#include "DFRobotDFPlayerMini.h" // Include the DFRobot DFPlayer Mini library
#ifdef ESP32
#define FPSerial Serial1 // For ESP32, use hardware serial port 1
#else
#include <SoftwareSerial.h> // Include SoftwareSerial library for non-ESP32 boards
SoftwareSerial FPSerial(16, 17); // Define SoftwareSerial on pins 16 (RX) and 17 (TX)
#endif
DFRobotDFPlayerMini myDFPlayer; // Create an instance of the DFRobotDFPlayerMini class
void setup() {
#ifdef ESP32
FPSerial.begin(9600, SERIAL_8N1, 16, 17); // Start serial communication for ESP32 with 9600 baud rate, 8 data bits, no parity, and 1 stop bit
#else
FPSerial.begin(9600); // Start serial communication for other boards with 9600 baud rate
#endif
Serial.begin(115200); // Start the Serial monitor communication with 115200 baud rate
Serial.println(F("DFRobot DFPlayer Mini Demo")); // Print a demo start message
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)")); // Print initialization message
if (!myDFPlayer.begin(FPSerial)) { // Initialize the DFPlayer Mini with the defined serial interface
Serial.println(F("Unable to begin:")); // If initialization fails, print an error message
Serial.println(F("1.Please recheck the connection!")); // Suggest rechecking the connection
Serial.println(F("2.Please insert the SD card!")); // Suggest checking for an inserted SD card
while(true); // Stay in an infinite loop if initialization fails
}
Serial.println(F("DFPlayer Mini online.")); // Print a success message if initialization succeeds
myDFPlayer.volume(30); // Set the DFPlayer Mini volume to 30 (max is 30)
myDFPlayer.play(1); // Start playing the first track on the SD card
}
void loop() {
static unsigned long timer = millis(); // Initialize a timer variable to track time elapsed
if (millis() - timer > 9000) { // Check if 9 seconds have passed
timer = millis(); // Reset the timer
myDFPlayer.next(); // Play the next MP3 track
}
if (myDFPlayer.available()) { // Check if the DFPlayer Mini has any new data available
uint8_t type = myDFPlayer.readType(); // Read the type of the message
int value = myDFPlayer.read(); // Read the message value
if (type == DFPlayerPlayFinished) { // Check if the message type indicates the end of a track
Serial.print(F("Finished playing track ")); // Print a message to the Serial monitor
Serial.println(value); // Print the track number that just finished playing
}
}
}
반응형
'ESP32' 카테고리의 다른 글
| ESP32-S3 DevKitC 핀아웃 레퍼런스 가이드: GPIO 설명 (6) | 2024.10.05 |
|---|---|
| ESP32-S3-WROOM-1 개발보드 처음 연결 (2) | 2024.10.05 |
| ESP32 아두이노 IDE 에서 사용하기 (3) | 2024.10.05 |
| ESP32-C3FH4 BLE5.0/WiFi 보드 (3) | 2024.09.30 |
| ESP32 및 DS18B20 디지털 1-wire 온도계 인터페이스하기 (2) | 2024.09.25 |
| HC-SR04 초음파 거리 센서와 함께 ESP32 사용 (0) | 2024.09.24 |
| Arduino IDE로 ESP32를 프로그래밍하는 방법 (5) | 2024.09.23 |
| ESP32에 I2C LCD를 연결하는 방법 (1) | 2024.09.20 |
취업, 창업의 막막함, 외주 관리, 제품 부재!
당신의 고민은 무엇입니까? 현실과 동떨어진 교육, 실패만 반복하는 외주 계약,
아이디어는 있지만 구현할 기술이 없는 막막함.
우리는 알고 있습니다. 문제의 원인은 '명확한 학습, 실전 경험과 신뢰할 수 있는 기술력의 부재'에서 시작됩니다.
이제 고민을 멈추고, 캐어랩을 만나세요!
코딩(펌웨어), 전자부품과 디지털 회로설계, PCB 설계 제작, 고객(시장/수출) 발굴과 마케팅 전략으로 당신을 지원합니다.
제품 설계의 고수는 성공이 만든 게 아니라 실패가 만듭니다. 아이디어를 양산 가능한 제품으로!
귀사의 제품을 만드세요. 교육과 개발 실적으로 신뢰할 수 있는 파트너를 확보하세요.
캐어랩