Hệ thống Radar sử dụng Arduino UNO

Hệ thống Radar sử dụng Arduino UNO

Xây dựng hệ thống radar tự chế bằng cảm biến siêu âm và Arduino UNO.

Linh kiện và vật tư tiêu hao

  • Micro servo 1 9G
  • 1 Arduino Uno
  • 1 còi báo động
  • 1 x cáp USB loại A/B
  • 1 công tắc trượt
  • Điện trở 1 1k
  • Điện trở 4 220 ohm
  • 1 Cảm biến siêu âm HC-SR04
  • Dây nối 25
  • 2 đèn LED màu đỏ
  • 1 đèn LED xanh

Ứng dụng và nền tảng

  • 1 Arduino IDE
  • 1 Xử lý 3

Chi tiết dự án

Giới thiệu

Hệ thống radar thường phức tạp và tốn kém. Giờ đây, những người đam mê và sáng tạo có thể sử dụng Arduino Uno, chúng ta sẽ khám phá cách xây dựng hệ thống radar cơ bản bằng bộ vi điều khiển phổ biến này. Tập trung vào các nguyên tắc cơ bản về đo khoảng cách và phát hiện vật thể. Đây không phải là vấn đề về phép thuật hay sức mạnh đặc biệt. Nhưng vấn đề ở đây là ứng dụng thực tế của điện tử và lập trình để tạo ra các cảm biến thực tế có khả năng lập bản đồ môi trường.

Hệ thống radar tự chế này xoay sang trái và phải để quét các vật thể trong một khu vực xác định bằng cảm biến siêu âm. Thiết bị được điều khiển bằng công tắc trượt để bật và tắt. Có ba đèn LED và một còi báo động được gắn trên bảng thử nghiệm. Đèn LED đỏ đầu tiên sẽ sáng khi thiết bị được bật để báo hiệu trạng thái. Đèn LED xanh chỉ sáng khi thiết bị đang hoạt động và tìm kiếm vật thể. Đèn LED đỏ thứ hai sẽ chỉ nhấp nháy khi radar phát hiện có vật thể trong phạm vi. Tốc độ nhấp nháy phụ thuộc vào khoảng cách từ cảm biến đến vật thể được phát hiện. Chuông báo hoạt động như một đèn báo khi cảm biến phát hiện thấy vật gì đó trong phạm vi đã thiết lập.

Cài đặt

Trong cài đặt hệ thống radar, chúng ta sẽ sử dụng hai chương trình. Vui lòng xem liên kết tải xuống trong phần Ứng dụng & Nền tảng.

Chương trình đầu tiên là Arduino IDE, cho phép chúng ta gửi mã trực tiếp đến thiết bị Arduino của mình. Sau khi cài đặt phần mềm, hãy tạo một Sketch mới và dán đoạn mã bên dưới. Nếu bạn làm theo sơ đồ đấu dây thì bây giờ bạn đã thiết lập xong mọi thứ. Tuy nhiên, nếu bạn thay đổi bất kỳ cổng nào, hãy đảm bảo thay đổi biến lưu trữ số chân. Sau khi kết nối Arduino với PC bằng cáp USB-A sang USB-B ở góc dưới bên phải. Bạn sẽ thấy thứ gì đó tương tự như Arduino UNO trên COM6. COM[số] của bạn sẽ quan trọng sau này.

Chúng ta có thể kiểm tra các giá trị radar bằng chương trình có tên là Processing, chương trình này tạo ra giao diện cần thiết cho dự án. Sau khi cài đặt, hãy tạo một Sketch mới và dán mã được cung cấp. Có một số điều bạn cần phải thay đổi trước khi chạy chương trình. Ở dòng 24, hãy thay đổi giá trị theo độ phân giải màn hình của bạn và ở dòng 26, hãy thay đổi tham số thứ hai thành cổng COM mà bạn đã tìm thấy trước đó. Sau khi mọi thứ thay đổi Bạn đã sẵn sàng! Vào Arduino IDE và nhấp vào Tải lên ở góc trên bên trái. Chờ vài giây để mã được gửi đến thiết bị của bạn. Sau đó quay lại Xử lý và khởi động chương trình. Nếu mọi thứ được thực hiện đúng thì các đường màu xanh lá cây trong ứng dụng của bạn và máy quét xoay sẽ đồng bộ. Thưởng thức!

mã số

Tập lệnh radar để xử lý Java

import processing.serial.*; 
import  java.awt.event.KeyEvent;
import java.io.IOException;

Serial myPort;

String angle = "";
String distance = "";
String data = "";

String  noObject;

float pixsDistance;

int iAngle, iDistance;

int index1 = 0;
int index2 = 0;

PFont orcFont;


void setup() {
 size(1920, 1080);  // !!! change these values to your screen's resolution !!!
 smooth();
 myPort = new Serial(this, "COM6",  9600); // !!! CHANGE THE SECOND PARAMETER TO YOUR CUSTOM COM PORT !!!
 myPort.bufferUntil('.'); // actually reading:  angle,distance.
}

