Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering

Efficient Wall Following Robot with Ultrasonic Sensor that Works in both Indoor and Outdoor Environments

By Hai Prasaath K

Like Line following robots are fun, making a wall following robot is even more exciting. A wall following robot is designed to move along a wall without hitting it. It has obstacle detection sensors mounted on the body which detects wall and drive DC motors attached to the wheels such that the robot keeps moving along the wall. 
The robot can be designed right oriented or left oriented or even can be designed to follow either sides. A right or left oriented wall follower can be designed easily with the help of just two sensors. Though more sensors can be used in making such a robot which will ultimately improve the path accuracy of the robot. For making a wall follower which can side either ways, at least three sensors are must to use and the program logic goes a little complex and sophisticated. If right oriented wall follower is designed, the obstacle detection sensors need to be mounted on front and right side of the robot. If Left oriented wall follower is designed, the obstacle detector sensors need to be mounted on front and left side of the robot. If the robot is designed to follow either sides, obstacle detector sensors need to be mounted on front, left and right side of the robot. In this project a left side wall following robot is designed.
Prototype of Wall Follower Robot
Fig. 1: Prototype of Wall Follower Robot
The obstacle detector sensor used in a wall follower can be either IR sensor or ultrasonic sensor. The IR sensors can be used to detect a predetermined and calibrated distance from the wall and so, on using IR sensors, the robot is designed to maintain a fixed distance from the wall. In such a case, a high path accuracy cannot be achieved. Secondly, in the presence of sun light or reflection from a black spot on the wall, the robot may not work as desired due to limitations of the IR sensor in such cases. While if ultrasonic sensor is used as obstacle detector, the robot can be designed to keep a range of distance with the wall which improves not only the flexibility of the path, but also improves its accuracy. Since ultrasonic sensors operate on the basis of reflection of ultrasonic (sound) waves, they can be relied in environments where there is sunlight or black obstacles in path. Considering these advantages of ultrasonic sensors over IR sensors, the ultrasonic sensors are used for obstacle or wall detection in this project.
The microcontroller board providing the intelligence to the robot is Arduino Pro Mini. The robot can also be designed on any other microcontroller board. The program code developed for this robot is also compatible with Arduino UNO and will work fine if the interfacing of sensors and motor driver IC is done the same way as the program operates. The Arduino Pro Mini is used due to its small size and light weight. Plus, the Pro Mini has sufficient GPIO pins just required for making this robot. The program code is written and burnt using Arduino IDE.
Components Required – 
List of Components required for Wall Follower Robot
Fig. 2: List of Components required for Wall Follower Robot
Block Diagram –
Block Diagram of Wall Follower Robot
Fig. 3: Block Diagram of Wall Follower Robot
 
