본문 바로가기

ESP32

ESP32로 MP3 파일을 재생하는 방법

반응형

 

이 튜토리얼에서는 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
    }
  }
}

 

 

 

 

 

반응형

캐어랩 고객 지원

취업, 창업의 막막함, 외주 관리, 제품 부재!

당신의 고민은 무엇입니까? 현실과 동떨어진 교육, 실패만 반복하는 외주 계약, 아이디어는 있지만 구현할 기술이 없는 막막함.

우리는 알고 있습니다. 문제의 원인은 '명확한 학습, 실전 경험과 신뢰할 수 있는 기술력의 부재'에서 시작됩니다.

이제 고민을 멈추고, 캐어랩을 만나세요!

코딩(펌웨어), 전자부품과 디지털 회로설계, PCB 설계 제작, 고객(시장/수출) 발굴과 마케팅 전략으로 당신을 지원합니다.

제품 설계의 고수는 성공이 만든 게 아니라 실패가 만듭니다. 아이디어를 양산 가능한 제품으로!

귀사의 제품을 만드세요. 교육과 개발 실적으로 신뢰할 수 있는 파트너를 확보하세요.

지난 30년 여정, 캐어랩이 얻은 모든 것을 함께 나누고 싶습니다.

카카오 채널 추가하기

카톡 채팅방에서 무엇이든 물어보세요

당신의 성공을 위해 캐어랩과 함께 하세요.

캐어랩 온라인 채널 바로가기

캐어랩