Building An Obstacle-Avoiding Robot

Introduction

 
This article will help those who are interested in learning the Internet of Things. It gives brief details of Arduino and Ultrasonic sensors which are used to detect a collision.
 
Requirements
  • Arduino Uno
  • Ultrasonic sensors
  • Servo motor
  • DC motors
  • Chaises board
  • Jumper wires
  • L293D motor driver
  • Battery
Steps to follow.
 
Step 1
 
First of all, connect the Arduino board with a laptop or computer.
 
IoT
 
Step 2
 
Install the Arduino software on the computer.
 
IoT       
 
Step 3
 
Open the Arduino software. The following screen will appear.
 
IoT
 
Step 4
 
You can see the Main Page of the software.
 
IoT  
 
Step 5
 
Then, open the new page on the software.
 
Then select the File→ New.
 
IoT
 
A new page opens on the Arduino software.
 
IoT
 
Step 6
 
I have given a source code that you can use or you can use your own source code.
 
Then, select File → open → select written program →open the program.
 
 
IoT
 
Open the program folder.
 
IoT
 
Select the program and open it.
 
IoT
 
Source code
  1. #include <Servo.h>   
  2. #include <NewPing.h>   
  3. const int LeftMotorForward = 7;  
  4. const int LeftMotorBackward = 6;   
  5. const int RightMotorForward = 4;   
  6. const int RightMotorBackward = 5;  
  7. #define trig_pin A1 //analog input 1  
  8. #define echo_pin A2 //analog input 2  
  9. #define maximum_distance 200  
  10.  boolean goesForward= false;  
  11.  int distance = 100;  
  12.   
  13.  NewPing sonar(trig_pin,echo_pin, maximum_distance); //sensor function  
  14.  Servo servo_motor;//our servo name  
  15.   
  16.  void setup(){  
  17.   Serial.begin(9600);  
  18.    pinMode(RightMotorForward,OUTPUT);   
  19.    pinMode(LeftMotorForward,OUTPUT);  
  20.    pinMode(LeftMotorBackward,OUTPUT);  
  21.    pinMode(RightMotorBackward,OUTPUT);  
  22.      
  23.    servo_motor. attach(10);//our servo pin  
  24.    servo_motor.write(115);  
  25.    delay(2000);   
  26.    distance =readPing();  
  27.    delay(100);  
  28.    distance = readPing();  
  29.    delay(100);  
  30.    distance =readPing();  
  31.    delay(100);  
  32.    distance = readPing();  
  33.    delay(100);  
  34.       
  35.  }  
  36.   void loop(){  
  37.     int distanceRight = 0;  
  38.     int distanceLeft = 0;  
  39.     delay(50);  
  40.   
  41.    if(distance<=40){  
  42.     //Serial.println(distance);  
  43.      moveStop();  
  44.      delay(400);  
  45.      moveBackward();  
  46.      delay(400);  
  47.      moveStop();  
  48.      delay(400);  
  49.      distanceRight = lookRight();  
  50.      delay(400);  
  51.      distanceLeft = lookLeft();  
  52.      delay(400);  
  53.   
  54.      if(distance >= distanceLeft){  
  55.      turnRight();  
  56.      moveStop();  
  57.      }  
  58.      else{  
  59.       turnLeft();  
  60.       moveStop();  
  61.      }  
  62.      }  
  63.      else{  
  64.       moveForward();  
  65.         }  
  66.       distance = readPing();  
  67.       }  
  68.   
  69.       int lookRight(){  
  70.     
  71.         servo_motor.write(50);  
  72.         delay(500);  
  73.         int distance=readPing();  
  74.         delay(100);  
  75.         servo_motor.write(115);  
  76.         return distance;  
  77.           
  78.       }  
  79.   
  80.       int lookLeft(){  
  81.         servo_motor.write(170);  
  82.         delay(500);  
  83.         int distance = readPing();  
  84.         delay(100);  
  85.         servo_motor.write(115);  
  86.         return distance;  
  87.         delay(100);  
  88.         }  
  89.         int readPing(){  
  90.           delay(70);  
  91.           int cm = sonar.ping_cm();  
  92.           if (cm==0){  
  93.             cm=250;  
  94.           }  
  95.           return cm;  
  96.         }  
  97.         void moveStop(){  
  98.   
  99.           digitalWrite(RightMotorForward,LOW);  
  100.           digitalWrite(LeftMotorForward,LOW);  
  101.           digitalWrite(RightMotorBackward,LOW);  
  102.           digitalWrite(LeftMotorBackward,LOW);  
  103.           }  
  104.     void moveForward(){  
  105.       if(!goesForward){  
  106.         goesForward=true;  
  107.   
  108.         digitalWrite(LeftMotorForward,HIGH);  
  109.         digitalWrite(RightMotorForward,HIGH);  
  110.         digitalWrite(LeftMotorBackward,LOW);  
  111.         digitalWrite(RightMotorBackward,LOW);  
  112.           
  113.       }  
  114.     }  
  115.     void moveBackward(){  
  116.       goesForward=false;  
  117.       digitalWrite(LeftMotorBackward,HIGH);  
  118.       digitalWrite(RightMotorBackward,HIGH);  
  119.       digitalWrite(LeftMotorForward,LOW);  
  120.       digitalWrite(RightMotorForward,LOW);  
  121.       }  
  122.       void turnRight(){  
  123.         digitalWrite(LeftMotorForward,HIGH);  
  124.         digitalWrite(RightMotorBackward,HIGH);  
  125.           
  126.         digitalWrite(LeftMotorBackward,LOW);  
  127.         digitalWrite(RightMotorForward,LOW);  
  128.           
  129.         delay(500);  
  130.           
  131.         digitalWrite(LeftMotorForward,HIGH);  
  132.         digitalWrite(RightMotorForward,HIGH);  
  133.           
  134.         digitalWrite(LeftMotorBackward,LOW);  
  135.         digitalWrite(RightMotorBackward,LOW);  
  136.           
  137.       }  
  138.  void turnLeft(){  
  139.     digitalWrite(LeftMotorBackward,HIGH);  
  140.     digitalWrite(RightMotorForward,HIGH);  
  141.       
  142.     digitalWrite(LeftMotorForward,LOW);  
  143.     digitalWrite(RightMotorBackward,LOW);  
  144.       
  145.     delay(500);  
  146.       
  147.     digitalWrite(LeftMotorForward,HIGH);  
  148.      digitalWrite(RightMotorForward,HIGH);  
  149.        
  150.       digitalWrite(LeftMotorBackward,LOW);  
  151.        digitalWrite(RightMotorBackward,LOW);    
  152.  }  
Step 9
 
The obstacle avoiding robot program coding will be there already. The value of distance for the ultrasonic sensors to detect the collision is set in the program as 30 cm.
 
IoT
 
Step 10
 
Click TOOL-  Select BOARD - then press ARDUINO/GENUINO UNO R3.
 
IoT
 
Again Select TOOL >> Select a PORT >> COM3 ARDUINO UNO
 
IoT
 
Step 11
 
Verify the program or compile the program.
 
IoT
 
The compilation is done.
 
Step 12
 
Connect the hardware as per the connection diagram given for the Obstacle avoiding robot.
 
IoT
 
Complete the connection as per the diagram.
 
Step 13
 
Then connect the Arduino to the laptop and upload the code in Arduino.
 
IoT
 
Step 14
 
Then the obstacle avoiding robot will start working.
 
IoT
 
The ultrasonic sensor detects the collision using Arduino.
 

Summary

 
Hence, we have created and executed an Arduino project which prevents collisions with an obstacle avoiding robot.
 
More exciting articles are coming up next.