Circuit Connections – 
This wall following robot is built around Arduino Pro Mini. The ultrasonic sensors and the L293D motor driver IC are interfaced to the controller board to make this a functional robot.
Image of Electronic Circuit Mounted on Wall Follower Robot
Fig. 4: Image of Electronic circuit mounted on Wall Follower Robot
The electronic circuit mounted on the robot and controlling its motion can be breakdown in the following sections –
Power Supply – In the circuit, Arduino Pro Mini and the ultrasonic sensors need a 5V regulated DC for their operation while the motor driver IC needs 12V DC. A 12V NIMH battery is used as the primary source of power. The supply from the battery is regulated to 5V and 12V using 7805 and 7812 ICs. The pin 1 of both the voltage regulator ICs is connected to the anode of the battery and pin 2 of both ICs is connected to ground. The respective voltage outputs are drawn from pin 3 of the respective voltage regulator ICs. An LED along with a 10K Ω pull-up resistor is also connected between common ground and output pin to get a visual hint of supply continuity. Despite using 12V battery, 7812 is used to provide a regulated and stable supply to the motor driver IC.
Arduino Pro Mini – Arduino Pro Mini is a Atmega 168 based microcontroller board. It has 14 GPIO pins, 6 analog inputs, 6 PWM pins, 2 external interrupts and onboard UART, SPI and I2C. The board is just 1.3 inch by 0.7 inch in size that is six times smaller than Arduino UNO. With such features packed in small size, this board is most ideal for any robotic project. In this project, 8 input output pins of Pro Mini are utilized, four pins for interfacing with ultrasonic sensors and four pins for interfacing with motor driver IC.
HC-SR04 Ultrasonic Sensor – The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats or dolphins do. It offers excellent non-contact range detection with high accuracy and stable readings in range from 2 cm to 400 cm.
There are two ultrasonic sensors used in the circuit, one is mounted on front of the robot and other is mounted on left side of the robot. The ultrasonic sensor mounted on front is connected to pins 5 and 6 of the Arduino board and sensor mounted on left side is connected to pins 10 and 11 of the Arduino board. The ultrasonic sensor has four pins – Ground (Pin 1), Echo (Pin 2), Trigger (Pin 3) and Trigger. The VCC and ground pins are connected to common VCC and Ground respectively. The Echo pins of front and left sensor are connected to pins 5 and 11 of the Arduino board respectively while Trigger pins of front and left sensors are connected to pins 6 and 10 of the Arduino board.
The ultrasonic sensor works on the principle of echo of sound waves. When a HIGH pulse of 10 u sec is passed to the trigger pin of the sensor, it  transmits eight 40 KHz waves of HIGH Sonic Pulse shots back to back. A High pulse signal is out from the echo pin as the ultrasonic wave is transmitted. This wave when collides with an obstacle, it is reflected back and detected by the sensor. On detecting the wave again, the High pulse signal from the echo pin of the sensor is terminated. The signal received from the echo pin is analog in nature. The distance from the obstacle can be measured by measuring the high time of the echo pin. This is the time between the transmission and reflection back of the sonic wave. The distance is given by the formulae –
Test distance = (high level time × velocity of sound (340M/S)) / 2
The time multiplied by velocity is divided by 2 as the time taken is for sonic wave to reach obstacle and return back. Therefore the distance measurement in cm can be given by the formulae –
Test distance = (high level time × velocity of sound (340M/S)) / 2
                         = (high level time(microsecond) × velocity of sound (340M/S)) / 2
                         = high level time x 340/2000000 m
 = high level time x 34000/2000000 cm
 = high level time x 34/2000 cm
The ultrasonic sensors output the high pulse from their pin 2 which is detected at the pins 5 and 11 of the Arduino Board. The program code measures the pulse durations and digitize them to distance values using the formulae stated above. These distance values are utilized to maintain a set distance with the left side wall and divert from an obstacle in front of the robot from a preset distance. The motors are driven to  maintain pre-determined distance from the left side wall.
L293D DC Motor Driver IC –    The L293D is a dual H-bridge motor driver integrated circuit (IC). The Motor drivers act as current amplifiers since they take a low-current control signal and provide a higher-current signal. This higher current signal is used to drive the motors. It has 16 pins with following pin configuration:
Table Listing Pin Configuration of L293D Motor Driver IC
Fig. 5: Table listing pin configuration of L293D Motor Driver IC
There are two DC motors used for making the robotic car. The DC motors are interfaced between pins 3 and 6 and pins 14 and 11 of the motor driver IC.
The L293D IC controls the DC Motors according to the following truth tables:
Truth Table of L293D Motor Driver IC
Fig. 6: Truth Table of L293D Motor Driver IC
Truth Table of L293D Motor Driver IC
Fig. 7: Truth Table of L293D Motor Driver IC
The pin 4, 5, 13 and 12 of the L293D are grounded while pin 1, 16 and 9 are connected to 5V DC and pin 8 is connected to 12V DC. The pins 15, 2, 7 and 10 of the motor driver IC are connected to pins 8, 2, 3 and 7 of the Arduino board. The DC motor attached to right wheel is connected to pins 11 and 14 while motor attached to left wheel is connected to pins 3 and 6 of the Arduino.
Geared DC Motors – In this robot, 12V geared DC motors are attached to the wheels. Geared DC motors are available with wide range of RPM and Torque, which allow a robot to move based on the control signal it receives from the motor driver IC.
How the circuit works – 
When the robot is powered on, it is initialized to move forward and keep turning left until it reaches a minimum distance with the left wall. For this, the robot is made to start motion in forward direction and start reading values from the ultrasonic sensors. The robot also keeps turning left by rotating right side DC motor more speedily until the left sensor reading approaches minimum value. Now onwards, the robot can face two conditions – either some obstacle appears in front of the robot or the distance with the wall may reduce due to the structure or layout of the wall. If an obstacle is detected in front of the robot at a preset distance, the robot will be turned right until it overcomes the obstacle. If there is no obstacle in front of the robot, the robot will continue forward motion. In case, the distance between the left wall and robot is reduced below minimum value, the robot will be made to move again in right direction by driving left side motor more speedily until the distance reaches a maximum value. The Arduino implements the same algorithm. This algorithm is summarized in the following flow chart –
Flowchart of Arduino Code used for Wall Following Robot
 Fig. 8: Flowchart of Arduino Code used for Wall Following Robot
