How to Build a Wall-Following Robot

December 01, 2015 by Travis Fagerness

This is the final part in a series of articles on building a robot that can follow lines or walls and avoid obstacles!

Make your robot follow a wall!

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 5: Avoiding Obstacles


This is part 6 of a series of articles on my experiences building a robot that can do various things. 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 follow walls.

Following Walls

In order to follow walls, you need at least two sensors (2 bits of information) to handle the four potential situations the robot could be in. One sensor has to be in the front, and the second could be on the left or right of the robot. The more sensors you use, the more information you have, so you can make better judgements about what is going on. For this example, I just used two. The robot cannot find the wall, so you have to place the robot next to the wall. If you placed it in the middle of the room, it would just drive in circles.

Truth Table

Front Sensor Right Sensor Situation Action
Off Off Robot is driving away from wall. Come back to wall, turn right.
On Off Robot is away from wall but headed towards a wall or obstacle. Turn hard left to get back parallel with the wall.
Off On Robot is following the wall. Drive forward.
On On The robot is at a corner. Turn hard left.

In order to work, I had to add code to turn hard left. Hard left just means I only turn on the right wheel so the robot basically turns in place rather than continue to move forward while turning. I couldn't just turn slowly like the line follower because you have no idea how close the robot is to the wall. This is a limitation of the sensor I chose, because the sensor reflects differently based on the surface. Additionally, the logic is set up to only be binary because there is no way to tell the distance based on the sensor. If you knew distance, you could add additional logic to vary the speed based on the distance to make the robot travel around the room faster. I actually couldn't get the sensor to reflect at all off a black surface, so in the video you'll see I had to put a white surface in front of the dishwasher. An audio based sensor would not have this problem.


The following sketch performs the logic:

  • Start the robot driver and wait 5 seconds. This gives you time to put the robot next to the wall before it starts moving.
  • Read the sensor
  • Implement the truth table above using if statements.
  • Each statement executes the action associated with the sensor configuration.
  • I had the robot turn right faster than normal so that the robot can turn around corners more sharply. Most wall corners are 90 degrees so turning faster makes sense.

#include "robot.h"

void setup()

uint16_t lleft,lmid,lright;
boolean wleft,wmid,wright;
uint16_t avoid_count=0;
void loop()                     
  /*if the wall is sensed, go forward
  * the wall is sensed if the right sensor is on but the mid
  * sensor is off.
  if(wright && !wmid)
  /*likely going towards the wall
  * not sure how close so turn as fast
  * as we can
  if(wright && wmid)
  /*going away from the wall
  * slowly turn back towards the wall
  if(!wright && !wmid)
  /*likely at a corner or coming in at an angle to the wall*/
  if(!wright && wmid)

Changes required for hard left to robot.h and robot.


Add HARD_LEFT to the enum for direction.

/*robot interface*/
typedef enum{


Add a case for HARD_LEFT in rbt_move().


Following the Walls in my Kitchen:


In this article, I showed how you might use the proximity sensors to follow walls in order to navigate around a room. This concludes the series of articles on making a robot! The robot is able to autonomously follow a line, a wall and avoid obstacles. Can you combine them into a single robot that does it all? You can also take control by adding a BLE module and control the robot from your phone!

  • zaintech January 15, 2016

    Nice…Good Hard Work.

    Like. Reply
  • S
    SAMYAK June 04, 2018

    How can i learn more about your code as i am trying to build similar robot

    Like. Reply
    • RK37 June 05, 2018
      We're no longer in contact with the author of this article, so if you need some help with your robot project, I recommend that you post your question in the AAC forum.
      Like. Reply