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.
-PC to Arduino USB.
Step 1: Connect the Arduino to Sensor and Servo
follow the connection scheme as described.
- trigger to pin2 of Arduino
- echo to pin3 of arduino
- Vcc and Gnd to 5v and Gnd respectively
- 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)
Step 2: Setting Up the Sensor
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: Setting Up the Arduino
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.
Step 4: Declaring the Variables
#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.
Step 5: 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 6: Left and Right
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.
in our case variable1 is the angle and variable2 is the distance.
Step 7: Calculating the Distance
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.
Step 8: Upload the Code and Start
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
Step 9: Interpreting the Serial Plotter
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.
Step 10: 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.