The Arduino sketch moves the robot according to the following table –
Logic Table of Wall Following Robot
Fig. 9: Logic Table of Wall Following Robot
The follow up actions can be implemented by driving the motors in following fashion –
Table Summarizing Action Plan for Wall Following Robot
Fig. 10: Table summarizing Action Plan for Wall Following Robot
  The robot can be moved forward, backward, left or right by implementing the following input logic at the motor driver pins –
Logic Table of L293D Motor Driver IC for Wall Follower Robot
Fig. 11: Logic Table of L293D Motor Driver IC for Wall Follower Robot
For slowing or speeding specific robotic movement, PWM can be applied at the input pins of the L293D driver. So the ultrasonic sensors sense the distance from wall or obstacle and the DC motors are driven to respond the changed situation. This is how the robot moves following wall and overcoming any obstacles.
In a right oriented wall follower, the algorithm will be almost similar except that the robot will be designed to follow right wall and turn according in different situations. The design of a robot following either wall could be a bit complex as it will face new situations and will have to decide that which wall should be followed under certain circumstances.
Image of Arduino based Wall Follower Robot
Fig. 12: Image of Arduino based Wall Follower Robot
Check out the program code to see how the algorithm is implemented in the Arduino sketch. Check out how Arduino reads data from the ultrasonic sensors, compare readings with minimum and maximum offsets and change motor rotation in response to perceived situations.
Programming Guide – 
The code uses the new ping library for the ultrasonic sensor. Learn more about the ping library from Arduino.CC and download NewPing.h.
#include <NewPing.h>
The #define directive is used to declare some constant values which will be assigned to the variables used in the code.  These constants denote how many ultra sonic sensors are used, the maximum distance sensor should respond and Ping interval is that time between the pings of two sensors in milliseconds.
#define SONAR_NUM     2
#define MAX_DISTANCE 500
#define PING_INTERVAL 33
The following #define directives are used to determine the arduino pin numbers used to connect the ultrasonic sensor (trigger pin and echo pin), and the pins which should be connected to the motor driver IC.
#define trigPin1 5
#define echoPin1 6
#define trigPin2 10
#define echoPin2 11
#define LM1 2
#define LM2 3
#define RM1 7
#define RM2 8
The following block describes the sensor object that define the two sensors in array. It instantiates NewPing library object which describes the trigger pin and echo pins connected to the arduino and maximum distance they should measure.
  NewPing sonar[SONAR_NUM] = {
  NewPing(trigPin1, echoPin1, MAX_DISTANCE),
  NewPing(trigPin2, echoPin2, MAX_DISTANCE)
};
The following block describes the setup() function, which is used to set the baud rates and defines whether arduino pin should be used as digital input or output. It is used to set the baud rate for the serial communication between the arduino and the pc.
Serial.begin(9600);
int i;
The following statements use PinMode function to declare pins connected to the motor driver from the arduino as digital output.
pinMode(LM1, OUTPUT);
pinMode(LM2, OUTPUT);
pinMode(RM1, OUTPUT);
pinMode(RM2, OUTPUT);
The following statements use PinMode function to declare trigger pins as output and echo pins as input in respect the Arduino board.
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
digitalWrite(trigPin1, LOW);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
digitalWrite(trigPin2, LOW);
The following block of code initializes the range of values which arduino should signal the motor driver to take a right turn or left turn or drive forward depending on the distance measured by the ultrasonic sensor.
int toCloseWall = 1000;
int toFarWall = 1500;
int toCloseFront = 1000;
The main operation in the code is implemented by a decision making loop which is called in the main() function. It should be noted that in the source code, serial.print() functions are used to check range of ultrasonic sensors while debugging code and calibrating them. The main() function calls the readRangeFront() function in order to measure the distance detected by the front ultrasonic sensor.
  rangeFront = readRangeFront();
