Menu Close

Obstacle Avoiding Robot on The Arduino Robotic Kit

Robo India Presents obstacle avoiding robot using SR-04 Ultrasonic Range finder on The Arduino Robotic Kit.

Where to Buy.

Buy on Amazon Prime – click here

Buy on Robo India – click here

1. Introduction:

A step by step guide for to make line follower on The Arduino Robotic Kit.

You may need this tutorial to assemble the chassis. While you assemble chassis. Here is the basic robot assembly of The Arduino Robotic Kit.

1.2 Additional assembly guide to Obstacle Avoiding Robot.

The Arduino Robotic Kit comprises several parts. These parts come in sorted form in separate poly bags. Each poly bag is named. Following parts will be required to construct line follower on The Arduino Robotic Kit.

  1. Basic Assembled robot of The Arduino Robotic Kit
  2. Servo (With all accessories)
  3. Servo screw and nut set
  4. Ultrasonic sensor base moudule
  5. Ultrasonic sensor
  6. Screw driver

Follow the following guide to make line follower, please note that this tutorial is a sequel of basic robot assembly.

1.3 Ultrasonic Library:

Here is the library to use this sensor. You may download it from here.

Once you have downloaded this library. Extract this and copy the folder to My Documents/Arduino/Libraries or other similar locations.

2. Assembly and Connections

After assembling the robot make the connection as given in the following diagram. The Power Jumper on the motor shield works as Switch to motor it will be useful while debugging.

2.1 Testing motor Connection

It is difficult to find GND and positive supply terminal of motor. Transfer the following code to the Arduino Board. The robot should move in forward direction, if it not doing so, interchange the of the wire of motor. e.g. suppose left motor is rotating in back ward direction and right motor is rotating in forward direction then you have to interchange the wires of M3 terminal.

You may download this sketch from here.

//Robo India Line follower Tutorial
// This codes runs robo int the forward direction.

// Shield Pins Declaration 
int dataPin = 8;        
int latchPin = 12;
int clockPin = 4;
int en = 7;
void setup()
{
    pinMode(dataPin, OUTPUT);     // Setting up the motor shield. 
    pinMode(latchPin, OUTPUT);
    pinMode(clockPin, OUTPUT);
    pinMode(en, OUTPUT);    
    digitalWrite(en, LOW); 
 
 forward(); // This funtion for forward robot motion
 
  
 }

void loop()
{
}

void forward(void){
        digitalWrite(latchPin, LOW);            
        shiftOut(dataPin, clockPin, LSBFIRST, 3);
        digitalWrite(latchPin, HIGH);  

}

void backward(void){
        digitalWrite(latchPin, LOW);            
        shiftOut(dataPin, clockPin, LSBFIRST, 164);
        digitalWrite(latchPin, HIGH);  
}

void turn_left(void){
        digitalWrite(latchPin, LOW);            
        shiftOut(dataPin, clockPin, LSBFIRST, 161);
        digitalWrite(latchPin, HIGH);  
}

void turn_right(void){
        digitalWrite(latchPin, LOW);            
        shiftOut(dataPin, clockPin, LSBFIRST, 38);
        digitalWrite(latchPin, HIGH);  
}

void halt(void){
        digitalWrite(latchPin, LOW);            
        shiftOut(dataPin, clockPin, LSBFIRST, 32);
        digitalWrite(latchPin, HIGH);  
}


2.2 Programming Obstacle Avoiding Robot

Till the above step you have made all of the connection and your motors are connected to the correct terminals. The following programme is based upon following algorithm.

This is a very basic algorithm for obstacle avoiding robot. We kept this simple so that beginners can understand the concept. You may improve the programme by enhancing algorithm.

It has following predefined function for robot motion-

1. forward() : forward movement of robot.

2. backward() : backward movement of robot.

3. turn_left() : for turning left.

4. turn_right(): for turning right.

5. halt() : for stopping robot.

Download Ultrasonic Library

You may download this sketch from here.

// Tutorial Obstacle avoiding robot
// Prepared by Robo India
// www.roboindia.com

 
#include <Servo.h>  // Includes servo library.
#include <Ultrasonic.h> // Includes SR-04 Sensor Library.

Ultrasonic ultrasonic(A0,A1); // (Trig PIN,Echo PIN)
Servo servo_1;      // Creating Servo object. 

// declaring Motor Shield
int dataPin = 8;        
int latchPin = 12;
int clockPin = 4;
int en = 7;

// Variable to store distance
int left_d = 0;
int right_d = 0;
int front_d = 0;

int max_d = 50;  // Max distance to obastacle 

void setup() 
{ 
  
  // setting up shield. 
    pinMode(dataPin, OUTPUT);      
    pinMode(latchPin, OUTPUT);
    pinMode(clockPin, OUTPUT);
    pinMode(en, OUTPUT);    
    digitalWrite(en, LOW);   
     
    servo_1.attach(10);  // Attaching servo to Pin No.10
    servo_1.write(90);   // Initial position
    delay(350);
   
} 
 
 
void loop() 
{ 
   front_d = ultrasonic.Ranging(CM); // measuring fornt distance  
   if (front_d < max_d)
   {
     halt();
     get_d();     
     if(right_d > max_d)
     {
       turn_right();
       delay(400);
       forward();
     }
     else if ( left_d > max_d)
     {
       turn_left();
       delay(400);
       forward();     
     }     
     else  {           
       backward();
       delay (500);
       halt();      
      }
   }
   else{   
    forward();   
   } 
} 

void forward(void){  // function for forward movement. 
        digitalWrite(latchPin, LOW);            
        shiftOut(dataPin, clockPin, LSBFIRST, 3);
        digitalWrite(latchPin, HIGH);  

}

void backward(void){   // function for forward movement. 
        digitalWrite(latchPin, LOW);            
        shiftOut(dataPin, clockPin, LSBFIRST, 164);
        digitalWrite(latchPin, HIGH);  
}

void turn_left(void){   // function for left turn. 
        digitalWrite(latchPin, LOW);            
        shiftOut(dataPin, clockPin, LSBFIRST, 161);
        digitalWrite(latchPin, HIGH);  
}

void turn_right(void){   // function for Right turn. 
        digitalWrite(latchPin, LOW);            
        shiftOut(dataPin, clockPin, LSBFIRST, 38);
        digitalWrite(latchPin, HIGH);  
}

void halt(void){        // function for stopping robot. 
        digitalWrite(latchPin, LOW);             
        shiftOut(dataPin, clockPin, LSBFIRST, 32);
        digitalWrite(latchPin, HIGH);  
}

void get_d(void)  // Fuction to get distances.
{
    servo_1.write(0);   // Right Position 
    delay(500);
    right_d = ultrasonic.Ranging(CM);     
    servo_1.write(90);  // Front Positon 
    delay(500);
    front_d = ultrasonic.Ranging(CM);     
    servo_1.write(180); // Left position of servo
    delay(500);
    left_d = ultrasonic.Ranging(CM);  
    servo_1.write(90);  // back to front
    delay(250);
}

This is basic obstacle avoiding robot. Simple algorithm is taken here so that it can be understood easily. You may enhance the algorithm to improve performance of the robot.

3. Resources:

Where to Buy.

Buy on Amazon Prime – click here

Buy on Robo India – click here

Robo India’s SR-04 Ultrasonic Sensor Tutorial.

Robo India’s Arduino Servo control Tutorial

This motor shield is based on Adafruit Motor Shield. Resources can be found here.

If you have any query please write us at support@roboindia.com

Thanks and Regards
Content Development Team 
Robo India
https://roboindia.com

Leave a Reply