void draw() {
  fill(98, 245, 31);
  
  noStroke();
  
  fill(0, 4); 
  rect(0, 0, width, height-height * 0.065); 
  
  fill(98, 245, 31); // green

  drawRadar(); 
  drawLine();
  drawObject();
  drawText();
}

void serialEvent(Serial myPort) { // reading data from the Serial Port
  data = myPort.readStringUntil('.');
  data = data.substring(0, data.length() - 1);
  
  index1 = data.indexOf(","); 
  angle= data.substring(0, index1);
  distance= data.substring(index1 + 1, data.length());
  
  iAngle = int(angle);
  iDistance = int(distance);
}

void drawRadar() {
  pushMatrix();
  translate(width / 2, height - height * 0.074);
  noFill();
  strokeWeight(2);
  stroke(98, 245, 31);
  
  // drawing the arc lines
  arc(0, 0, (width - width * 0.0625), (width - width * 0.0625), PI, TWO_PI);
  arc(0, 0, (width - width * 0.27), (width - width * 0.27), PI, TWO_PI);
  arc(0, 0, (width - width * 0.479), (width - width * 0.479), PI, TWO_PI);
  arc(0, 0, (width - width * 0.687), (width - width * 0.687), PI, TWO_PI);
  
  // drawing the  angle lines
  line(-width / 2, 0, width / 2, 0);    
  line(0, 0, (-width / 2) * cos(radians(30)), (-width / 2) * sin(radians(30)));
  line(0, 0, (-width / 2) * cos(radians(60)), (-width / 2) * sin(radians(60)));
  line(0, 0, (-width / 2) * cos(radians(90)), (-width / 2) * sin(radians(90)));
  line(0, 0, (-width / 2) * cos(radians(120)), (-width / 2) * sin(radians(120)));
  line(0, 0, (-width / 2) * cos(radians(150)), (-width / 2) * sin(radians(150)));
  line((-width / 2) * cos(radians(30)), 0, width / 2, 0);
  popMatrix();
}

void drawObject() {
  pushMatrix();
  translate(width / 2, height - height * 0.074);
  strokeWeight(9);
  stroke(255, 10, 10); // red 
  pixsDistance = iDistance*((height - height * 0.1666) * 0.025); // converting the distance from cm to pixels
  
  if (iDistance<40) { // limiting the range to 40 cm
    line(pixsDistance * cos(radians(iAngle)), -pixsDistance * sin(radians(iAngle)), (width - width * 0.505) * cos(radians(iAngle)), -(width - width * 0.505) * sin(radians(iAngle)));
  }
  popMatrix();
}

void drawLine() {
  pushMatrix();
  strokeWeight(9);
  stroke(30, 250, 60);
  translate(width / 2, height - height * 0.074); 
  line(0, 0, (height - height * 0.12) * cos(radians(iAngle)), -(height - height * 0.12) * sin(radians(iAngle)));
  popMatrix();
}

// drawing the text on the screen
void drawText() { 
  pushMatrix();
  if (iDistance>40) {
    noObject = "Out of Range";
  } else {
    noObject = "In Range";
  }
  fill(0, 0, 0);
  noStroke();
  rect(0, height - height * 0.0648, width, height);
  fill(98, 245, 31);
  textSize(25);
  
  text("10cm", width - width * 0.3854, height - height * 0.0833);
  text("20cm", width - width * 0.281, height - height * 0.0833);
  text("30cm", width - width * 0.177, height - height * 0.0833);
  text("40cm", width - width * 0.0729, height - height * 0.0833);
  textSize(40);
  text("Angle:  " + iAngle + " °", width - width * 0.48, height - height * 0.0277);
  text("Distance:  ", width - width * 0.26, height - height * 0.0277);
 
  if (iDistance<40) {
    text("             " + iDistance + " cm", width - width * 0.225, height - height * 0.0277);
    fill(255, 10, 10);
    text("WARNING!", width - width * 0.875, height - height * 0.0277);
  }
  
  textSize(25);
  fill(98, 245, 60);
  translate((width - width * 0.4994) + width / 2 * cos(radians(30)), (height - height * 0.0907) - width / 2 * sin(radians(30)));
  rotate(-radians(-60));
  text("30°", 0, 0);
  resetMatrix();
  translate((width - width * 0.503) + width / 2 * cos(radians(60)), (height - height * 0.0888) - width / 2 * sin(radians(60)));
  rotate(-radians(-30));
  text("60°", 0, 0);
  resetMatrix();
  translate((width - width * 0.507) + width / 2 * cos(radians(90)), (height - height * 0.0833) - width / 2 * sin(radians(90)));
  rotate(radians(0)); 
  text("90°", 0, 0);
  resetMatrix();
  translate(width - width * 0.513 + width / 2 * cos(radians(120)), (height - height * 0.07129) -width / 2 * sin(radians(120)));
  rotate(radians(-30));
  text("120°", 0, 0);
  resetMatrix();
  translate((width - width * 0.5104) + width / 2 * cos(radians(150)), (height - height * 0.0574) - width / 2 * sin(radians(150)));
  rotate(radians(-60));
  text("150°", 0, 0);
  popMatrix(); 
}