It calls the readRangeWall() function in order to measure the distance between the wall the robot is moving along.
  rangeWall = readRangeWall();
The following if condition is used to check the distance between the front sensor and the object, if the object is closer according to pre-determined range, it calls the drive_backward(), drive_forward() and the turn_left function with some delay in-between.
if (rangeFront < toCloseFront)
 {
    delay(500);
    drive_backward();
    delay(500);
    delay(800);
    drive_forward();
    turn_left();
    delay(1000);
    goto Main;
  }
The following if condition is used to check the distance between the front sensor and the front wall and side wall sensor and the side wall. If the distance measured by the two sensors are at desired range it calls drive_forward() function.
if(rangeWall > toCloseWall && rangeWall < toFarWall)
  {
    drive_forward();
    goto Main;
  }
The following if conditions are used to check the distance between the side wall and the sensor and check for closer and far value. If the distance becomes close, it calls the function drive_left() and drive_forward() function and if the distance becomes far it calls the function drive_right() and drive_forward() function.
  if (rangeWall < toCloseWall)
  {
    delay(100);
    turn_left();
    delay(100);
    drive_forward();
    goto Main;
  }
  if (rangeWall > toFarWall)
  {
    delay(100);
    turn_right();
    delay(100);
    drive_forward();
    goto Main;
  }
Goto statements are used to run the loop continuously. Check out the complete project code and burn to an Arduino for making the robot run. This completes the Arduino sketch for the Wall Following (Left Side) Robot.

Project Source Code

###

//Program to 

#include

 

#define SONAR_NUM     2             //define Number of ultrasonic sensor used

#define MAX_DISTANCE 800            //Max distance between the object and robot

#define PING_INTERVAL 33           

 

#define trigPin1                 6                  // define pin connections for sensor and motor

#define echoPin1            5

#define trigPin2                11

#define echoPin2            10

#define LM1      2      

#define LM2      3     

#define RM1      7 

#define RM2      8    

 

int rangeFront = 0;

int rangeWall  = 0;

 

NewPing sonar[SONAR_NUM] = {                                // Define a Newping array to measure the distance

  NewPing(trigPin1, echoPin1, MAX_DISTANCE),

  NewPing(trigPin2, echoPin2, MAX_DISTANCE)

};

void setup()               // setup function to make pin configuration to define whether it is input or output

{

  Serial.begin(9600);

  int i;

  pinMode(LM1, OUTPUT);

  pinMode(LM2, OUTPUT);

  pinMode(RM1, OUTPUT);

  pinMode(RM2, OUTPUT);

  pinMode(en1, OUTPUT);

  pinMode(en2, OUTPUT);

 

  pinMode(trigPin1, OUTPUT);

  pinMode(echoPin1, INPUT);

  digitalWrite(trigPin1, LOW);

 

  pinMode(trigPin2, OUTPUT);

  pinMode(echoPin2, INPUT);

  digitalWrite(trigPin2, LOW);

}

 

int toCloseWall = 1800;           //Initialze and define the values for distance between wall and robot

int toFarWall = 2500;

int toCloseFront = 1000;

 

void loop()

