Arduino Obstacle Avoidance Robot (NEW SCHEMATIC)




Hey guys,

In this project, we will be making an obstacle avoidance robot using the Arduino Uno. This robot is designed to move around and avoid any obstacle it encounters using an ultrasonic sensor. For those who needed a schematic diagram, I have added it for visual learners.



  • Scissors
  • Tape
  • Computer
  • Arduino Program
  • Your Hands


  • Velcro
  • Hot-Glue Gun
  • Pliers
  • 9 Volt Battery

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Step 1: Servos

The first step is to make sure your servos are working. For the Parallax continuous servos, use the jumper wires to connect the black wire to GND, red wire to 5V, and the white wire to digital pin 9 on the Arduino. For the Hi-Tec servo, the only difference is that the signal wire for the servo is yellow, compared the the white wire on the Parallax servo. After, just upload the sweep code that I have uploaded for you at the bottom of the page.

Are your servos working? If it does, good. If not, then go get a refund from your supplier. Now that your servos are working, it is time to center them. This will ensure that your servos are well balanced and even. I have provided you with the code file below.

After you have uploaded check to see if your servo moves. If it doesn't, don't freak out because that's good news. You're all set. If your servo does move, use a Phillips screwdriver, and slowly turn either way in the tiny slot to adjust until your servo stops moving. After you are done, you are ready for the next step. Yay!

Step 2: Step 2: Ping Sensor

Now, you are ready to test your Ping))) sensor. First, connect the GND pin on the Ping sensor to the GND on the Arduino. Then connect 5V pin on the Ping sensor to the 5V slot on the Arduino. Lastly, connect the SIG pin on the Ping sensor to digital pin 7 on the Arduino. After that, upload the code onto the Arduino and go up to tools, and open up serial monitor. A window will open, and it will show you the sensor's distance from an object in front of it. Congrats! You are done testing the Ping))) sensor!

Step 3: Step 3: Attaching the Servos

This is the fun part! You will now be completing the hardware section of your project. First, you will have to assemble the Tamiya track kit. If you want, you can assemble the whole thing, but the thing you really need to assemble are the 2 tracks and the rest of the wheels. You will notice that the wheels with teeth do not have a free moving axle, but a gearbox instead. You do not need to assemble or even use the gearbox! Save the parts for other projects because we only need the 2 wheels with teeth. First, you will have to cut the plus signs that comes with your continuous servos. Just cut half way down, about 2 holes from the center, enough to fit it into the Tamiya wheels. After you are done doing it to both servos (ONLY THE CONTINUOUS ONES), hot glue the wheels the the servos. Now, place the servos with the bottoms touching each other, sides facing down, and the long part pointing out, or else your robot will run backwards. Then attach the sensor servo (180 degree servo) straight up with the long side pointing back. You can use hot glue, velcro, or tape to attach the servos down to the base (I used velcro, but anything will work). Now using the breadboard, connect the servos to the 5V port and both to a common ground (GND). Also connect the servos to the right digital pins. Then attach the battery box between the continuous and 180 servos and stick the breadboard on top of the battery box.

Step 4: Step 4: Connecting the Sensors and the Rest

Now the servos are done, Its time to connect the Ping))) sensor. First, you must attach the Ping)) sensor onto the servo. I glued 6 Styrofoam meat trays cut into squares on top of each other to make a block. You can do the same, use cardboard, or just purchase a block of foam. I do not recommend gluing the sensor directly to the block, but instead use pins, or nails to pin the sensor to the block. Then, I used tape to secure the Ping sensor onto the block. After that, I stuck the breadboard on top of the battery pack (the breadboard was really small) and the Arduino onto both of the continuous rotational servos.

Step 5: Step 5: Finishing Touches and Coding

