Obstacle Avoiding Robot With Servo Motor Arduino





Introduction: Obstacle Avoiding Robot With Servo Motor Arduino

About: undergraduate of University of Moratuwa ,SriLanka

what could be more awsome than building your own robot,today im gonna explain how to build your own obstacle avoiding robot with a servo motor and an ultrasonic sensor.dont worry! I'll guide you step by step through the processes.

so basically ,if you search online ,you will be able to find so many obstacle avoiding robots with different algorithms,in here the algorithm will be,

  • robot moves forward
  • when detect an obstacle with 35 cm range


look right (40') check for obstacle,if no obstacle is detected turn the ser vo to initial positon ,turn the chassis to right, move forward ,

if obstacle detected in rightside ,look left (130'),if no obstacle turn the servo to initial position turn the chassis left ,move forward in that direction

if obstacle detected in all 3 directions,rotate the robot chassis in 180 degrees and go forward.

now lets see how to build the robot.

Step 1: Finding Components

you will need.

2 wheel robot chassis

arduino UNO/MEGA

l298N motor driver

HC-SR04 ultrasonic sensor

towerpro micro servo 9g

11.1V lipo battery

jumper wires

nuts and bolts,screwdriver

double sided tape

Step 2: Assembling

at this point you should have your 2 wheeled robot chassis on your hand ready to build your robot.assemble the lipo battery,motor driver,arduino, on to the robot chassis.use double sideded tape to fix them onto the chassis .make sure they are properly fixed.

take the micro servo and attach a ultrasonic sensor to a stand like in the picture and attach it to the chassis.

Step 3: L298N Motordriver Connection

attach the Left motor to OUT1 and OUT2

Right motor to OUT3 and OUT4

detach the pin holders in ENA and ENB ,these pins will be use to control the speed of the robot.and let the pin connection be





ENA -9

ENB - 3

connect the lipo battery as shown in the diagram.

if the motors are rotating in the other direction for forward command then simply intechange the motor wires

Step 4: Ultrasonic Sensor Connection





this sensor has 4 pins .the trigger pin will emit 8 ultrasonic waves,to trigger the trgger pin we must give a pulse of 10us-1ms with freq 40Hz.

then by equation S=Ut ,distance can be measured here U will be velocity of air 330ms-1 converted to cm per ms and t will be the high period of the echo/2

the connection with arduino will be as the given diagram .

digitalWrite(trigpin, HIGH);
delayMicroseconds(10); digitalWrite(trigpin, LOW);

duration = pulseIn(echo, HIGH); distance = (1 / 29.1) * (duration / 2); delay(60);

Step 5: Servo Motor Connection

red(+)- arduino 5V

brown(-)-arduino 0V

orange(signal pin)-9 (ANY PWM PIN)

if given it to rotate at 90 degrees it doesnt mean that it will rotate 90' where the axis was at last..it means that it will rotate to 90 position .(longer flaps will be vetical)

keep in mind that this servomotor can rotate only 0-180 degrees .this is an advatange for us in positional control

if we incoparate PWM with angle 0-255 maps to 0-180 degrees

without using the map operation we can use a built in function

#include <servo.h>
Servo SM;

SM.write(90);// the angle you want to rotate

Step 6: Programming

1. Download and Install the Arduino

Desktop IDE · windows - https://www.arduino.cc/en/Guide/Windows ·

Mac OS X - https://www.arduino.cc/en/Guide/MacOSX ·

Linux - https://www.arduino.cc/en/Guide/MacOSX

2. Download and open obstacle_avoid.ino

3. Upload the code to the arduino board via a USB cable

Important !-when given an instance if there are no obstacle within 35 cm range sometimes the programme might execute SM.Write operation part.(since ultrasonic sensor gives dummy Values)to eliminate this problem we have to wait 60ms in else part.

thats it!! now you have a obstacle avoiding robot.




    • Clocks Contest

      Clocks Contest
    • Creative Misuse Contest

      Creative Misuse Contest
    • Water Contest

      Water Contest

    4 Discussions

    Nice article. Very good for beginners. Your robot is decent!!!