ESP32 Máy Phát MP3

Trong hướng dẫn này, chúng ta sẽ khám phá cách tạo máy phát MP3 sử dụng ESP32, module phát MP3, thẻ Micro SD Card và loa. Các file MP3, có thể là nhạc hoặc âm thanh đã ghi, được lưu trữ trên thẻ micro SD Card. ESP32 sẽ được lập trình để gửi lệnh điều khiển module phát MP3 chọn bài nào từ thẻ SD, chuyển đổi thành âm thanh, và sau đó gửi âm thanh đó đến loa. Chúng ta sẽ tìm hiểu các khía cạnh sau:

Sau đó, bạn có thể mở rộng code bằng cách kết hợp potentiometer hoặc rotary encoder để điều chỉnh âm lượng.

Linh Kiện Cần Thiết

1×mô-đun phát triển ESP-WROOM-32
1×Alternatively, ESP32 Uno-form board
1×Alternatively, ESP32 S3 Uno-form board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Serial MP3 Player Module
1×Micro SD Card
1×3.5mm Aux Speaker
1×breadboard
1×dây jumper
1×(Khuyến nghị) Screw Terminal Expansion Board for ESP32
1×(Khuyến nghị) Breakout Expansion Board for ESP32
1×(Khuyến nghị) Power Splitter for ESP32

Or you can buy the following kits:

