Arduino Máy nghe MP3

Trong bài hướng dẫn này, chúng ta sẽ học cách làm một trình phát MP3 bằng Arduino, module phát MP3, thẻ micro SD và loa. Các tệp MP3 (nhạc hoặc âm thanh đã ghi) được lưu trên thẻ micro SD. Sau đó, Arduino có thể điều khiển module phát MP3 để đọc một bài hát được chọn từ thẻ SD, chuyển đổi nó thành tín hiệu âm thanh và gửi tín hiệu đó tới loa. Cụ thể, chúng ta sẽ học:

Sau đó, bạn có thể chỉnh sửa mã để thêm một biến trở hoặc encoder quay để điều chỉnh âm lượng.

Phần cứng cần chuẩn bị

1×Arduino Uno R3
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×Serial MP3 Player Module
1×Micro SD Card
1×3.5mm Aux Speaker
1×dây jumper
1×(Khuyến nghị) Screw Terminal Block Shield for Arduino Uno
1×(Khuyến nghị) Breadboard Shield for Arduino Uno
1×(Khuyến nghị) Enclosure for Arduino Uno
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

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

Về mô-đun phát MP3 nối tiếp và loa

Bố trí các chân của mô-đun MP3 phát qua Serial

Một mô-đun trình phát MP3 nối tiếp có ba giao diện:

  • Giao diện với Arduino bao gồm 4 chân:
    • Chân RX: chân dữ liệu, cần được kết nối với chân TX của Arduino (Hardware hoặc Software Serial)
    • Chân TX: chân dữ liệu, cần được kết nối với chân RX của Arduino (Hardware hoặc Software Serial)
    • Chân VCC: chân nguồn, cần được kết nối với VCC (5V)
    • Chân GND: chân nguồn, cần được kết nối với GND (0V)
  • Giao diện với loa là jack AUX 3,5 mm nữ.
  • Giao diện với thẻ Micro SD là một khe cắm thẻ Micro SD ở phía sau của mô-đun.
bố trí chân của module phát mp3 serial
image source: diyables.io

Bố trí chân loa

Một loa thường có hai giao diện:

  • Giao diện tín hiệu âm thanh: nó là đầu nối Aux 3,5 mm nam kết nối với mô-đun MP3
  • Giao diện nguồn: nó có thể là USB, bộ nguồn 5V hoặc bất kỳ giao diện cấp nguồn nào khác

Cách hoạt động

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

  • Lưu trữ trước danh sách các bài hát hoặc âm thanh đã ghi mà chúng ta muốn phát trên thẻ nhớ micro SD.
  • Lắp thẻ nhớ micro SD vào module phát MP3.
  • Kết nối module phát MP3 với Arduino.
  • Kết nối loa với module MP3 đến một
  • Kết nối loa với nguồn cấp điện.

Mỗi tệp MP3 được lưu trên thẻ Micro SD sẽ có một chỉ mục. Chỉ mục cho biết thứ tự của bài hát được lưu, bắt đầu từ 0.

Sau đó chúng ta có thể lập trình Arduino để gửi lệnh cho module phát MP3. Nó hỗ trợ các lệnh sau:

  • Phát
  • Tạm dừng
  • Phát tiếp theo
  • Phát trước
  • Điều chỉnh âm lượng

Khi mô-đun phát MP3 hoạt động, nó đọc tệp MP3 từ thẻ microSD, chuyển các tệp MP3 thành tín hiệu âm thanh và xuất tín hiệu âm thanh tới loa qua giao diện Aux 3,5 mm.

Sơ đồ đấu dây

sơ đồ nối dây của module mp3 Arduino

This image is created using Fritzing. Click to enlarge image

Mã Arduino - Phát nhạc

Đoạn mã dưới đây phát bài hát đầu tiên được lưu trên thẻ nhớ microSD.

/* * Mã Arduino này được phát triển bởi newbiely.vn * Mã Arduino 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/arduino-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 // Arduino Pin connected to the TX of the Serial MP3 Player module #define ARDUINO_TX 6 // Arduino 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]); } }

Hướng dẫn từng bước

  • Làm theo các hướng dẫn trên Cách hoạt động
  • Sao chép mã ở trên và mở bằng Arduino IDE
  • Nhấn nút Tải lên trên Arduino IDE để tải mã lên Arduino
  • Nghe nhạc

Mã Arduino - Phát nhạc với các nút điều khiển

Đoạn mã dưới đây là một bản nâng cấp của mã trước đó. Nó thêm bốn nút để bạn có thể tương tác với trình phát MP3.

/* * Mã Arduino này được phát triển bởi newbiely.vn * Mã Arduino 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/arduino-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 // Arduino Pin connected to the TX of the Serial MP3 Player module #define ARDUINO_TX 6 // Arduino 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]); } }

Sơ đồ mạch cho mã ở trên:

sơ đồ đấu nối loa cho Arduino mp3

This image is created using Fritzing. Click to enlarge image

Bây giờ, bạn có thể chỉnh sửa các 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