Arduino Cảm biến chuyển động Trình phát MP3

Trong hướng dẫn này, chúng ta sẽ tìm hiểu cách sử dụng Arduino, cảm biến chuyển động HC-SR501 và một trình phát MP3 để kích hoạt việc phát lại một tệp âm thanh đã được ghi lại khi phát hiện chuyển động. Dự án này mang tính linh hoạt và có thể được tùy chỉnh cho các ứng dụng như tự động phát các chỉ dẫn âm thanh đã ghi hoặc cảnh báo bất cứ khi nào có sự xuất hiện của con người.

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×HC-SR501 Motion Sensor
1×Alternatively, AM312 Mini Motion Sensor
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áy nghe nhạc MP3 và Cảm biến chuyển động

Nếu bạn chưa biết về trình phát MP3 và cảm biến chuyển động (bố trí chân, cách hoạt động, cách lập trình ...), hãy tìm hiểu về chúng trong các bài hướng dẫn sau:

Sơ đồ đấu dây

sơ đồ nối dây Arduino với cảm biến chuyển động và trình phát mp3

This image is created using Fritzing. Click to enlarge image

Cài đặt ban đầu của cảm biến chuyển động

Time Delay AdjusterScrew it in anti-clockwise direction fully.
Detection Range AdjusterScrew it in clockwise direction fully.
Repeat Trigger SelectorPut jumper as shown on the image.
cài đặt ban đầu cho cảm biến chuyển động Arduino

Chuẩn bị

  • Lưu trữ trước tập tin MP3 đã ghi mà chúng ta muốn phát vào thẻ nhớ micro SD.
  • Lắp thẻ nhớ micro SD vào module MP3.
  • Kết nối module MP3 với Arduino.
  • Kết nối loa với module MP3.
  • Kết nối loa với nguồn cấp điện.
  • Kết nối cảm biến chuyển động với Arduino.

Mã Arduino - Cảm biến chuyển động điều khiển máy nghe nhạc 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-motion-sensor-mp3-player */ #include <SoftwareSerial.h> #include <Servo.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 #define MOTION_SENSOR_PIN 2 // Arduino pin connected to motion sensor's pin SoftwareSerial mp3(ARDUINO_RX, ARDUINO_TX); int prev_motion_state; // the previous state of motion sensor int motion_state; // the current state of motion sensor 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 pinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input mode motion_state = digitalRead(MOTION_SENSOR_PIN); } void loop() { prev_motion_state = motion_state; // save the last state motion_state = digitalRead(MOTION_SENSOR_PIN); // read new state if (motion_state == LOW && prev_motion_state == HIGH) { // pin state change: LOW -> HIGH Serial.println("Motion detected!"); mp3_command(CMD_PLAY, 0x0000); // Play the first mp3 file } else if (motion_state == HIGH && prev_motion_state == LOW) { // pin state change: HIGH -> LOW Serial.println("Motion stopped!"); } } 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

  • Kết nối Arduino với máy tính bằng cáp USB
  • Mở Arduino IDE, chọn board và cổng đúng
  • Sao chép mã ở trên và mở bằng Arduino IDE
  • Nhấp vào nút Tải lên trên Arduino IDE để tải mã lên cho Arduino
  • Di chuyển tay trước cảm biến
  • Kiểm tra âm thanh từ máy nghe MP3

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.