In this article we are going build an Arduino-based robot to navigate on it’s own by avoiding any obstacle in front of the robot. To do that, we will equip our mobile robot with a frontal ultrasonic sensor to know the distance in front of the robot. First we will assemble the robot, then write the corresponding code, and finally test it. Let’s start!

Hardware & Software Requirements

Let’s first see what we need for our mobile robot. The first thing that we will need is the robot chassis itself. There are many options out there, but I really recommend using the DFRobot MiniQ 2 wheels robot chassis. It is a small robot with 2 motors and 2 wheels, and it is really easy to mount an Arduino board on it.

Then, you will need several Arduino boards to control the robot. The first one is an Arduino Uno board, which will be the ‘brain’ of the robot. Then, you will need a DFRobot motor shield to easily control the two wheels of the robot. I also used a prototyping shield on top of the robot in case I wanted to add more sensors in the future, but that’s not necessary.

After that, you will also need an URM37 ultrasonic sensor to measure distance in front of the robot. You will also need a battery to power up the robot so you don’t have to keep it attached to your computer for power. I used a simple 3.7V LiPo battery for that task, along with an Adafruit Powerboost 500 board to convert the 3.7V from the battery to  the 5V required by the Arduino Uno board. Of course, you will also need some jumper wires to make all the connections.

This is the list of all the components you will need for this project:

On the software side, you will first need to install the Arduino IDE. I recommend version 1.5.7, that you can find on this page.

Hardware Configuration

Now we are going to configure the mobile robot. The configuration itself is very similar to another article I

This is a picture of the assembled robot:

Basic Obstacles Avoidance with an Arduino Robot

When you have the robot assembled, connect the ultrasonic sensor: connect the VCC pin of the sensor to Arduino 5V, the GND pin to GND, and finally the output of the sensor (pin 4) to Arduino pin A0.

Writing the Code for Autonomous Navigation

Now we are going to see the details of the code. First we declare the motor pins:

int speed_motor1 = 6;  
int speed_motor2 = 5;
int direction_motor1 = 7;
int direction_motor2 = 4;

Then, we define the ultrasonic sensor pin:

int distance_sensor = A0;

By default we make the robot move forward, you can check details of function in the complete code:


Then, in the loop() function we measure the distance in front of the robot:

int distance = measure_distance(distance_sensor);

If distance is smaller than 10cm, we make the robot go backwards, turn around, and then go forward again:

if (distance < 10) {
    // Go backward
    // Turn around
    // Go forward again

You can get the full code from:

Let’s Test the Project!

Now it’s time to test the project. Make sure the battery is connected to the robot, and also make sure the robot is standing on a safe place where wheels don’t touch the ground. Otherwise it could just go away and be damaged.

Then, upload the code via the Arduino IDE, and disconnect the USB cable after that.

Finally, leave the robot to move around. You can see an example in this video:

You should see that at every obstacle the robot is going backwards, turning around, and then moving forward again!

How to go further

In this article we build a simple Arduino robot that moves around on its own and avoid obstacles.

To improve the project, you can for example add more sensors (for example on the sides). You can also improve the behaviour of the robot by building more advanced behaviors in the code. Finally, you can mount the ultrasonic sensor on servo motor to see all around the robot.

Did you enjoy this article? Have you ever built a robot that avoids obstacles? Please comment below!