ESP32 Cảm Biến Âm Thanh Servo Motor

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 servo motor. Cụ thể, chúng ta sẽ tìm hiểu hai ứng dụng thú vị:

Phần Cứng 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×Cáp USB Type-C
1×Cảm Biến Âm Thanh
1×động cơ servo
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ề Servo Motor và Cảm Biến Âm Thanh

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

Sơ Đồ Đấu Nối

ESP32 cảm biến âm thanh động cơ servo 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.

Code ESP32 - Công Tắc Âm Thanh chuyển đổi Góc của Servo Motor

Code dưới đây sẽ chuyển đổi góc của servo motor giữa 0 và 90 độ mỗi khi phát hiện âm thanh.

/* * 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-sound-sensor-servo-motor */ #include <ESP32Servo.h> #define SENSOR_PIN 25 // The ESP32 pin GPIO25 connected to the sound sensor #define SERVO_PIN 26 // The ESP32 pin GPIO26 connected to the servo motor Servo servo; // create servo object to control a servo // variables will change: int angle = 0; // the current angle of servo motor int prev_sound_state; // the previous state of sound sensor int sound_state; // the current state of sound sensor void setup() { Serial.begin(9600); // initialize serial pinMode(SENSOR_PIN, INPUT); // set ESP32 pin to input mode servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(angle); sound_state = digitalRead(SENSOR_PIN); } void loop() { prev_sound_state = sound_state; // save the last state sound_state = digitalRead(SENSOR_PIN); // read new state if (prev_sound_state == HIGH && sound_state == 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); } }

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

  • Nếu đây là lần đầu bạn sử dụng ESP32, hãy xem ESP32 - Cài Đặt Phần Mềm.
  • Thực hiện đấu nối như hình ảnh trên.
  • Kết nối board ESP32 với PC qua cáp micro USB
  • Mở Arduino IDE trên PC của bạn.
  • Chọn đúng board ESP32 (ví dụ: ESP32 Dev Module) và cổng COM.
  • Nhấp vào biểu tượng Libraries ở thanh bên trái của Arduino IDE.
  • ESP32Servo trong ô tìm kiếm, sau đó tìm thư viện servo của Kevin Harrington, John K. Bennett.
  • Nhấp nút Install để cài đặt thư viện servo motor cho ESP32.
ESP32 động cơ servo thư viện
  • Kết nối ESP32 với PC qua cáp USB
  • Mở Arduino IDE, chọn đúng board và cổng
  • Sao chép code trên và mở bằng Arduino IDE
  • Nhấp nút Upload trên Arduino IDE để tải code lên ESP32
  • Vỗ tay trước cảm biến âm thanh
  • Quan sát sự thay đổi của servo motor

Code ESP32 - Servo Motor kích hoạt bằng Âm Thanh trong một khoảng thời gian

Code dưới đây sẽ xoay servo motor đến 90 độ trong một khoảng thời gian khi phát hiện âm thanh. Sau khoảng thời gian đó, servo motor sẽ xoay trở lại 0 độ.

/* * 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-sound-sensor-servo-motor */ #include <ESP32Servo.h> #define SENSOR_PIN 25 // The ESP32 pin GPIO25 connected to the sound sensor #define SERVO_PIN 26 // The ESP32 pin GPIO26 connected to the servo motor #define TIME_PERIOD 5000 // in milliseconds Servo servo; // create servo object to control a servo // variables will change: int prev_sound_state; // the previous state of sound sensor int sound_state; // the current state of sound sensor void setup() { Serial.begin(9600); // initialize serial pinMode(SENSOR_PIN, INPUT); // set ESP32 pin to input mode servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(0); sound_state = digitalRead(SENSOR_PIN); } void loop() { prev_sound_state = sound_state; // save the last state sound_state = digitalRead(SENSOR_PIN); // read new state if (prev_sound_state == HIGH && sound_state == 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 code được đề cập ở trên sử dụng hàm delay() để đơn giản hóa. Tuy nhiên, nếu bạn thêm code khác, nó có thể bị chặn trong thời gian delay. Để giải quyết vấn đề này, code sau đây thực hiện cách tiếp cận không chặn bằng cách sử dụng hàm millis() thay vì delay để tránh bị chặn.

/* * 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-sound-sensor-servo-motor */ #include <ESP32Servo.h> #define SENSOR_PIN 25 // The ESP32 pin GPIO25 connected to the sound sensor #define SERVO_PIN 26 // The ESP32 pin GPIO26 connected to the servo motor #define TIME_PERIOD 5000 // in milliseconds Servo servo; // create servo object to control a servo // variables will change: int prev_sound_state; // the previous state of sound sensor int sound_state; // 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 ESP32 pin to input mode servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(angle); sound_state = digitalRead(SENSOR_PIN); } void loop() { prev_sound_state = sound_state; // save the last state sound_state = digitalRead(SENSOR_PIN); // read new state if (prev_sound_state == HIGH && sound_state == 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.

Bình Luận