1×DIYables ESP32 Starter Kit (ESP32 included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về Serial MP3 Player Module và Loa

Pinout của Serial MP3 Player Module

Một module phát MP3 nпоследовательный có ba giao diện:

  • Giao diện với ESP32 bao gồm 4 chân:
    • Chân RX: chân data, cần kết nối với chân TX của ESP32
    • Chân TX: chân data, cần kết nối với chân RX của ESP32
    • Chân VCC: chân nguồn, cần kết nối với VCC (3.3V hoặc 5V)
    • Chân GND: chân nguồn, cần kết nối với GND (0V)
  • Giao diện với loa là jack cắm Aux 3.5mm ra.
  • Giao diện với thẻ Micro SD Card là Socket thẻ Micro SD Card ở mặt sau của module.
serial mp3 player module sơ đồ chân

Pinout Loa

Loa thường có hai giao diện:

  • Giao diện tín hiệu âm thanh: là connector Aux 3.5mm male kết nối với module phát MP3
  • Giao diện nguồn: có thể là USB, adapter nguồn 5V hoặc bất kỳ giao diện nguồn nào khác

Cách Hoạt Động

Để bắt đầu, hãy đảm bảo bạn có những thứ sau:

  • Chuẩn bị một bộ sưu tập các bài hát hoặc âm thanh đã ghi mà bạn muốn phát và lưu chúng trên thẻ micro SD Card.
  • Đưa thẻ micro SD Card vào module phát MP3.
  • Kết nối module phát MP3 với ESP32 và kết nối loa với module phát MP3. Đồng thời, đảm bảo loa được kết nối với nguồn điện.

Mỗi file MP3 trên thẻ micro SD Card sẽ có một ID, bắt đầu từ 0. Sau đó, bạn có thể yêu cầu ESP32 thực hiện nhiều việc khác nhau với module phát MP3, chẳng hạn như:

  • Phát: Bắt đầu phát bài hát đã chọn.
  • Tạm dừng: Tạm dừng bài hát.
  • Phát tiếp theo: Chuyển sang bài hát tiếp theo.
  • Phát trước đó: Quay lại bài hát trước đó.
  • Thay đổi âm lượng: Điều chỉnh độ to của nhạc.

Khi module phát MP3 nhận được lệnh, nó sẽ đọc file MP3 từ thẻ micro SD Card, chuyển đổi thành tín hiệu âm thanh, và gửi tín hiệu đó đến loa thông qua kết nối Aux 3.5mm.

Sơ Đồ Đấu Nối

  • Cách kết nối ESP32 và module phát mp3 sử dụng breadboard
ESP32 mp3 player module sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

Nếu bạn chưa rõ cách cấp nguồn cho ESP32 và các linh kiện khác, xem: Cách Cung Cấp Nguồn Điện Cho ESP32.

how to connect ESP32 and mp3 player

Code ESP32 - Phát Nhạc

Code dưới đây phát bài hát đầu tiên được lưu trữ trên thẻ Micro SD Card.

/* * Mã ESP32 này được phát triển bởi newbiely.vn * Mã ESP32 này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/esp32/esp32-mp3-player */ #define CMD_PLAY_NEXT 0x01 #define CMD_PLAY_PREV 0x02 #define CMD_PLAY_W_INDEX 0x03 #define CMD_SET_VOLUME 0x06 #define CMD_SEL_DEV 0x09 #define CMD_PLAY_W_VOL 0x22 #define CMD_PLAY 0x0D #define CMD_PAUSE 0x0E #define CMD_SINGLE_CYCLE 0x19 #define DEV_TF 0x02 #define SINGLE_CYCLE_ON 0x00 #define SINGLE_CYCLE_OFF 0x01 void setup() { Serial.begin(9600); Serial2.begin(9600, SERIAL_8N1, 16, 17); delay(500); // wait chip initialization is complete mp3_command(CMD_SEL_DEV, DEV_TF); // select the TF card delay(200); // wait for 200ms mp3_command(CMD_PLAY, 0x0000); // Play mp3 //mp3_command(CMD_PAUSE, 0x0000); // Pause mp3 //mp3_command(CMD_PLAY_NEXT, 0x0000); // Play next mp3 //mp3_command(CMD_PLAY_PREV, 0x0000); // Play previous mp3 //mp3_command(CMD_SET_VOLUME, 30); // Change volume to 30 } void loop() { } void mp3_command(int8_t command, int16_t dat) { int8_t frame[8] = { 0 }; frame[0] = 0x7e; // starting byte frame[1] = 0xff; // version frame[2] = 0x06; // the number of bytes of the command without starting byte and ending byte frame[3] = command; // frame[4] = 0x00; // 0x00 = no feedback, 0x01 = feedback frame[5] = (int8_t)(dat >> 8); // data high byte frame[6] = (int8_t)(dat); // data low byte frame[7] = 0xef; // ending byte for (uint8_t i = 0; i < 8; i++) { Serial2.write(frame[i]); } }

Các Bước Thực Hiện

  • Nếu đây là lần đầu tiên bạn sử dụng ESP32, hãy xem ESP32 - Cài Đặt Phần Mềm.
  • Làm theo hướng dẫn trong phần Cách Hoạt Động
  • Sao chép code ở trên và mở bằng Arduino IDE
  • Nhấp nút Upload trên Arduino IDE để upload code lên ESP32
  • Thưởng thức âm nhạc

Code ESP32 - Phát Nhạc với Nút Điều Khiển

Code dưới đây là phiên bản nâng cấp của code trước đó. Nó thêm bốn nút để cho phép bạn tương tác với máy phát MP3.

/* * Mã ESP32 này được phát triển bởi newbiely.vn * Mã ESP32 này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/esp32/esp32-mp3-player */ #include <ezButton.h> #define CMD_PLAY_NEXT 0x01 #define CMD_PLAY_PREV 0x02 #define CMD_PLAY_W_INDEX 0x03 #define CMD_SET_VOLUME 0x06 #define CMD_SEL_DEV 0x09 #define CMD_PLAY_W_VOL 0x22 #define CMD_PLAY 0x0D #define CMD_PAUSE 0x0E #define CMD_SINGLE_CYCLE 0x19 #define DEV_TF 0x02 #define SINGLE_CYCLE_ON 0x00 #define SINGLE_CYCLE_OFF 0x01 ezButton button_play(32); // create ezButton object that attach to ESP32's pin GPIO32 ezButton button_pause(33); // create ezButton object that attach to ESP32's pin GPIO33 ezButton button_next(25); // create ezButton object that attach to ESP32's pin GPIO25 ezButton button_prev(26); // create ezButton object that attach to ESP32's pin GPIO26 void setup() { Serial.begin(9600); Serial2.begin(9600, SERIAL_8N1, 16, 17); delay(500); // wait chip initialization is complete mp3_command(CMD_SEL_DEV, DEV_TF); // select the TF card delay(200); // wait for 200ms button_play.setDebounceTime(50); // set debounce time to 50 milliseconds button_pause.setDebounceTime(50); // set debounce time to 50 milliseconds button_next.setDebounceTime(50); // set debounce time to 50 milliseconds button_prev.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { button_play.loop(); // MUST call the loop() function first button_pause.loop(); // MUST call the loop() function first button_next.loop(); // MUST call the loop() function first button_prev.loop(); // MUST call the loop() function first if (button_play.isPressed()) { Serial.println("Play mp3"); mp3_command(CMD_PLAY, 0x0000); } if (button_pause.isPressed()) { Serial.println("Pause mp3"); mp3_command(CMD_PAUSE, 0x0000); } if (button_next.isPressed()) { Serial.println("Play next mp3"); mp3_command(CMD_PLAY_NEXT, 0x0000); } if (button_prev.isPressed()) { Serial.println("Play previous mp3"); mp3_command(CMD_PLAY_PREV, 0x0000); } } void mp3_command(int8_t command, int16_t dat) { int8_t frame[8] = {0}; frame[0] = 0x7e; // starting byte frame[1] = 0xff; // version frame[2] = 0x06; // the number of bytes of the command without starting byte and ending byte frame[3] = command; // frame[4] = 0x00; // 0x00 = no feedback, 0x01 = feedback frame[5] = (int8_t)(dat >> 8); // data high byte frame[6] = (int8_t)(dat); // data low byte frame[7] = 0xef; // ending byte for (uint8_t i = 0; i < 8; i++) { Serial2.write(frame[i]); } }

Sơ đồ đấu nối cho code ở trên:

ESP32 mp3 player speaker sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

Bây giờ, bạn có thể chỉnh sửa dự án để thêm nhiều chức năng hơn, ví dụ:

Video Tutorial

Việc sản xuất video tốn rất nhiều thời gian. Nếu video hướng dẫn hữu ích cho việc học của bạn, hãy đăng ký kênh YouTube để ủng hộ. Nếu nhu cầu đủ cao, chúng tôi sẽ cố gắng làm thêm nhiều video.

Tham Khảo Hàm

Bình Luận