Tập lệnh radar cho Arduino IDE cpp

#include <Servo.h> 

// variables
const int trigPin = 2;
const int echoPin  = 9;

long duration;
int  distance;

int buzzpin = 7;
int buzzState = LOW;

int ledRed = 3;
int ledGreen = 4;

int switchpin = 10;
int ledStatus = 8;

byte leds = 0;

bool canSpin = true;

unsigned long previousMillis = 0;

const long intervalFar = 250;
const long intervalClose = 50;
const long intervalIdle = 1250;

Servo myServo; 


void setup() {
  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT); 
  Serial.begin(9600);
  myServo.attach(12); 
  pinMode(buzzpin, OUTPUT);
  Serial.begin(9600);
  pinMode(ledRed, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(switchpin, INPUT);
  pinMode(ledStatus, OUTPUT);
}

// turning the radar on-off
void loop() {
  if (digitalRead(switchpin) == HIGH){
    digitalWrite(ledStatus, LOW);
    StartScan();
  }
  if (digitalRead(switchpin) == LOW){
    digitalWrite(ledStatus, HIGH);
  }
}

// calculating the distance to a detected object
int calculateDistance(){ 
  digitalWrite(trigPin,  LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH); 
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH); // reads echoPin and returns the sound wave travel time (ms)
  distance = duration * 0.034 / 2;
  return distance;
}

// start scanning for objects
void StartScan() {
  // checking for every degree

  // first from right to left
  for (int i = 15; i <= 165; i++) {  
    myServo.write(i);
    delay(30);
    distance = calculateDistance();

    // checking if something is detected in the 40 cm range
    if (distance <= 40 && distance > 20) {
      unsigned long currentMillis = millis();

      if (currentMillis - previousMillis >= intervalFar) {
        previousMillis = currentMillis;

        if (buzzState == LOW) {
          buzzState = HIGH;
        } else {
          buzzState = LOW;
        }

        digitalWrite(buzzpin, buzzState);
        digitalWrite(ledRed, buzzState); 
        digitalWrite(ledGreen, HIGH); 
      }
    } else if (distance <= 20 && distance > 0) {
      unsigned long currentMillis = millis();

      if (currentMillis - previousMillis >= intervalClose) {
        previousMillis = currentMillis;

        if (buzzState == LOW) {
          buzzState = HIGH;
        } else {
          buzzState = LOW;
        }

        digitalWrite(buzzpin, buzzState);
        digitalWrite(ledRed, buzzState); 
        digitalWrite(ledGreen, HIGH); 
      }
    } else if (distance > 40) {
      digitalWrite(buzzpin, LOW);
      digitalWrite(ledGreen, LOW);
      digitalWrite(ledRed, HIGH); 
    }
      
    Serial.print(i); 
    Serial.print(","); 
    Serial.print(distance);  
    Serial.print(".");
  }

  // then from left to right
  for (int i = 165; i >= 15; i--) {  
    myServo.write(i);
    delay(30);
    distance  = calculateDistance();

    if (distance <= 40 && distance > 20) {
      unsigned long currentMillis = millis();

      if (currentMillis - previousMillis >= intervalFar) {
        previousMillis = currentMillis;

        if (buzzState == LOW) {
          buzzState = HIGH;
        } else {
          buzzState = LOW;
        }

        digitalWrite(buzzpin, buzzState);
        digitalWrite(ledRed, buzzState); 
        digitalWrite(ledGreen, HIGH); 
      }
    } else if (distance <= 20 && distance > 0) {
      unsigned long currentMillis = millis();

      if (currentMillis - previousMillis >= intervalClose) {
        previousMillis = currentMillis;

        if (buzzState == LOW) {
          buzzState = HIGH;
        } else {
          buzzState = LOW;
        }

        digitalWrite(buzzpin, buzzState);
        digitalWrite(ledRed, buzzState); 
        digitalWrite(ledGreen, HIGH); 
      }
    } else if (distance > 40) {
      digitalWrite(buzzpin, LOW);
      digitalWrite(ledGreen, LOW);
      digitalWrite(ledRed, HIGH); 
    }

    Serial.print(i);
    Serial.print(",");
    Serial.print(distance);
    Serial.print(".");
  }
}

Dây dẫn radar

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

Win a Raspberry Pi!

Answer 5 questions for your chance to win!
Question 1

What color is the sky?

Tìm kiếm bằng danh mục

Chọn danh mục