Arduino Cảm biến âm thanh Đèn LED

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 đèn LED. 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×LED Kit
1×LED (red)
1×LED Module
1×220Ω Resistor
1×breadboard
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ề LED và Cảm biến âm thanh

Nếu bạn chưa biết về đèn LED 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 về chúng trong các bài hướng dẫn sau:

Sơ đồ đấu dây

sơ đồ đấu nối LED 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 LED

Đoạn mã dưới đây sẽ đổi trạng thái của LED 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-led */ #define SENSOR_PIN A0 // Arduino pin connected to sound sensor's pin #define LED_PIN 7 // Arduino pin connected to LED's pin int lastSoundState; // the previous state of sound sensor int currentSoundState; // the current state of sound sensor int ledState = LOW; // the current state of LED void setup() { Serial.begin(9600); // initialize serial pinMode(SENSOR_PIN, INPUT); // set arduino pin to input mode pinMode(LED_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 LED ledState = !ledState; // control LED arccoding to the toggled state digitalWrite(LED_PIN, ledState); } }

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
  • 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
tải mã nguồn bằng Arduino ide
  • Vỗ tay trước cảm biến âm thanh
  • Xem sự thay đổi của trạng thái LED

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 - Đèn LED kích hoạt bằng âm thanh trong một khoảng thời gian

Mã dưới đây bật đèn LED trong một khoảng thời gian khi có âm thanh được phát hiện. Sau khoảng thời gian đó, đèn LED sẽ 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-led */ #define SENSOR_PIN A0 // Arduino pin connected to sound sensor's pin #define LED_PIN 7 // Arduino pin connected to LED'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(LED_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 LED digitalWrite(LED_PIN, HIGH); delay(TIME_PERIOD); // turn off LED digitalWrite(LED_PIN, LOW); } }

Xin lưu ý rằng mã trước đó sử dụng hàm delay(), điều này khá dễ hiểu. Tuy nhiên, khi bổ sung thêm mã, hàm delay() có thể gây ra tình trạng chặn trong thời gian trễ. Để khắc phục điều này, đoạn mã sau triển khai một cách tiếp cận không chặn bằng cách sử dụng thư viện Arduino - Thư viện ezLED. Thư viện ezLED, hoạt động âm thầm ở phía sau, sử dụng hàm millis() thay cho delay để tránh 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-led */ #include <ezLED.h> // ezLED library #define SENSOR_PIN A0 // Arduino pin connected to sound sensor's pin #define LED_PIN 7 // Arduino pin connected to LED's pin #define TIME_PERIOD 5000 // in milliseconds ezLED led(LED_PIN); // create a LED object that attach to pin LED_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() { led.loop(); // MUST call the led.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"); led.turnON(); // turn on immediately led.turnOFF(TIME_PERIOD); // turn off after TIME_PERIOD } }

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

  • Kết nối Arduino với PC qua cáp USB
  • Mở Arduino IDE, chọn board 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 phát hành
  • 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ã ở phía 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
  • Xem sự thay đổi trạng thái của đèn LED

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.