Arduino Cảm biến âm thanh Động cơ servo

Trong hướng dẫn này, chúng ta sẽ khám phá cách sử dụng cảm biến âm thanh để điều khiển động cơ servo. Cụ thể, chúng ta sẽ đi sâu vào hai ứng dụng thú vị:

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×Sound Sensor
1×động cơ servo
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ề động cơ servo và cảm biến âm thanh

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

Sơ đồ đấu dây

sơ đồ nối dây cảm biến âm thanh Arduino với động cơ servo

This image is created using Fritzing. Click to enlarge image

Mã Arduino - Công tắc âm thanh chuyển đổi góc của động cơ servo

Mã dưới đây sẽ điều chỉnh góc của động cơ servo giữa 0 và 90 độ mỗi khi phát hiện âm thanh.

/* * 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-sound-sensor-servo-motor */ #include <Servo.h> #define SENSOR_PIN A0 // Arduino pin connected to sound sensor's pin #define SERVO_PIN 9 // Arduino pin connected to servo motor's pin Servo servo; // create servo object to control a servo // variables will change: int angle = 0; // the current angle of servo motor int lastSoundState; // the previous state of sound sensor int currentSoundState; // the current state of sound sensor void setup() { Serial.begin(9600); // initialize serial pinMode(SENSOR_PIN, INPUT); // set arduino pin to input mode servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(angle); currentSoundState = digitalRead(SENSOR_PIN); } void loop() { lastSoundState = currentSoundState; // save the last state currentSoundState = digitalRead(SENSOR_PIN); // read new state if (lastSoundState == HIGH && currentSoundState == LOW) { // state change: HIGH -> LOW Serial.println("The sound has been detected"); // change angle of servo motor if (angle == 0) angle = 90; else if (angle == 90) angle = 0; // control servo motor arccoding to the angle servo.write(angle); } }

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

  • Kết nối Arduino với máy tính qua cáp USB
  • Mở Arduino IDE, chọn bo mạch 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 Arduino
  • Vỗ tay trước cảm biến âm thanh
  • Quan sát sự thay đổi của động cơ servo

Mã Arduino - Động cơ servo kích hoạt bằng âm thanh trong một khoảng thời gian

Đoạn mã dưới đây quay động cơ servo đến 90 độ trong một khoảng thời gian khi phát hiện âm thanh. Sau khoảng thời gian đó, động cơ servo quay về 0 độ.

/* * 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-sound-sensor-servo-motor */ #include <Servo.h> #define SENSOR_PIN A0 // Arduino pin connected to sound sensor's pin #define SERVO_PIN 9 // Arduino pin connected to servo motor's pin #define TIME_PERIOD 5000 // in milliseconds Servo servo; // create servo object to control a servo // variables will change: int lastSoundState; // the previous state of sound sensor int currentSoundState; // the current state of sound sensor void setup() { Serial.begin(9600); // initialize serial pinMode(SENSOR_PIN, INPUT); // set arduino pin to input mode servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(0); currentSoundState = digitalRead(SENSOR_PIN); } void loop() { lastSoundState = currentSoundState; // save the last state currentSoundState = digitalRead(SENSOR_PIN); // read new state if (lastSoundState == HIGH && currentSoundState == LOW) { // state change: HIGH -> LOW Serial.println("The sound has been detected"); servo.write(90); // control servo motor to 90 degree delay(TIME_PERIOD); servo.write(0); // control servo motor to 0 degree } }

Xin lưu ý rằng đoạn mã được đề cập ở trên sử dụng hàm delay() để đơn giản hóa. Tuy nhiên, nếu bạn bổ sung thêm mã, nó có thể bị treo trong thời gian trì hoãn. Để giải quyết vấn đề này, đoạn mã dưới đây triển khai một phương pháp không chặn bằng cách sử dụng hàm millis() thay cho delay để ngăn chặn việc bị treo.

/* * 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-sound-sensor-servo-motor */ #include <Servo.h> #define SENSOR_PIN A0 // Arduino pin connected to sound sensor's pin #define SERVO_PIN 9 // Arduino pin connected to servo motor's pin #define TIME_PERIOD 5000 // in milliseconds Servo servo; // create servo object to control a servo // variables will change: int lastSoundState; // the previous state of sound sensor int currentSoundState; // the current state of sound sensor unsigned long lastTime; // the current state of sound sensor int angle = 0; void setup() { Serial.begin(9600); // initialize serial pinMode(SENSOR_PIN, INPUT); // set arduino pin to input mode servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(angle); currentSoundState = digitalRead(SENSOR_PIN); } void loop() { lastSoundState = currentSoundState; // save the last state currentSoundState = digitalRead(SENSOR_PIN); // read new state if (lastSoundState == HIGH && currentSoundState == LOW) { // state change: HIGH -> LOW Serial.println("The sound has been detected"); angle = 90; servo.write(angle); // control servo motor to 90 degree lastTime = millis(); } if (angle == 90 && (millis() - lastTime) > TIME_PERIOD) { angle = 0; servo.write(angle); // control servo motor to 0 degree } }

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.