Arduino UNO R4 Máy Phát MP3

Hướng dẫn này sẽ chỉ cho bạn cách tạo máy phát MP3 sử dụng Arduino UNO R4, module phát MP3, thẻ Micro SD và loa. Máy phát MP3 sẽ tải nhạc hoặc file âm thanh từ thẻ Micro SD. Arduino UNO R4 điều khiển module phát MP3 để chọn và phát một bài hát từ thẻ, chuyển đổi thành tín hiệu âm thanh và gửi tín hiệu này đến loa. Chúng ta sẽ tìm hiểu chi tiết các bước sau:

Sau đó, bạn có thể chỉnh sửa code để thêm potentiometer hoặc rotary encoder để điều chỉnh âm lượng.

Arduino UNO R4 mp3 player

Linh Kiện Cần Thiết

1×Arduino UNO R4 WiFi hoặc Arduino UNO R4 Minima
1×(Tùy chọn) DIYables STEM V4 IoT, tương thích với Arduino Uno R4 WiFi
1×Arduino UNO R4 Minima (Thay thế)
1×Cáp USB Type-C
1×Module Phát MP3 Serial
1×Thẻ Micro SD
1×Loa 3.5mm Aux
1×Dây Jumper
1×(Khuyến nghị) Screw Terminal Block Shield for Arduino UNO R4
1×(Khuyến nghị) Breadboard Shield for Arduino UNO R4
1×(Khuyến nghị) Enclosure for Arduino UNO R4
1×(Khuyến nghị) Power Splitter for Arduino UNO R4
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

1×DIYables STEM V4 IoT Starter Kit (Arduino included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về Module Phát MP3 Serial và Loa

Sơ Đồ Chân Module Phát MP3 Serial

Module phát MP3 serial bao gồm ba giao diện:

  • Giao diện với Arduino UNO R4: có bốn chân:
    • Chân RX: Dành cho dữ liệu và cần kết nối với chân TX trên Arduino UNO R4, sử dụng Hardware hoặc Software Serial.
    • Chân TX: Cũng dành cho dữ liệu và cần kết nối với chân RX trên Arduino UNO R4, thông qua Hardware hoặc Software Serial.
    • Chân VCC: Dành cho nguồn điện và cần kết nối với VCC (5V).
    • Chân GND: Là chân ground và cần kết nối với GND (0V).
  • Giao diện với loa: jack cái đầu ra 3.5mm Aux.
  • Giao diện với thẻ Micro SD: Socket thẻ Micro SD nằm ở mặt sau của module.
serial mp3 player module sơ đồ chân
image source: diyables.io

Sơ Đồ Chân Loa

Loa thường có hai điểm kết nối:

  • Kết nối âm thanh: Sử dụng đầu nối 3.5mm Aux đực để kết nối với máy phát MP3.
  • Kết nối nguồn: Có thể sử dụng USB, adapter nguồn 5V hoặc các loại kết nối nguồn khác.

Cách Hoạt Động

Những gì chúng ta cần chuẩn bị:

  • Lưu danh sách bài hát hoặc file ghi âm vào thẻ micro SD.
  • Đặt thẻ micro SD vào module phát MP3.
  • Kết nối module phát MP3 với Arduino UNO R4.
  • Kết nối module phát MP3 với loa.
  • Kết nối loa với nguồn điện.

Mỗi file MP3 trên thẻ Micro SD có một số bắt đầu từ 0 để thể hiện thứ tự của các bài hát.

Sau đó chúng ta có thể thiết lập Arduino UNO R4 để gửi các lệnh đến module phát MP3. Nó có thể xử lý các lệnh sau:

  • Bắt Đầu Phát
  • Dừng
  • Phát Tiếp Theo
  • Phát Trước Đó
  • Điều Chỉnh Âm Lượng

Module phát MP3 phát file MP3 được lưu trữ trên thẻ micro SD, chuyển đổi thành tín hiệu âm thanh và gửi tín hiệu này đến loa thông qua giao diện 3.5mm Aux.

Sơ Đồ Kết Nối

Arduino UNO R4 mp3 player module sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

Code Arduino UNO R4 - Phát Nhạc

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

/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 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/arduino-uno-r4/arduino-uno-r4-mp3-player */ #include <SoftwareSerial.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 #define ARDUINO_RX 7 // The Arduino UNO R4 pin connected to the TX of the Serial MP3 Player module #define ARDUINO_TX 6 // The Arduino UNO R4 pin connected to the RX of the Serial MP3 Player module SoftwareSerial mp3(ARDUINO_RX, ARDUINO_TX); void setup() { Serial.begin(9600); mp3.begin(9600); 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++) { mp3.write(frame[i]); } }

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

Thực hiện theo các hướng dẫn từng bước:

  • Nếu đây là lần đầu bạn sử dụng Arduino Uno R4 WiFi/Minima, hãy tham khảo hướng dẫn Arduino UNO R4 - Cài Đặt Phần Mềm.
  • Kết nối các linh kiện theo sơ đồ được cung cấp.
  • Kết nối board Arduino Uno R4 với máy tính của bạn bằng cáp USB.
  • Khởi động Arduino IDE trên máy tính.
  • Chọn board Arduino Uno R4 phù hợp (ví dụ: Arduino Uno R4 WiFi) và cổng COM.
  • Thực hiện theo các bước trong phần Cách Hoạt Động
  • Sao chép code được cung cấp và sử dụng trong Arduino IDE
  • Nhấn nút Upload trong Arduino IDE để gửi code đến Arduino UNO R4
  • Tận hưởng âm nhạc

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

Code dưới đây là phiên bản cải tiến của code trước đó. Nó bao gồm bốn nút cho phép bạn điều khiển máy phát MP3.

/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 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/arduino-uno-r4/arduino-uno-r4-mp3-player */ #include <SoftwareSerial.h> #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 #define ARDUINO_RX 7 // The Arduino UNO R4 pin connected to the TX of the Serial MP3 Player module #define ARDUINO_TX 6 // The Arduino UNO R4 pin connected to the RX of the Serial MP3 Player module SoftwareSerial mp3(ARDUINO_RX, ARDUINO_TX); ezButton button_play(2); // create ezButton object that attach to pin 2 ezButton button_pause(3); // create ezButton object that attach to pin 3 ezButton button_next(4); // create ezButton object that attach to pin 4 ezButton button_prev(5); // create ezButton object that attach to pin 5 void setup() { Serial.begin(9600); mp3.begin(9600); 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++) { mp3.write(frame[i]); } }

Các kết nối dây cho code được đề cập:

Arduino UNO R4 mp3 player speaker sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

Bây giờ, bạn có thể thay đổi dự án để thêm nhiều tính 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