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:
- Arduino Board (e.g., Arduino Uno)
- Ultrasonic Sensor (e.g., HC-SR04)
- Servo Motor (e.g., SG90)
- Jumper wires
- Breadboard
- Power supply (Battery or USB cable)
Wiring:
- 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
- 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:
- The ultrasonic sensor emits sound waves (using the Trig pin) and then listens for the sound waves to return (using the Echo pin).
- The servo motor rotates the ultrasonic sensor to scan a defined area.
- The distance to objects is calculated based on the time it takes for the sound to return to the sensor.
- 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:
- 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. - 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 usepulseIn()
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
.
- The
- 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.
- Serial Output: The distance to any detected object at each angle is printed to the Serial Monitor.
Testing the Radar:
- Upload the code to your Arduino.
- Open the Serial Monitor (set to 9600 baud rate).
- You will see the distance measurements for each angle as the radar scans.
- If an object is detected, the system will report the distance at each specific angle.
Optional Improvements:
- Display Output: You could add an LCD or OLED display to show the distance and angle in real-time.
- Obstacle Detection: Add an LED or buzzer that goes off when an object is detected within a certain distance.
- 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.