Ultrasonic Radar Using Arduino Nano and Serial Plotter

by AncheP in Circuits > Sensors

3627 Views, 8 Favorites, 0 Comments

Ultrasonic Radar Using Arduino Nano and Serial Plotter

IMG_20190616_083312.jpg

In this Instructable we will learn about the basics of a servo library as well as setting up the ultrasonic sensor and use it as a radar. the output of this project will be visible on the serial plotter monitor.

Supplies

-Arduino Nano.

-Breadboard.

-Glue gun.

-Jumper wires.

-PC to Arduino USB.

Connect the Arduino to Sensor and Servo

circuit.JPG
Arduino-Nano-Pinout-1024x544.jpg
breadboard.jpg
hc-sr04.jpg
jumpers.jpg
servo.jpg
glu gun.jpg

follow the connection scheme as described.

ultrasonic sensor

- trigger to pin2 of Arduino

- echo to pin3 of arduino

- Vcc and Gnd to 5v and Gnd respectively

servo:

- brown wire to ground

- red wire to vcc

- yellow/orangeish wire to pin 9 (the connections shown in the circuit diagram are not the same as described follow the description for best result)

Setting Up the Sensor

IMG_20190616_083312.jpg

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.

Setting Up the Arduino

IMG_20190616_083301.jpg
IMG_20190616_083319.jpg

when the whole setup looks like the one in the picture, connect the Arduino to the computer and start the Arduino IDE. there is a step by step explanation of each block of code in the following steps.

Declaring the Variables

declaration.JPG

#include 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.

Setup and Loop

setup.JPG

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.

Left and Right

left right.JPG

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(variable1);

Serial.print(" ");

Serial.println(variable2);

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

Calculating the Distance

sensorloop.JPG

the sensor requires a 10 microsencond pulse to send the ultrasonic soung signal which should then reflect off the object and will be received by the receiver. as shown in the omage 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 to wherever the function is called.

Upload the Code and Start

IMG_20190616_083140.jpg
IMG_20190616_083149.jpg
IMG_20190616_083214.jpg

once the code is verified and uploaded simply place some objects in front of the sensor and run it .

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

Interpreting the Serial Plotter

IMG_20190616_083521.jpg

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.

Precautions

do not place objects made of cloth. cloth disperses ultrasounds and causes the project to puke values in the range of 2000cm.

it is good for solid objects.

make sure the height of the object is enough to intercept to ultrasound pulse.

adjust the delay in the right(), left(), function to make the sensor rotate faster.