Hi everyone!
For this post, I’m doing something very different. I’m building a simulation for an Arduino radar!

DISCLAIMER:
A big shoutout to Robotics India for the amazing ideas and tutorials on Arduino projects. Many of the concepts here were inspired by their work, and we’ve adapted them to fit the flow of this post. We encourage readers to check out Robotics India’s content for even more detailed insights and guidance.
Furthermore, I did not use any form of AI assistance in this project. Thanks!
This is my first time EVER doing something like this. Therefore, I’ve looked online for some ideas, and have come across the holy grail of videos.
Check it out!
After watching the video, I decided to recreate the project myself, a mini Arduino radar system using an ultrasonic sensor (HC-SR04) and a servo motor.
DISCLAIMER: This code is heavily inspired by a YouTuber, and under no circumstances do I claim it as my own. I have, however, studied how the code works and believe I have a solid understanding of its functionality. My explanation within the code below reflects my own understanding and interpretation.
The basic idea is simple: the servo rotates the ultrasonic sensor back and forth, sending out sound waves to detect objects. The distance readings are then used to simulate a radar sweep, just like what you’d see on an actual radar screen.
Here’s what the setup includes:
- Arduino UNO (the brain of the project)
- HC-SR04 Ultrasonic Sensor (for distance measurement)
- SG90 Micro Servo (to rotate the sensor)
- Breadboard and jumper wires (for connections)
The circuit sends out ultrasonic pulses, measures how long they take to return, and converts that time into distance. Then, the servo moves to a new angle and repeats the process, scanning the area in front of it.
Here is the BOM (parts list), and code below, along with an explanation of how everything works!
| Component | Quantity | Description |
| Arduino UNO | 1 | The “Brain” of he circuit |
| HC-SR04 Ultrasonic Sensor | 1 | Sends and receives sound waves to measure distance. |
| SG90 Micro Servo Motor | 1 | Rotates the ultrasonic sensor to scan different angles. |
| Breadboard | 1 | Makes it easy to connect components without soldering. |
| Male-to-Male Jumper Wires | 1 | For all electrical connections between the components. |
| USB Cable (Type-B) | 8 | Used to upload code and power the Arduino. |
#include <Servo.h>
// Includes the Servo library, which allows control of servo motors.
const int SERVO_PIN = 11;
const int TRIG_PIN = 8;
const int ECHO_PIN = 9;
// Defines which Arduino pins are connected to the servo and ultrasonic sensor.
const int MIN_ANGLE = 0;
const int MAX_ANGLE = 180;
const int ANGLE_STEP = 1;
const int SWEEP_DELAY = 15;
// Sets the servo movement range and speed of rotation.
const float SOUND_SPEED_FACTOR = 58.2;
// Used to convert the echo time (in microseconds) to distance (in centimeters).
Servo myServo;
// Creates a Servo object to control the servo motor.
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
myServo.attach(SERVO_PIN);
Serial.begin(9600);
}
// Initializes pin modes, connects the servo, and starts serial communication.
void loop() {
sweepAndMeasure(MIN_ANGLE, MAX_ANGLE, ANGLE_STEP);
sweepAndMeasure(MAX_ANGLE, MIN_ANGLE, -ANGLE_STEP);
}
// Continuously sweeps the servo from 0° to 180° and back, taking distance readings.
void sweepAndMeasure(int startAngle, int endAngle, int step) {
for (int angle = startAngle; (step > 0) ? (angle <= endAngle) : (angle >= endAngle); angle += step) {
myServo.write(angle);
delay(SWEEP_DELAY);
int distance = calculateDistance();
printData(angle, distance);
}
}
// Moves the servo in steps, measures the distance at each angle, and sends the data to Serial.
int calculateDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
return static_cast<int>(duration / SOUND_SPEED_FACTOR);
}
// Sends an ultrasonic pulse, measures how long it takes to return, and converts that time to distance.
void printData(int angle, int distance) {
Serial.print(angle);
Serial.print(",");
Serial.print(distance);
Serial.print(".");
}
// Prints each angle and corresponding distance reading to the Serial Monitor for radar visualization.
In short, it sweeps a servo back and forth while measuring and logging distances with an ultrasonic sensor.
Here is the link to this project
Here is the demonstration:
Thanks for reading!
Leave a Reply to mcrompton Cancel reply