Yay! This is the final step! Now, connect the arduino to your computer and paste in this code into your Arduino program and install it into your Arduino (make sure you verify the code, just in case). I give credit for this code to Brandon Campbell. Check out his YouTube videos because he got some really awesome videos and tutorials (this Instructable was based on this code, and since he didn't give a tutorial I decided to give one myself).


After that, plug in the power pack into the smaller plug onto the Arduino, turn it on, and watch your robot go! If it does not go, feel free to ask me in the comment below, or through my email. I will try my best to reply as soon as possible and thanks for reading this Instructable! If you like this, please follow me for more fun projects like these and I will see you again!

Be the First to Share


    • Made with Math Contest

      Made with Math Contest
    • Multi-Discipline Contest

      Multi-Discipline Contest
    • Robotics Contest

      Robotics Contest

    28 Discussions


    2 years ago

    if i use different component (for example, different servo motor and different ultrasonic sensor) does it require different code?

    1 reply

    Reply 2 years ago


    No, it will not require a different code unless the component has different connections from the components I've been using (i.e. instead of 3 ports there are 4)


    3 years ago

    Great tutorial and well written. Unfortunately on my Robot, I'm using an HC-SRO4 Sensor and am having a hard time relating that to your PingTest sketch. :(


    3 years ago

    can u please tell me how can i start my roboat

    nd which switch i have on


    4 years ago on Introduction


    I am
    building an obstacle avoidance robot using the Arduino UNO board. The robot
    consists of a pair of dc motors being driven using the L293D H-Bridge which has
    been functioning just fine for a line tracer robot project. I have added a
    HiTEC HS-422 servo motor (in parallel with a 100 micro farad decoupling
    capacitor) for the distance measurement part to be able to rotate the ping
    sensor to the left (~10 degrees) and right (~170 degrees).

    Both DC
    motors and the servo have their own dedicated power source not to overload the
    Arduino power output and their grounds are connected to the common ground with
    the Arduino board (reported as a common mistake).

    DC motors
    and the servo work just fine on their own as the robot goes forward and
    backward and turns right and left and servo passes the sweep test and moves
    nicely to any predefined angle. As I try to run the DC motors and the servo
    together, the servo turns to almost 0 degree and kind of twitches around 0
    degree regardless of the predefined angle such as 90 or 170 degrees. I have
    tried the following scenarios and the following code for wiring the H-Bridge to
    overcome the problem but had no success:


    Controlling the speed of the DC motors with a potentiometer
    and analogWrite(right_mot_Enable_Pin,Motor_Speed):

    L293D-pin-1 connected to Arduino-pin- ~6

    L293D-pin-2 connected to Arduino-pin- 8

    L293D-pin-3 connected to left motor

    L293D-pin-4 connected to common ground

    L293D-pin-5 connected to common ground

    L293D-pin-6 connected to left motor

    L293D-pin-7 connected to Arduino-pin- ~10

    L293D-pin-8 connected to + 9 V Battery

    L293D-pin-9 connected to Arduino-pin- ~3

    L293D-pin-10 connected to Arduino-pin- ~5

    L293D-pin-11 connected to right motor

    L293D-pin-12 connected to common ground

    L293D-pin-13 connected to common ground

    L293D-pin-14 connected to right motor

    L293D-pin-15 connected to Arduino-pin- ~9

    L293D-pin-16 connected to +5 V from Arduino

    Servo-signal-pin connected to Arduino-pin- ~11


    No speed control on the DC motors:

    L293D-pin-1 connected to +5 V from Arduino

    L293D-pin-2 connected to Arduino-pin- ~5

    L293D-pin-3 connected to left motor

    L293D-pin-4 connected to common ground

    L293D-pin-5 connected to common ground

    L293D-pin-6 connected to left motor

    L293D-pin-7 connected to Arduino-pin- ~6

    L293D-pin-8 connected to + 9 V Battery

    L293D-pin-9 connected to +5 V from Arduino

    L293D-pin-10 connected to Arduino-pin- 8

    L293D-pin-11 connected to right motor

    L293D-pin-12 connected to common ground

    L293D-pin-13 connected to common ground

    L293D-pin-14 connected to right motor

    L293D-pin-15 connected to Arduino-pin- ~9

    L293D-pin-16 connected to +5 V from Arduino

    Servo-signal-pin connected to Arduino-pin- 4

    #include <Servo.h>

    Servo myservo;

    int Motor_Speed = 0;

    const int left_mot_forward_Pin = 8; // Left Motor connected to digital pin 8

    const int left_mot_backward_Pin = 10; // left Motor connected to digital pin 10

    const int right_mot_forward_Pin = 9; // right Motor connected to digital pin 9

    const int right_mot_backward_Pin = 5; // right Motor connected to digital pin 5

    const int left_mot_Enable_Pin = 6; // Left Motor Enable Pin

    const int right_mot_Enable_Pin = 3; // Right Motor Enable Pin

    const int Pot_Pin = A3;
    // Pot Pin

    void setup()



    Motor_Speed = map(analogRead(Pot_Pin), 0, 1023, 0, 255);

    myservo.attach(11); //
    attaches the servo on pin 11 to the servo object

    pinMode(left_mot_forward_Pin, OUTPUT); // Left Motor

    pinMode(left_mot_backward_Pin, OUTPUT); // Left Motor

    pinMode(right_mot_forward_Pin, OUTPUT); // Right Motor

    pinMode(right_mot_backward_Pin, OUTPUT); // Right Motor


    void loop()


    //Rotate the servo to face the front



    void moveForward() //This function tells the robot to go


    digitalWrite(right_mot_forward_Pin, HIGH); //Right Motor Forward

    digitalWrite(right_mot_backward_Pin, LOW); //Right Motor Backward


    digitalWrite(left_mot_forward_Pin, HIGH); //Left Motor Forward

    digitalWrite(left_mot_backward_Pin, LOW); //Left Motor Backward



    4 replies

    Reply 3 years ago


    I am building the same robot as yours but unfortunately I dont have much knowledge about how to use servo so can you please send me the code with little guidance about using servos thanks in advance


    Reply 3 years ago


    Here is the link to the code of the entire robot if it is what you're asking for:

    So a servo is a little like a regular dc motor except that servos are easier to program with a microcontroller. That is one of the biggest differences of it. They usually come in micro, standard and giant size.

    Servos usually have a 180 degree freedom of rotation but there are also fully rotating 360 degree freedom of rotation servos out on the market, like the Parallax Continuous servo motor, which I used 2 on my Obstacle Avoidance Robot.

    The servo that I used on the robot all came with three connecting wires, but other types may come with 4 connecting wires. The three main connecting wires are GND (ground), 5V (power), and SIG (signal from microcontroller). GND wires are usually colored black. 5V wires are usually colored red, and SIG wires can come in white or yellow depending on the brand.

    5V usually connects with the 5V pins on microcontrollers. GND usually connects to GND on microcontrollers, and SIG usually connect with the numbered output pins, usually digital.

    Hoped that helped!


    Reply 3 years ago

    Thanks a lot it was very helpfull I just wanted to know that it is necessary to use a potentiometer with servo motor ?


    Reply 3 years ago

    plus the code you gave me uses two servos, intstead if I use on servo will it be feasible ?

    I use arduino uno and servo motor with 180 degree rotation and ultrasonic sensor.Can I please get the arduino code for my obstacle avoider robot. my email-

    11008500_683947535048761_5444217947516993042_o (Medium).jpg
    4 replies

    Reply 3 years ago

    hi bro I am using the same servo as yours in obstacle avoider so can you please send me the code, thanks alot in advance


    The code is located on step five, but I realized that there is a problem with some of the code links, so I will fix that. Thanks for telling me, and I will send the code to you. Your robot looks very nice, but the 180 degree standard servo is for the rotating Ping))) sensor, not the wheels.


    Reply 4 years ago on Introduction

    hi sir.. we have a project for this can you send me the code..


    3 years ago

    We had some... problems

    1. Your code didn't work very well. It kept saying it had trouble defining the servos when we tried to verify. Still trying.



    4 years ago

    Any chance of some tips on how to make this with the stuff i have already? got 2 dc gear motors and pan tilt with servos, id like to know how to change the code to use dc motors and 2 servos for the scanning.

    im using motorshield on a uno r3

    thanks in advance.

    If it is okay to you, may I see your schematic diagram. Thanks :) email me


    4 years ago

    Can I get a diagram of where the servos connect to the breadboard and arduino or u could send it to


    Reply 4 years ago on Introduction

    Hi ZakD,

    I am not sure about it, however I think it may work but don't take my word for it.