Arduino Cảm biến siêu âm Đèn LED

Chúng ta sẽ học cách làm thế nào:

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×cảm biến siêu âm
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 siêu âm

Nếu bạn chưa biết về LED và cảm biến siêu âm (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 nối LED cho cảm biến siêu âm Arduino

This image is created using Fritzing. Click to enlarge image

Mã Arduino

/* * 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-ultrasonic-sensor-led */ // constants won't change const int TRIG_PIN = 6; // Arduino pin connected to Ultrasonic Sensor's TRIG pin const int ECHO_PIN = 7; // Arduino pin connected to Ultrasonic Sensor's ECHO pin const int LED_PIN = 3; // Arduino pin connected to LED's pin const int DISTANCE_THRESHOLD = 50; // centimeters // variables will change: float duration_us, distance_cm; void setup() { Serial.begin (9600); // initialize serial port pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode pinMode(ECHO_PIN, INPUT); // set arduino pin to input mode pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode } void loop() { // generate 10-microsecond pulse to TRIG pin digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); // measure duration of pulse from ECHO pin duration_us = pulseIn(ECHO_PIN, HIGH); // calculate the distance distance_cm = 0.017 * duration_us; if(distance_cm < DISTANCE_THRESHOLD) digitalWrite(LED_PIN, HIGH); // turn on LED else digitalWrite(LED_PIN, LOW); // turn off LED // print the value to Serial Monitor Serial.print("distance: "); Serial.print(distance_cm); Serial.println(" cm"); delay(500); }

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
  • Sao chép mã ở trên và mở bằng Arduino IDE
  • Nhấp nút Tải lên trên Arduino IDE để tải mã lên Arduino
tải mã lên Arduino ide
  • Đưa tay trước cảm biến
  • Nhìn thấy sự thay đổi trạng thái của đèn LED

Giải thích mã nguồn

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

※ Lưu ý:

Đoạn mã ở trên nhằm mục đích học tập. Cảm biến siêu âm rất nhạy với nhiễu. Nếu bạn muốn sử dụng cảm biến siêu âm trong thực tế, bạn nên lọc nhiễu cho cảm biến siêu âm. Xem cách lọc nhiễu cho cảm biến siêu âm

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.