Arduino Book
Arduino Book
Introduction
Object detection system using ultrasonic sensor is a project that can detect any
object in the path with the help of an ultrasonic sensor. Ultrasonic sensor rotates over
the servo motor and almost cover all directions in rotatory motion. There is processing
software that makes it possible. The ultrasonic sensor is very useful in my electronics
projects. It uses in distance measuring and object detection. The radar system uses the
ultrasonic sensor to detect the object in front of the sensor and servo motor help to cover
the distance by rotating itself slowly. This detection system has one ultrasonic sensor,
one servo motor, two LEDs to work as a virtual indicator, one buzzer to alert if the
object is within the limited range, and an Arduino uno.
1
2. Objectives
2
3. Components List
3
4. Hardware Descriptions
The ultrasonic sensor works on the principle of SONAR and RADAR system
which is used to determine the distance to an object. An ultrasonic sensor generates the
high frequency sound (ultrasound) waves. An ultrasonic sensor uses a transducer to
send and receive ultrasonic pulses that relay back information about an object's
proximity. When this ultrasound hits the object, it reflects as echo which is sensed by
the receiver as shown in below.
4
Figure 2: Ultrasonic Sensor
A servo motor is a rotary or linear actuator that allows for precise control of
angular or linear position, velocity, and acceleration in a mechanical system. It
constitutes part of a servomechanism, and consists of a suitable motor coupled to a
sensor for position feedback. It usually comes with a gear arrangement that allows us
to get a very high torque servo motor in small and lightweight packages. Due to these
features, they are being used in many applications like toy car, RC helicopters and
planes, Robotics, etc.
5
4.4. Breadboard
Figure 4: Breadboard
4.5. LED
Figure 5: LED
4.6. Buzzer
6
Figure 6: Buzzer
4.8. Resistors
The resistor is a passive electrical component that creates resistance in the flow
of electric current. In almost all electrical networks and electronic circuits they can be
found. The resistance is measured in ohms (Ω). An ohm is the resistance that occurs
when a current of one ampere (A) passes through a resistor with a one volt (V) drop
across its terminals. The current is proportional to the voltage across the terminal ends.
This ratio is represented by Ohm’s law. Resistors are used for many purposes. A few
examples include limiting electric current, voltage division, heat generation, matching
7
and loading circuits, gain control, and setting time constants. They are commercially
available with resistance values over a range of more than nine orders of magnitude.
They can be used as electric brakes to dissipate kinetic energy from trains, or be smaller
than a square millimeter for electronics.
Figure 8: Resistor
The USB cable has one end as USB type A connector and another end has a
USB type B connector. The USB type-A end is connected to the power source or the
programming device (e.g., your computer/laptop) and the USB B end is connected to
Arduino UNO or MEGA 2560 board.
8
5. Software Requirements
9
Figure 9: Processing Software
Ultrasonic sensor
Buzzer
Display
Microcontroller
Servo motor LED
Power supply
10
6.2 System Implementation
1. Connect the VCC pin of the ultrasonic sensor to the positive terminal of the
Arduino (5V) pin.
2. Connect the GND pin of the ultrasonic sensor to any GND pin of the Arduino.
3. Connect the Trig pin and echo pin of ultrasonic sensor to digital pins 2 and 3 of
the Arduino.
1. Connect the VCC pin of the Servo motor to the positive terminal of the Arduino
(5 V).
2. Connect the GND pin of the Servo motor to any GND pin on the Arduino.
3. Connect the Servo’s signal pin of the Servo motor to digital pin 9 of the Arduino.
1. Connect the Ground pin of the buzzer to any GND pin of the Arduino.
11
2. Connect the Signal pin of the buzzer to digital pin 4 of the Arduino.
3. Open the Arduino IDE and select the appropriate board from the Tools menu
under Boards.
5. Copy the following sketch, which appears in your web browser, to your Arduino
sketch page.
7. The final step is to connect the Arduino’s cable to the adapter and then plug it
into the socket.
3. Sketches are stored in the sketchbook, a folder that's used as the default location
for saving all of your projects.
6. The port number in the Arduino must match the port number in the processing
code.
12
7. Click on the Sketch icon or go to Run or Click Run icon above the code.
Object detection system has been successfully implemented and shows good
performance. The ultrasonic sensor detects an object and send the right signal to the
Arduino board and the Arduino communicate the processing software so that the angle
of the object is calculated successfully. Buzzer and LEDs were also used to give an
alarm if a ship is detected at a specific distance. The result implementation is shown in
Fig-12. The ultrasonic radar can actually detect an object of about 40cm away but a
specific distance was chosen in this project for the alarm system to triggered.
An LED remain high indicating that no ship is close to colliding with another
ship if the position of the other ship or object is greater than 40cm, but if it is less than
40cm then the Arduino will trigger the alarm system to come up with a buzzer and red
LED indicating that the ship is close to colliding with another ship.
13
8. Conclusion
Within the first week of starting our project, we decided to make a project based
on our mini-project. So, we chose to make something useful related to ultrasonic sensor.
And then, an ultrasonic radar system was designed and implemented experimentally for
distance measurements purposes to be used in various applications. An Arduino Uno
device was used as a controller in the design beside other requirements such as
servomotor, ultrasonic sensor and computer for distance calculation of objects or
obstacles placed at different angles (from 0 to 180 degrees) within the range up to 40
centimeters.
9. References
[1] https://picobricks.com/blogs/info/what-is-buzzer?currency=USD
[2] https://projecthub.arduino.cc/nimishac/ultrasonic-radar-with-arduino-19baa3
[3] https://techatronic.com/radar-using-arduino-ultrasonic-sensor/
[4] https://circuitdigest.com/article/servo-motor-working-and-basics
10. Appendix
Arduino Code
#include<Servo.h>
#define trigPin 2
#define echoPin 3
#define LED_PIN 7
#define LED_PING 8
long duration;
int distance;
14
Servo myservo;
void setup(){
pinMode(LED_PIN, OUTPUT);
pinMode(LED_PING, OUTPUT);
pinMode(trigPin , OUTPUT);
pinMode(echoPin, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
myservo.attach(9);
Serial.begin(9600);
void loop(){
int i;
myservo.write(i);
delay(15);
distance = calculateDistance();
Serial.print(i);
Serial.print(",");
Serial.print(distance);
Serial.print(".");
myservo.write(i);
delay(15);
distance = calculateDistance();
15
Serial.print(i);
Serial.print(",");
Serial.print(distance);
Serial.print(".");
int calculateDistance()
{digitalWrite(LED_PIN, HIGH);
digitalWrite(LED_PING, LOW);
digitalWrite(BUZZER_PIN, HIGH); }
else{
digitalWrite(LED_PIN, LOW);
digitalWrite(LED_PING, HIGH);
digitalWrite(BUZZER_PIN, LOW);}
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
distance = duration*0.034/2;
return distance;
}}
16
Processing Code
import processing.serial.*;
import java.awt.event.KeyEvent;
import java.io.IOException;
Serial myPort;
String angle="";
String distance="";
String data="";
String noObject;
float pixsDistance;
int index1=0;
int index2=0;
PFont orcFont;
void setup() {
smooth();
myPort.bufferUntil('.');
void draw() {
noStroke();
fill(0, 4);
17
fill(98, 245, 31);
drawRadar();
drawLine();
drawObject();
drawText();
data = myPort.readStringUntil('.');
index1 = data.indexOf(",");
iAngle = int(angle);
iDistance = int(distance);
void drawRadar() {
pushMatrix();
translate(width/2, height-height*0.074);
noFill();
strokeWeight(2);
18
line(-width/2, 0, width/2, 0);
popMatrix();
void drawObject() {
pushMatrix();
translate(width/2, height-height*0.074);
strokeWeight(9);
pixsDistance = iDistance*((height-height*0.1666)*0.025);
if (iDistance<40) {
line(pixsDistance*cos(radians(iAngle)), -pixsDistance*sin(radians(iAngle)),(width-
width*0.505)*cos(radians(iAngle)), -(width-width*0.505)*sin(radians(iAngle)));
popMatrix();
void drawLine() {
pushMatrix();
strokeWeight(9);
19
stroke(30, 250, 60);
translate(width/2, height-height*0.074);
popMatrix();
void drawText() {
pushMatrix();
if (iDistance>40) {
} else {
fill(0, 0, 0);
noStroke();
textSize(25);
textSize(40);
20
text("Distance: ", width-width*0.26, height-height*0.0277);
if (iDistance<40) {
textSize(25);
translate((width-width*0.4994)+width/2*cos(radians(30)), (height-height*0.0907)-
width/2*sin(radians(30)));
rotate(-radians(-60));
text("30", 0, 0);
resetMatrix();
translate((width-width*0.503)+width/2*cos(radians(60)), (height-height*0.0888)-
width/2*sin(radians(60)));
rotate(-radians(-30));
text("60", 0, 0);
resetMatrix();
translate((width-width*0.507)+width/2*cos(radians(90)), (height-height*0.0833)-
width/2*sin(radians(90)));
rotate(radians(0));
text("90", 0, 0);
resetMatrix();
translate(width-width*0.513+width/2*cos(radians(120)), (height-height*0.07129)-
width/2*sin(radians(120)));
rotate(radians(-30));
text("120", 0, 0);
resetMatrix();
21
translate((width-width*0.5104)+width/2*cos(radians(150)), (height-height*0.0574)-
width/2*sin(radians(150)));
rotate(radians(-60));
text("150", 0, 0);
popMatrix();
22