Arduino radar project

How to Create an Arduino radar project using an ultrasonic sensor.

Creating an Arduino radar project using an ultrasonic sensor is a fun and educational project that involves using an ultrasonic sensor to detect objects and a servo motor to rotate the sensor to scan the area. Below are the steps for building a basic radar system with an ultrasonic sensor:

Components Required for Auduino Radar project:

  1. Arduino Board (e.g., Arduino Uno)
  2. Ultrasonic Sensor (e.g., HC-SR04)
  3. Servo Motor (e.g., SG90)
  4. Jumper wires
  5. Breadboard
  6. Power supply (Battery or USB cable)

Wiring:

  1. Ultrasonic Sensor (HC-SR04) connections:
    • VCC to 5V on Arduino
    • GND to GND on Arduino
    • Trig to a digital pin (e.g., pin 9) on Arduino
    • Echo to a digital pin (e.g., pin 10) on Arduino
  2. Servo Motor Connections:
    • VCC to 5V on Arduino
    • GND to GND on Arduino
    • Signal to a PWM capable pin (e.g., pin 6) on Arduino

Basic Radar Working Principle:

  1. The ultrasonic sensor emits sound waves (using the Trig pin) and then listens for the sound waves to return (using the Echo pin).
  2. The servo motor rotates the ultrasonic sensor to scan a defined area.
  3. The distance to objects is calculated based on the time it takes for the sound to return to the sensor.
  4. This information is displayed on the serial monitor, or you can use LEDs to show object detection or distance.

Arduino Code:

#include <Servo.h>

const int trigPin = 9;  // Pin for Trig on the ultrasonic sensor
const int echoPin = 10; // Pin for Echo on the ultrasonic sensor
const int servoPin = 6; // Pin for Servo motor

Servo radarServo; // Create a Servo object

void setup() {
  // Start serial communication for debugging
  Serial.begin(9600);
  
  // Set up the ultrasonic sensor pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  // Attach the servo to the servoPin
  radarServo.attach(servoPin);
  
  // Initialize the servo to 0 degrees
  radarServo.write(0);
  delay(500);
}

void loop() {
  // Rotate the servo motor from 0 to 180 degrees and back to scan the area
  for (int angle = 0; angle <= 180; angle++) {
    radarServo.write(angle);  // Rotate the servo to the current angle
    delay(15);  // Wait for the servo to reach the position
    
    long duration, distance;
    
    // Send a pulse to trigger the ultrasonic sensor
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    
    // Read the pulse duration from Echo pin
    duration = pulseIn(echoPin, HIGH);
    
    // Calculate distance based on the pulse duration
    distance = duration * 0.0344 / 2; // Distance in cm
    
    // Display the distance in the Serial Monitor
    Serial.print("Angle: ");
    Serial.print(angle);
    Serial.print("°, Distance: ");
    Serial.print(distance);
    Serial.println(" cm");
    
    delay(200); // Short delay between readings
  }

  // Move the servo back to 0 degrees after scanning
  for (int angle = 180; angle >= 0; angle--) {
    radarServo.write(angle);  // Rotate the servo to the current angle
    delay(15);  // Wait for the servo to reach the position
  }
}

Explanation of the Code:

  1. Servo Control: We use the Servo library to control the servo motor. The servo is rotated from 0 to 180 degrees and back to cover a wide angle for radar scanning.
  2. Ultrasonic Sensor Logic:
    • The trigPin is used to send a short pulse that triggers the ultrasonic sensor.
    • The echoPin receives the reflected pulse, and we use pulseIn() to measure the time the pulse takes to return.
    • The distance is then calculated using the speed of sound formula: distance = (duration * 0.0344) / 2.
  3. Radar Scan: The servo motor rotates the sensor in 1-degree increments (from 0° to 180° and back to 0°), providing a sweep of the area to measure distances.
  4. Serial Output: The distance to any detected object at each angle is printed to the Serial Monitor.

Testing the Radar:

  1. Upload the code to your Arduino.
  2. Open the Serial Monitor (set to 9600 baud rate).
  3. You will see the distance measurements for each angle as the radar scans.
  4. If an object is detected, the system will report the distance at each specific angle.

Optional Improvements:

  1. Display Output: You could add an LCD or OLED display to show the distance and angle in real-time.
  2. Obstacle Detection: Add an LED or buzzer that goes off when an object is detected within a certain distance.
  3. Radar Visualization: If you have an LED strip or NeoPixels, you could create a more visual radar system, with LEDs lighting up in the direction of an object.

This project gives you a great way to learn about ultrasonic sensors, servo motors, and radar principles.

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart