Arduino UNO R4 Cảm Biến Siêu Âm

Hướng dẫn này sẽ chỉ bạn cách sử dụng cảm biến siêu âm và Arduino UNO R4 để đo khoảng cách đến một vật thể. Chi tiết, chúng ta sẽ học:

Arduino UNO R4 cảm biến siêu âm

Phần Cứng Cần Thiết

1×Arduino UNO R4 WiFi hoặc Arduino UNO R4 Minima
1×(Tùy chọn) DIYables STEM V4 IoT, tương thích với Arduino Uno R4 WiFi
1×Arduino UNO R4 Minima (Thay thế)
1×Cáp USB Type-C
1×Cảm Biến Siêu Âm
1×Dây Jumper
1×(Khuyến nghị) Screw Terminal Block Shield for Arduino UNO R4
1×(Khuyến nghị) Breadboard Shield for Arduino UNO R4
1×(Khuyến nghị) Enclosure for Arduino UNO R4
1×(Khuyến nghị) Power Splitter for Arduino UNO R4
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

1×DIYables STEM V4 IoT Starter Kit (Arduino included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về Cảm Biến Siêu Âm

Cảm biến siêu âm HC-SR04 đo khoảng cách đến các vật thể bằng sóng âm. Nó phát ra một sóng âm mà con người không thể nghe thấy, sau đó lắng nghe tiếng vọng khi sóng âm phản hồi từ một vật thể. Bằng cách đo thời gian sóng âm trở lại, cảm biến có thể tính toán khoảng cách đến vật thể.

Sơ Đồ Chân

Cảm biến siêu âm HC-SR04 có bốn chân:

  • Chân VCC: Kết nối chân này với VCC (5V).
  • Chân GND: Kết nối chân này với GND (0V).
  • Chân TRIG: Kết nối chân này với Arduino UNO R4 để gửi tín hiệu điều khiển (xung).
  • Chân ECHO: Chân này gửi tín hiệu (xung) trở lại Arduino UNO R4. Arduino UNO R4 sau đó tính toán thời lượng của những xung này để xác định khoảng cách.
cảm biến siêu âm sơ đồ chân
image source: diyables.io

Sơ Đồ Đấu Nối

Arduino UNO R4 cảm biến siêu âm sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

Code Arduino UNO R4

#define TRIG_PIN 9 // The Arduino UNO R4 pin connected to the ultrasonic sensor's TRIG pin #define ECHO_PIN 8 // The Arduino UNO R4 pin connected to the ultrasonic sensor's ECHO pin float duration_us, distance_cm; void setup() { // begin serial port Serial.begin (9600); // configure the trigger pin to output mode pinMode(TRIG_PIN, OUTPUT); // configure the echo pin to input mode pinMode(ECHO_PIN, INPUT); } 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; // print the value to Serial Monitor Serial.print("distance: "); Serial.print(distance_cm); Serial.println(" cm"); delay(500); }

Các Bước Nhanh

Thực hiện theo các hướng dẫn sau từng bước:

  • Nếu đây là lần đầu tiên bạn sử dụng Arduino Uno R4 WiFi/Minima, hãy tham khảo hướng dẫn về Arduino UNO R4 - Cài Đặt Phần Mềm.
  • Đấu nối các linh kiện theo sơ đồ được cung cấp.
  • Kết nối bo mạch Arduino Uno R4 với máy tính của bạn bằng cáp USB.
  • Khởi chạy Arduino IDE trên máy tính của bạn.
  • Chọn bo mạch Arduino Uno R4 phù hợp (ví dụ: Arduino Uno R4 WiFi) và cổng COM.
  • Sao chép code ở trên và mở bằng Arduino IDE
  • Nhấp vào nút Upload trong Arduino IDE để gửi code đến Arduino UNO R4
Arduino ide - how to upload code
  • Mở Serial Monitor.
  • Vẫy tay trước cảm biến siêu âm.
  • Kiểm tra khoảng cách giữa tay bạn và cảm biến trên Serial Monitor.
COM6
Send
distance: 29.4 cm distance: 27.6 cm distance: 26.9 cm distance: 17.4 cm distance: 16.9 cm distance: 14.3 cm distance: 15.6 cm distance: 13.1 cm
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Giải Thích Code

Phần giải thích có trong các comments của Arduino code ở trên.

Cách Lọc Nhiễu Từ Phép Đo Khoảng Cách Của Cảm Biến Siêu Âm

Kết quả đọc từ cảm biến siêu âm bao gồm nhiễu. Trong một số trường hợp, dữ liệu nhiễu này có thể dẫn đến hoạt động không chính xác. Chúng ta có thể loại bỏ nhiễu bằng phương pháp này:

  • Thực hiện nhiều phép đo và lưu chúng trong một mảng.
  • Sắp xếp mảng từ nhỏ đến lớn.
  • Loại bỏ nhiễu từ dữ liệu:
    • Bỏ qua các giá trị nhỏ nhất vì chúng là nhiễu.
    • Bỏ qua các giá trị lớn nhất vì chúng là nhiễu.
    • Tính trung bình sử dụng các giá trị ở giữa còn lại.

    Code ví dụ dưới đây thực hiện 20 phép đo.

    • Bỏ qua năm mẫu nhỏ nhất và năm mẫu lớn nhất vì chúng được coi là nhiễu. Tính trung bình của mười mẫu ở giữa, từ mẫu thứ 5 đến thứ 14.
    /* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 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-uno-r4/arduino-uno-r4-ultrasonic-sensor */ #define TRIG_PIN 9 // The Arduino UNO R4 pin connected to the ultrasonic sensor's TRIG pin #define ECHO_PIN 8 // The Arduino UNO R4 pin connected to the ultrasonic sensor's ECHO pin float filterArray[20]; // array to store data samples from sensor float distance; // store the distance from sensor void setup() { // begin serial port Serial.begin (9600); // configure the trigger and echo pins to output mode pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); } void loop() { // 1. TAKING MULTIPLE MEASUREMENTS AND STORE IN AN ARRAY for (int sample = 0; sample < 20; sample++) { filterArray[sample] = ultrasonicMeasure(); delay(30); // to avoid untrasonic interfering } // 2. SORTING THE ARRAY IN ASCENDING ORDER for (int i = 0; i < 19; i++) { for (int j = i + 1; j < 20; j++) { if (filterArray[i] > filterArray[j]) { float swap = filterArray[i]; filterArray[i] = filterArray[j]; filterArray[j] = swap; } } } // 3. FILTERING NOISE // + the five smallest samples are considered as noise -> ignore it // + the five biggest samples are considered as noise -> ignore it // ---------------------------------------------------------------- // => get average of the 10 middle samples (from 5th to 14th) double sum = 0; for (int sample = 5; sample < 15; sample++) { sum += filterArray[sample]; } distance = sum / 10; // print the value to Serial Monitor Serial.print("distance: "); Serial.print(distance); Serial.println(" cm"); } float ultrasonicMeasure() { // generate 10-microsecond pulse to TRIG pin digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); // measure duration of pulse from ECHO pin float duration_us = pulseIn(ECHO_PIN, HIGH); // calculate the distance float distance_cm = 0.017 * duration_us; return distance_cm; }

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.

Ứng Dụng Cảm Biến Siêu Âm

  • Tránh Va Chạm
  • Phát Hiện Độ Đầy
  • Đo Mức Độ
  • Phát Hiện Sự Gần Gũi