Part five of a series of articles on building a robot that can follow lines or walls and avoid obstacles!

Related Articles

How to Build a Robot:

Part 1: Design and Schematic

Part 2: PCB Design

Part 3: Testing Hardware

Part 4: Line Follower

Part 6: Wall-Following Robot


I thought it would be neat to create a robot that was easy to put together with a single soldering iron and was also affordable. I made up the following requirements for my robot:

  • Many kits are expensive, so it must be relatively inexpensive.
  • It must be easily put together without special equipment.
  • It must be easily programmable without a complicated IDE or programmer.
  • It must be powerful enough for expandability.
  • It should run off a simple power source.
  • It should be able to follow a line or a wall, and avoid obstacles.

In this article, I'll talk about how to program the robot to avoid obstacles.


Avoiding Obstacles

The approach I'm going to take is if an obstacles is detected in the path of the robot, the robot will back up and try a new direction. This allows the robot to explore areas without getting stuck or damaging itself. The sensor that is used in this robot only gives a binary output. In other words, something is either in the way or it isn't. The sensor also has a pretty wide angle of reception, so objects that are slightly off to the side may also be detected. This limits the complexity of the algorithm that can be used. If you had a sensor that could actually measure the distance to an object reliably, you could calculate if you have enough turn radius to get out of the way without going backwards.

The algorithm I decided on will tell the robot to back up for 1 second if an obstacle is detected. It will then randomly turn left or right in an attempt to avoid the obstacle. While turning left or right, it continues to check for obstacles in the way. If it detects obstacles, it will stop turning and repeat the reverse/turn cycle until free of the obstacle.



The following sketch performs the logic:

  1. Start the robot driver and wait 5 seconds. This gives you time to put the robot where you want it before it starts moving.
  2. Move forward.
  3. If the middle sensor is activated, reverse for 1 second.
  4. Randomly choose left or right.
  5. Turn for up to 1 second as long as the middle sensor is not activated.
  6. If the middle sensor is activated, go to step 3.
  7. If the sensor is not activated, go to step 2.


                    #include "robot.h"

void setup()

uint16_t lleft,lmid,lright;
boolean wleft,wmid,wright;
uint16_t avoid_count=0;
void loop()                     
  /*reverse if something is in the way and try to change direction*/
    /*choose a direction at random to avoid an obstacle*/
        if(wmid) break;
        if(wmid) break;

Avoiding Obstacles!



In this article I showed how you might use a sensor to avoid obstacle with a robot. Obstacle avoidance is important in autonomous vehicles to avoid damaging the environment or the robot itself. One important addition to this robot would be sensors in the rear in order to avoid obstacles when in reverse. In the next article, I'll make the follow a wall by taking advantage of the side sensor!

Next Article in Series: How to Build a Wall-Following Robot


  Download Code  



  • nmsr1196 2015-11-28

    Very nice project.  What type of sensor did you use? I could not see it clearly in the video. I did not see a link to the “Robot.h” library.

  • nmsr1196 2015-12-01

    Can you provide a download link for the “Robot.h” library?