Arduino Cảm biến âm thanh Rơ-le

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 rơ-le. Cụ thể, chúng ta sẽ đi sâu vào hai ứng dụng thú vị:

Bằng cách kết nối rơ-le với bóng đèn, dải đèn LED, động cơ hoặc cơ cấu truyền động... Chúng ta có thể dùng cảm biến âm thanh để điều khiển bóng đèn, dải đèn LED, động cơ hoặc cơ cấu truyền độ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×Sound Sensor
1×Relay
1×dây jumper
1×(Tùy chọn) Solenoid Lock
1×(Tùy chọn) 12V Power Adapter
1×(Tùy chọn) DC Power Jack
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ề rơ-le và cảm biến âm thanh

Nếu bạn chưa biết về rơ-le 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ơ đồ đấu dây rơ-le cảm biến âm thanh Arduino

This image is created using Fritzing. Click to enlarge image

Mã Arduino - Công tắc âm thanh bật/tắt rơ-le

Đoạn mã dưới đây sẽ đảo trạng thái của rơ-le mỗi khi âm thanh được phát hiện.

/* * 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-relay */ #define SENSOR_PIN A0 // Arduino pin connected to sound sensor's pin #define RELAY_PIN A2 // Arduino pin connected to LED's pin int lastSoundState; // the previous state of sound sensor int currentSoundState; // the current state of sound sensor int relayState = LOW; // the current state of relay void setup() { Serial.begin(9600); // initialize serial pinMode(SENSOR_PIN, INPUT); // set arduino pin to input mode pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode 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"); // toggle state of relay relayState = !relayState; // control relay arccoding to the toggrelay state digitalWrite(RELAY_PIN, relayState); } }

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 đúng bo mạch và cổ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
tải mã lên Arduino ide
  • Vỗ tay trước cảm biến âm thanh
  • Quan sát sự thay đổi trạng thái của rơ-le

Giải thích mã nguồn

Đọc lời giải thích theo từng dòng trong các chú thích của mã nguồn!

Mã Arduino - Rơ-le kích hoạt bằng âm thanh trong một khoảng thời gian nhất định

Đoạn mã dưới đây sẽ bật rơ-le trong một khoảng thời gian khi có âm thanh được phát hiện. Sau khoảng thời gian đó, rơ-le sẽ được tắt.

/* * 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-relay */ #define SENSOR_PIN A0 // Arduino pin connected to sound sensor's pin #define RELAY_PIN A2 // Arduino pin connected to the relay's pin #define TIME_PERIOD 5000 // in milliseconds 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 pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode 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"); // turn on relay digitalWrite(RELAY_PIN, HIGH); delay(TIME_PERIOD); // turn off relay digitalWrite(RELAY_PIN, LOW); } }

Xin lưu ý rằng mã được đề cập ở trên sử dụng hàm delay() để đơn giản. Tuy nhiên, nếu bạn tích hợp thêm mã khác, quá trình thực thi có thể bị chặn trong thời gian delay. Để giải quyết, mã sau đây triển khai một phương pháp không chặn bằng cách sử dụng thư viện Arduino - Thư viện ezLED. Đằng sau, thư viện ezLED sử dụng hàm millis() thay cho delay để ngăn chặn việc bị chặn.

/* * 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-relay */ #include <ezLED.h> // ezLED library #define SENSOR_PIN A0 // Arduino pin connected to sound sensor's pin #define RELAY_PIN A2 // Arduino pin connected to the relay's pin #define TIME_PERIOD 5000 // in milliseconds ezLED relay(RELAY_PIN); // create a relay object that attach to pin RELAY_PIN 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 currentSoundState = digitalRead(SENSOR_PIN); } void loop() { relay.loop(); // MUST call the relay.loop() function in 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"); relay.turnON(); // turn on relay immediately relay.turnOFF(TIME_PERIOD); // turn off relay after TIME_PERIOD } }

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

  • Kết nối Arduino với PC bằng cáp USB
  • Mở Arduino IDE, chọn bo mạch và cổng đúng
  • Đi tới biểu tượng Thư viện ở thanh bên trái của Arduino IDE.
  • Tìm kiếm “ezLED”, sau đó tìm thư viện LED do ArduinoGetStarted cung cấp
  • Nhấp vào nút Cài đặt để cài đặt thư viện ezLED.
thư viện LED cho Arduino
  • Sao chép mã ở trên và mở bằng Arduino IDE
  • Nhấn Tải lên trên Arduino IDE để tải mã lên Arduino
tải mã lên Arduino ide
  • Vỗ tay trước cảm biến âm thanh
  • Quan sát sự thay đổi trạng thái của rơ-le

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.