Introduction: Ultrasonic Radar Using Arduino and Serial Plotter

lets make a simple radar using the Arduino Nano and ultrasonic sensor.

the output of this project can be seen in the serial plotter in the latest Arduino ide


-Arduino Nano

-HC-SR04 ultrasonic sensor module


-Jumper wires

-Glue gun

-Micro servo and its shaft attachments

Step 1: Connections

the connection in the diagram differ from the description here. the description connections are according to the code.

Ultrasonic sensor-

-trigger to pin 2 of arduino

-echo to pin 3 of arduino

-vcc and ground to 5v and ground of arduino


-brown wire to ground

- red wire to vcc

- yellow/orangeish wire to pin 9

Step 2: Setting Up the Sensor and Servo

hot glue the servo on a piece of cardboard.
the servo comes with a range of attachments to the shaft.

attach the flat and large one on the motor shaft and rotate it completely to one side.

you can see that the servo can only rotate upto a limit of a 180 degrees either direction.

now adjust the attachment accordingly so it sits perfectly straight on the 180 degree angle .

then hot glue the sensor to the attachment as shown in the figure.

the servo must now be able to rotate the sensor from 0 to 180 degrees.

Step 3: Code: Declaring Variables

Servo.h: it is the library required to effectively run the servo motor which requires a pwm signal.
trigger, echo, duration, distance are all integers. pins of the trigger and echo are defined accordingly. a variable " servo " is created to address the motor that we connected the Arduino can support multiple servos as long as it can supply power to them and it has enough of those control pins.


int trigPin=2; int echoPin=3; long duration; int distance; Servo servo;

void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); Serial.begin(9600); servo.attach(9); } void loop() { left(); right(); }

void left() { for (int pos = 0; pos <= 180; pos += 1) { servo.write(pos); Serial.print(pos); Serial.print(" "); distance=echoloop(); Serial.println(distance); delay(50); } }

void right(){ for (int pos = 180; pos >= 0; pos -= 1) { servo.write(pos); Serial.print(pos); Serial.print(" "); distance=echoloop(); Serial.println(distance); delay(50); } }

int echoloop() { long duration, cm; pinMode(trigPin, OUTPUT); digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); pinMode(echoPin, INPUT); duration = pulseIn(echoPin, HIGH); cm = duration/29/2; return cm; }

Step 4: Code: Setup and Loop

in the void setup function, declare the the pin modes as in the figure.
in the void loop function call two other functions such as left and right these functions will later be built to rotate the motor shaft. also begin the serial communication between the Arduino and pc with a baud rate of 9600 which is enough to support our application.

Step 5: Code: Sweep Function

the micro servo can rotate between 0 to a 180degree angle.
to achieve that motion we must build a sweep motion function.

although it can be done using a single function, this is another way of doing it.

in each of the block of code we find the integer "distance" is given the return value of the function echoloop().

this function calculates the distance of the object from the sensor.

the functions contain the terms serial.print() and serial.println().

to get the serial plotter to plot the variables we need to print them in this format.


Serial.print(" ");


in our case variable1 is the angle and variable2 is the distance.

Step 6: Code: Calculating Distance

the sensor requires a 10 microsencond pulse to send the ultrasonic sound signal which should then reflect off the object and will be received by the receiver.

as shown in the image the code is designed to to exactly that.
once the duration of the reflection is known the distance of the object can be calculated easily.

ultrasound too travels at the speed of sound in air 343m/s.

the calculated distance is now returned whenever the function is called.

Step 7: Place Objects and Start Detecting

when compiling debugging uploading is done start the serial plotter from the IDE tools dropbox

or just press ctrl+shift+L (make sure you use the latest arduino IDE).

remember the objects I placed
- a multimeter to the left of the sensor

- a black box close and infront of the sensor

- a blue box to the right at some distance

Step 8: Interpreting the Signal From the Sensor

the code is designed to plot the distance calculated.

open the serial plotter by going to tools .
the latest Arduino IDE has the serial plotter so update the IDE.

in the plot we find a blue triangular wave which is the plot of the angle of the servo.

the red plot is that of the distance calculated by the sensor. the closer the object the lower the red plot falls .

the farther the object the higher and a little erratic the red plot becomes.

you can notice the three major depressions in the plot

- close to the zero degrees in the blue plot - the multimeter.

- in the middle of the upward slope as well as downward slope - the black box

- at the peak of the blue plot - a lesser depression because the object is farther - the blue box placed far to the right side.

use the blue plot as the reference of the angle which varies from 0 to 180 degrees the distance of the objects measured varies from 2 to 200 cm depending on the sensitivity of the object.

Sensors Contest

Participated in the
Sensors Contest