{

Main:

  rangeFront = readRangeFront();  // Read the sensor value by the function call

  Serial.print(rangeFront);          

  Serial.print(" Front");

  Serial.println();

  rangeWall = readRangeWall();

  Serial.print(rangeWall);

  Serial.print(" Wall");

  Serial.println(); 

 

  if (rangeFront <= 400)         //As the sensor value reads the small values we are manipulating to high values

  {

    rangeFront = 3000;

  }

  if(rangeWall <= 400)

  {

    rangeWall = 3000;

  }

 

 if (rangeFront < toCloseFront)  //Condition to check whether front sensor is close to robot

 {

    delay(500);

    drive_backward();

    delay(500);

    Serial.print(" Drive Back");

    //turn_right();

    Serial.print(" Right Turn");

    Serial.println();

    delay(800);

    drive_forward();

    turn_left();

    delay(1700);

    goto Main;   

  } 

 

  if(rangeWall > toCloseWall && rangeWall < toFarWall) //condition to check distance measured by front and side sensor is maintained correctly

  {

    drive_forward();

    Serial.print(" Drive Forward");

    Serial.println();

    goto Main;

  } 

 

  if (rangeWall < toCloseWall)                       //condition to check side wall is close to robot

  {

    delay(100);

    //turn_left();

    turn_right();

    delay(500);

    Serial.print(" Turn Left");

    drive_forward();

    Serial.print(" Drive Forward");

    Serial.println();

    goto Main;

  }

  if (rangeWall > toFarWall)                         //condition to check robot is far from side wall

  {

    delay(100);

    //turn_right();

    turn_left();

    Serial.print(" Turn Right");

    delay(500);

    drive_forward();

    Serial.print(" Drive Forward");

    Serial.println();

    goto Main;

  } 

}

 

void motor_stop()                                  // function to stop the robot

{   

    digitalWrite(LM1, LOW);

    digitalWrite(LM2, LOW);

    digitalWrite(RM1, LOW);

    digitalWrite(RM2, LOW);

}

 

void drive_forward()                               // function to drive the robot forward

{   

    digitalWrite(LM1, HIGH);

    digitalWrite(LM2, LOW);

    digitalWrite(RM1, HIGH);

    digitalWrite(RM2, LOW);

}

 

void drive_backward()                             // function to drive the robot backward

{   

    digitalWrite(LM1, LOW);

    digitalWrite(LM2, HIGH);

    digitalWrite(RM1, LOW);

    digitalWrite(RM2, HIGH);

}

 

void turn_left()                                  // function to turn the robot left

{  

    digitalWrite(LM1, HIGH);

    digitalWrite(LM2, LOW);

    digitalWrite(RM1, LOW);

    digitalWrite(RM2, LOW);

}

 

void turn_right()                                     //function to turn the robot right

{

    digitalWrite(LM1, LOW);

    digitalWrite(LM2, LOW);

    digitalWrite(RM1, HIGH);

    digitalWrite(RM2, LOW);

}

 

int readRangeFront()                                 //function to read the front sensor value

{

  delay(50);

  unsigned rangeFront = sonar[0].ping();

  sonar[0].timer_stop();

  return rangeFront;

}

int readRangeWall()                                 // function to read the left sensor value

{

  delay(50);

  unsigned rangeWall = sonar[1].ping();

  sonar[1].timer_stop();

  return rangeWall;

}


###

 


Circuit Diagrams

Circuit-Diagram-Arduino-Wall-Follower-Robot

Project Video


Filed Under: Electronic Projects

 

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • Designing Gate Driver Circuit and Switching Mechanism for Modified Sine Wave Inverter – (Part 9/17)
  • Completing Modified Sine Wave Inverter Design with Full Bridge Circuit and Step Up Transformer – (Part 10/17)
  • Designing an Offline UPS – Part (12 /17)
  • How to reduce Switching Time of a Relay – (Part 15/17)
  • Testing MOSFET – (Part 16/17)
  • Driving High Side MOSFET using Bootstrap Circuitry – (Part 17/17)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • Designing Gate Driver Circuit and Switching Mechanism for Modified Sine Wave Inverter – (Part 9/17)
  • Completing Modified Sine Wave Inverter Design with Full Bridge Circuit and Step Up Transformer – (Part 10/17)
  • Designing an Offline UPS – Part (12 /17)
  • How to reduce Switching Time of a Relay – (Part 15/17)
  • Testing MOSFET – (Part 16/17)

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd ldr led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics Research robot samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • MAX5389 resetting by noise
  • Verilog/SV: Using an array as a set of individual registers and not RAM
  • the mysterious emitter follower
  • Diagnosing a fault on a 18volt lithium Ion charger
  • electrode-skin impedance mismatch

RSS Electro-Tech-Online.com Discussions

  • Relaxation oscillator with neon or...
  • LCD display on PICDEM 2 Plus board
  • software PWM
  • Positive and negative sides of voltage source
  • Cordless vacuum cleaner problem
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering