Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

5/15/2018 Trending New Hot

Arduino 101: Using 2 ultrasonic sensors and servo motorPromoted


— Steemit
Login (/login.html) Sign up
(/trending/utopian- (/created/utopian- (/hot/utopian- (/promoted/utopian-
(/) (https://signup.steemit.com) (/static/search.html) (/submit.html)
io) io) io) io)

Arduino 101: Using 2 ultrasonic


sensors and servo motor
ted7 (58) (/@ted7) in utopian-io (/trending/utopian-io) •  3 months ago

What Will I Learn?


You will learn the working principle of the ultrasonic sensor.
You will learn how to program the arduino board by using the Arduino
so ware.
You will learn how to use 2 ultrasonic sensors and a servo motor to
create your very own motion follower circuit.

Requirements
Hardware

Arduino Uno
image.png
Image source

https://steemit.com/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and-servo-motor 1/19
T di N H t P t d
5/15/2018 Trending New Hot Promoted
Ultrasonic sensors(2Arduino
pcs.) 101: Using 2 ultrasonic sensors and servo motor — Steemit
Login (/login.html) Sign up
(/trending/utopian- (/created/utopian- (/hot/utopian- (/promoted/utopian-
(/) (https://signup.steemit.com) (/static/search.html) (/submit.html)
io) io) io) io)

Image source
Servo motor

Image source
Jumper wires
image.png
Image source
Breadboard
image.png
https://steemit.com/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and-servo-motor 2/19
T di N H t P t d
5/15/2018 Trending New Hot
Arduino 101: Using 2 ultrasonic sensors and servo motorPromoted
— Steemit
Image source Login (/login.html) Sign up
(/trending/utopian- (/created/utopian- (/hot/utopian- (/promoted/utopian-
(/) Computer (https://signup.steemit.com) (/static/search.html) (/submit.html)
io) io) io) io)
image.png
Image source
USB type A to B cable

Image source
So ware
Arduino so ware/ Arduino IDE

Knowledge

Basic electronics and programming knowledge

Di iculty
Intermediate

https://steemit.com/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and-servo-motor 3/19
T di N H t P t d
Trending New Hot Promoted
5/15/2018
Project description Arduino 101: Using 2 ultrasonic sensors and servo motor — Steemit
Login (/login.html) Sign up
(/trending/utopian- (/created/utopian- (/hot/utopian- (/promoted/utopian-
(/) This project uses the Arduino Uno board as the main controller for the
(https://signup.steemit.com) (/static/search.html) (/submit.html)
io) io) io) io)
whole circuitry. There will be 2 ultrasonic sensors that will serve as the
input and triggering device for the servo motor. Once something is in front
of the sensors, (assuming that there is a person or a moving object in
front) the sensors will send the data to the arduino board stating that there
is something near it, thus activating the servo motor to start moving and
face the source of the movement.

Component description

Arduino Uno - is a type of arduino board that is used regularly by specialist


and hobbyist because it is the most robust of the bunch. It is stuffed with a
microcontroller board that is based on ATMega328P with 14 advanced I/O
pins (6 are PWM outputs), 6 analog inputs, 16 Mhz quartz crystal, a power
jack, a USB port, an ICSP header and a reset button.

Ultrasonic sensor - is a type of device that can measure the distance to an


object by the use of sound waves. It measures the distance by sending
sound waves at a speci c frequency and listening for that sound wave to
bounce back. By recording the elapsed time between the sound wave being
produced and the sound wave bouncing back, it is able to compute the
distance between the sonar sensor and the object.

Servo Motor - is a type of motor that is built for precise control of the
angular or linear position, velocity and acceleration. It consists of a
suitable motor that is coupled to a sensor for position feedback.

Tutorial Contents
https://steemit.com/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and-servo-motor 4/19
T di N H t P t d
Trending New Hot Promoted
5/15/2018
Motion Follower Arduino
circuit [2 ultrasonic Login sensors and servoSign motor]
101: Using 2 ultrasonic sensors and servo motor — Steemit
(/login.html) up
(/trending/utopian- (/created/utopian- (/hot/utopian- (/promoted/utopian-
(/) (https://signup.steemit.com) (/static/search.html) (/submit.html)
io) 1: Gather the parts
Step io) io) io)

You can buy the following components in your nearby electronics shop
or order them online.

image.png

Step 2: Construct the circuit

Connect the Sources

Connect the 5V pin of the arduino board to the top/bottom row of the
breadboard(the connections here are horizontally connected).
Connect the GND pin of the arduino board to the top/bottom row of the
breadboard but make sure not to place the sources together as this will
short the arduino board and may cause damage.

https://steemit.com/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and-servo-motor 5/19
T di N H t P t d
Trending New Hotsensors and servo motorPromoted
5/15/2018
Connect the sensors Arduino 101: Using 2 ultrasonic
Login (/login.html) Sign up
— Steemit

(/trending/utopian- (/created/utopian- (/hot/utopian- (/promoted/utopian-


(/) (https://signup.steemit.com) (/static/search.html) (/submit.html)
io) sensor
1st io) io) io)

Connect the VCC pin of the ultrasonic sensor to the positive terminal
in the breadboard.
Connect the GND pin of the ultrasonic sensor to the negative terminal
in the breadboard.
Connect the TRIG pin of the ultrasonic sensor to pin number 11 of the
arduino board.
Connect the ECHO pin of the ultrasonic sensor to pin number 10 of the
arduino board.

2nd sensor

Connect the VCC pin of the ultrasonic sensor to the positive terminal
in the breadboard.
https://steemit.com/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and-servo-motor 6/19
T di N H t P t d
5/15/2018 Trending New Hot Promoted
Arduino 101: Using 2 ultrasonic sensors and servo motor — Steemit
Connect the GND pin of the ultrasonic Loginsensor to the negative
(/login.html) Sign upterminal
(/trending/utopian- (/created/utopian- (/hot/utopian- (/promoted/utopian-
(/) in the breadboard. (https://signup.steemit.com) (/static/search.html) (/submit.html)
io) io) io) io)
Connect the TRIG pin of the ultrasonic sensor to pin number 13 of the
arduino board.
Connect the ECHO pin of the ultrasonic sensor to pin number 12 of the
arduino board.

Connecting the Servo motor

Connect the positive terminal of the servo motor(red wire) to the


voltage source grid in the breadboard.
Connect the ground terminal of the servo motor (black wire) to the
negative terminal in the breadboard.
Connect the data wire(yellow wire) of the servo motor to pin number 9
of the arduino board.

https://steemit.com/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and-servo-motor 7/19
T di N H t P t d
5/15/2018 Trending New Hot Promoted
Arduino 101: Using 2 ultrasonic sensors and servo motor — Steemit
Note: Use color coded wires so thatLogin you will not get confuse
(/login.html) Sign up with the
(/trending/utopian- (/created/utopian- (/hot/utopian- (/promoted/utopian-
(/) wirings. (https://signup.steemit.com) (/static/search.html) (/submit.html)
io) io) io) io)

Step 3: Programming

Connect the arduino board to the computer by using the usb cable type
a to b.

Image source
Once connection has been established, open the Arduino IDE and go to
Tools > Board: > then select the Arduino/Genuino Uno.

https://steemit.com/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and-servo-motor 8/19
T di N H t P t d
5/15/2018 Trending New Hot
Arduino 101: Using 2 ultrasonic sensors and servo motorPromoted
— Steemit
Login (/login.html) Sign up
(/trending/utopian- (/created/utopian- (/hot/utopian- (/promoted/utopian-
(/) (https://signup.steemit.com) (/static/search.html) (/submit.html)
io) io) io) io)

Copy the codes below and paste it into your sketch in the Arduino IDE.

https://steemit.com/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and-servo-motor 9/19
T di N H t P t d
5/15/2018 Trending New Hot
Arduino 101: Using 2 ultrasonic sensors and servo motorPromoted
— Steemit
#include <Servo.h> Login (/login.html) Sign up
(/trending/utopian- (/created/utopian- (/hot/utopian- (/promoted/utopian-
(/) Servo myservo; //creates servo (https://signup.steemit.com)
object (/static/search.html) (/submit.html)
io) io) io) io)
const int echopin1 = 10; //sets echopin1 @pin 10
const int trigpin1 = 11; //sets trigpin1 @pin 11
const int echopin2 = 12; //sets echopin2 @pin 12
const int trigpin2 = 13; //sets trigpin2 @pin 13
const int servo = 9; //sets servo @pin 9
long Rightduration, Leftduration, Rightinch, Leftinch; //establishes
int threshold = 10; //Sets the sensor threshold at 10 inches
int angle = 80; //Sets the Initial angle
void setup()
{
myservo.attach(9); //attaches the servo on pin 9
}
void loop()
{
pinMode(trigpin1, OUTPUT); //trigpin1 is set as output
digitalWrite(trigpin1, LOW); //sets the trigpin1 to give low pulse
delayMicroseconds(3); //duration is 3 microseconds
digitalWrite(trigpin1, HIGH); //sets the trigpin1 to give high pu
delayMicroseconds(5); //duration is 5 microseconds
digitalWrite(trigpin1, LOW);
Rightduration = pulseIn(echopin1, HIGH); //reads high pulse
pinMode(trigpin2, OUTPUT); //trigpin2 is set as output
digitalWrite(trigpin2, LOW); //sets the trigpin2 to give low pulse
delayMicroseconds(3); //duration is 3 microseconds
digitalWrite(trigpin2, HIGH); //sets the trigpin2 to give high pu
delayMicroseconds(5); //duration is 5 microseconds
https://steemit.com/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and-servo-motor 10/19
T di N H t P t d
5/15/2018 Trending New Hot Promoted
Arduino 101: Using 2 ultrasonic sensors and servo motor — Steemit
digitalWrite(trigpin2, LOW); Login (/login.html) Sign up
(/trending/utopian- (/created/utopian-
Leftduration = pulseIn(echopin2, HIGH);
(/hot/utopian- (/promoted/utopian-
//reads high pulse
(/) (https://signup.steemit.com) (/static/search.html) (/submit.html)
io) //this will convert
io) the elapsed timeio) io)
into the distance
Rightinch = microsecondsToInches(Rightduration);
Leftinch = microsecondsToInches(Leftduration);
follow(); //follows the movement
}
long microsecondsToInches(long microseconds)
{
// The speed of sound is 340 m/s or 73.746 microseconds per inch.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance traveled.
return microseconds / 74 / 2;
}
void follow() //conditions for the follow command
{
if (Leftinch <= threshold || Rightinch <= threshold)
{
if (Leftinch + 2 < Rightinch)
{
angle = angle - 2;
}
if (Rightinch + 2 < Leftinch)
{
angle = angle + 2;
}
}
if (angle > 160)

https://steemit.com/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and-servo-motor 11/19
T di N H t P t d
5/15/2018 Trending
{
New Hot
Arduino 101: Using 2 ultrasonic sensors and servo motorPromoted
— Steemit
Login (/login.html) Sign up
(/trending/utopian- (/created/utopian- (/hot/utopian- (/promoted/utopian-
(/) angle = 160; (https://signup.steemit.com) (/static/search.html) (/submit.html)
io) } io) io) io)

if (angle < 0)
{
angle = 0;
}
myservo.write(angle);
}

What this code does is this, rst it declares the pins of the ultrasonic
sensors and servo as cons int meaning it will be a read-only variable
and cannot be changed. Then it declares the duration and sets the
distance as inches. The distance threshold is set to 10 inches with an
initial angle of 80 degrees of the servo motor. Now, once the
ultrasonic sensors detect something within the 10 inches threshold, it
will send that data to the arduino uno which will then activate the
servo motor to face the source of the movement. This will follow that
object if its within 10 inches with 2 degrees per step, the range of
angle that it can follow is from 0 - 160 degrees. Once the source of
movement is gone, it will stop the servo from moving and will keep
waiting until a new movement has been detected.

https://steemit.com/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and-servo-motor 12/19
T di N H t P t d
5/15/2018 Trending New Hot Promoted
Arduino 101: Using 2 ultrasonic sensors and servo motor — Steemit
A er typing the code into your arduino Login IDE, click Verify
(/login.html) Signto up
compile and
(/trending/utopian- (/created/utopian- (/hot/utopian- (/promoted/utopian-
(/) save your sketch and this will also (https://signup.steemit.com)
check for any errors in the program.
(/static/search.html) (/submit.html)
io) io) io) io)

https://steemit.com/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and-servo-motor 13/19
T di N H t P t d
5/15/2018 Trending New Hot Promoted
Arduino 101: Using 2 ultrasonic sensors and servo motor — Steemit
If there is no error found in the program, click the Upload
Login (/login.html) Sign button
up to
(/trending/utopian- (/created/utopian- (/hot/utopian- (/promoted/utopian-
(/) install the program into the arduino board.
(https://signup.steemit.com) (/static/search.html) (/submit.html)
io) io) io) io)

Step 4: Testing

Once the installation is complete, remove the arduino uno from the
computer and plug in your battery pack to start the arduino board.
https://steemit.com/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and-servo-motor 14/19
T di N H t P t d
5/15/2018 Trending New Hot Promoted
Once it has started, Arduino 101: Using 2 ultrasonic sensors and servo motor — Steemit
the circuit should Login already be functioning.
(/login.html) Sign up Hover
(/trending/utopian- (/created/utopian- (/hot/utopian- (/promoted/utopian-
(/) you hand withing the 10 inches range from the sensor, this should(/static/search.html) (/submit.html)
(https://signup.steemit.com)
io) io) io) io)
detect your hand and will move to face in the direction of your hand.
Slowly move your hand sideways and this should follow it by 2 degrees
per step until it reaches its 160 degrees or 0 degrees.
Now, remove your hand away from the sensor, this should stop the
servo motor from moving from its place.
If none is working, redo the previous steps and make sure that you
have followed each step carefully.
Also check your connections as the wire from the breadboard will
easily get loosen especially if the wire is not properly tted into the slot
of the breadboard.

Curriculum
Check out my other arduino tutorials below:

Arduino 101: Automated parking gate circuit using ultrasonic and


servo motor
Arduino 101: Motion activated Servo motor with Arduino
Arduino 101: PIR based burglar alarm with Piezoelectric buzzer and
Arduino
Arduino 101:Pir based burglar alarm with piezoelectric buzzer and
arduino
Arduino for beginners

https://steemit.com/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and-servo-motor 15/19
T di N H t P t d
Trending New Hot Promoted
5/15/2018
Posted on Utopian.io - Rewarding Open Source Contributors
Arduino 101: Using 2 ultrasonic sensors and servo motor — Steemit
Login (/login.html) Sign up
(/trending/utopian- (/created/utopian- (/hot/utopian- (/promoted/utopian-
(/) (https://signup.steemit.com) (/static/search.html) (/submit.html)
io) io) io) io)

utopian-io (/trending/utopian-io) technology (/trending/technology) howto (/trending/howto)

cebu (/trending/cebu) philippines (/trending/philippines)

3 months ago by ted7 (58) (/@ted7) $23.77 23 votes Reply 9 (/utopian-io/@ted7/arduino-101-

using-2-ultrasonic-sensors-and-servo-motor)

       

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!

Sign up. Get STEEM!

Sort Order:  Trending

jrswab (63) (/@jrswab)  ·  3 months ago (/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and-servo- [-]


motor#@jrswab/re-ted7-arduino-101-using-2-ultrasonic-sensors-and-servo-motor-20180130t220947090z)

This is a stellar post!


I will be featuring it in my weekly #technology (/trending/technology) curation post for the @minnowsupport
(/@minnowsupport) project and the Tech Bloggers' Guild ! TBG is a new group of Steem tech bloggers and
content creators looking to improve the overall quality of the niche.

If you wish to not be featured in the curation post this Saturday please let me know. Keep up the hard work
and I hope to see you at the Tech Bloggers' Guild!
$0.00 Reply

https://steemit.com/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and-servo-motor 16/19
T di N H t P t d
5/15/2018 Trending
ted7 (58) (/@ted7) New
·  3 months agoUsing
Arduino 101: Hotsensors and servo motorPromoted
(/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and-
2 ultrasonic [-]
— Steemit
Login (/login.html) Sign up
· (/trending/utopian- (/created/utopian- (/hot/utopian- (/promoted/utopian-
servo-motor#@ted7/re-jrswab-re-ted7-arduino-101-using-2-ultrasonic-sensors-and-servo-motor-
(/) (https://signup.steemit.com) (/static/search.html) (/submit.html)
io) 20180131t015812498z)io) io) io)
whoa! that's great. i'd love my post to be featured. thanks a lot
$0.00 1 vote Reply

smaeunabs (58) (/@smaeunabs)  ·  3 months ago (/utopian-io/@ted7/arduino-101-using-2-ultrasonic- [ - ]


· sensors-and-servo-motor#@smaeunabs/re-jrswab-re-ted7-arduino-101-using-2-ultrasonic-sensors-and-
servo-motor-20180130t220947090z-2018131t123232204z)
Dugo akong ilong pero interesting kaayo! Grabe lodi! :D
$0.00 Reply

johnesan (56) (/@johnesan)  ·  3 months ago (/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and- [-]


servo-motor#@johnesan/re-ted7-arduino-101-using-2-ultrasonic-sensors-and-servo-motor-20180130t232246961z)
I have always felt anything that has to do with microcontrollers and hardware in general, was hard to deal
with. This kinda simplifies the process for me.

One thing though, what other areas can I apply this adruino programming to. I mean, ultrasonic sensors don't
look too personal for personal consumption. What are some other areas I could look into
$0.00 1 vote Reply

ted7 (58) (/@ted7)  ·  3 months ago (/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and- [-]


· servo-motor#@ted7/re-johnesan-re-ted7-arduino-101-using-2-ultrasonic-sensors-and-servo-motor-
20180131t015630404z)
It is always hard at first my friend, I also had a hard time figuring out how this microcontrollers work
the first time but a er tons of research and desperation, I was finally able to understand it . I'm sure
you can also figure it out in no time! The hardware are much simpler than the so ware you just need
to understand their functionality/mechanism.
As for the the areas that you can apply this arduino, you can use every sensors available to make
anything you want. you can make your home smart just by using the arduino and some relays and
sensors, you can create a robot arm by using some servos and controller. you can even create make
an automatic co ee maker that will activate a er it senses that you have already woken up and pour

https://steemit.com/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and-servo-motor 17/19
T di N H t P t d
Trending New
it into a cup. The possibilities areUsing
endless withHotusing
sensors arduino andPromoted
other
Signmicrocontrollers especially
5/15/2018 Arduino 101: 2 ultrasonic and servo motor — Steemit
Login (/login.html) up
(/trending/utopian- (/created/utopian-
this is the time of IoT where you can do anything (/hot/utopian- (/promoted/utopian-
wirelessly!Technology is just super exciting, dont
(/) (https://signup.steemit.com) (/static/search.html) (/submit.html)
io) you agree? io) io) io)
$0.00 Reply

deathwing (65) (/@deathwing)  ·  3 months ago (/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors- [-]


and-servo-motor#@deathwing/re-ted7-arduino-101-using-2-ultrasonic-sensors-and-servo-motor-
20180131t134933125z)
Thank you for the contribution. It has been approved.

You can contact us on Discord .


[utopian-moderator]
$0.00 1 vote Reply

ted7 (58) (/@ted7)  ·  3 months ago (/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and- [-]


· servo-motor#@ted7/re-deathwing-re-ted7-arduino-101-using-2-ultrasonic-sensors-and-servo-motor-
20180131t135458589z)
Thanks a lot @deathwing (/@deathwing) :D
$0.00 Reply

calvinklein47 (42) (/@calvinklein47)  ·  3 months ago (/utopian-io/@ted7/arduino-101-using-2-ultrasonic- [-]


sensors-and-servo-motor#@calvinklein47/re-ted7-arduino-101-using-2-ultrasonic-sensors-and-servo-motor-
20180201t173729274z)
Nice one this could help me a lot of project.
$0.00 Reply

utopian-io (67) (/@utopian-io)  ·  3 months ago (/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and- [ - ]


servo-motor#@utopian-io/re-ted7-arduino-101-using-2-ultrasonic-sensors-and-servo-motor-20180202t033700923z)

Hey @ted7 (/@ted7) I am @utopian-io (/@utopian-io). I have just upvoted you!

Achievements
https://steemit.com/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and-servo-motor 18/19
T di N H t P t d
5/15/2018 Trending New
You have less than 500 followers.
Arduino Just gave2 you
101: Using a Hot
gisensors
ultrasonic to help
and you motorPromoted
servo succeed!
— Steemit
Login (/login.html) Sign up
(/trending/utopian- (/created/utopian-
Seems like you contribute quite o en. AMAZING! (/hot/utopian- (/promoted/utopian-
(/) (https://signup.steemit.com) (/static/search.html) (/submit.html)
io) io) io) io)
Suggestions
Contribute more o en to get higher and higher rewards. I wish to see you o en!
Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly
based on that. Good luck!

Get Noticed!
Did you know project owners can manually vote with their own voting power or by voting power
delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord . Lets GROW TOGETHER!

Vote for my Witness With SteemConnect


Proxy vote to Utopian Witness with SteemConnect
Or vote/proxy on Steemit Witnesses (https://steemit.com/~witnesses)

(https://steemit.com/~witnesses)

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat?
Join me on Discord https://discord.gg/Pc8HG9x
$0.00 Reply

https://steemit.com/utopian-io/@ted7/arduino-101-using-2-ultrasonic-sensors-and-servo-motor 19/19

You might also like