Kenan Paralija
Published © CERN-OHL2

Robot with Sonar Obstacle Detection and Follow-Me

How to build an autonomous robot car that follows the object in front of it, but also can search and track lost objects again.

IntermediateProtip5 hours15
Robot with Sonar Obstacle Detection and Follow-Me

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Dual H-Bridge motor drivers L298
SparkFun Dual H-Bridge motor drivers L298
×1
DC motor (generic)
×2
Ultrasonic Sensor - HC-SR04
SparkFun Ultrasonic Sensor - HC-SR04
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Connector Accessory, Hex Nut
Connector Accessory, Hex Nut
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Multitool, Screwdriver
Multitool, Screwdriver

Story

Read more

Schematics

Schematic All connections of electronic modules

First of all we see the connection of the motor of the L298N board, I use the Arduino expansion board (Arduino Sensor Shield) pins 5, 6, 7, 8, 9, 11 on L298N board ENA, ENB, IN1-IN4 to control the car .

Code

FolowMeOnly

C/C++
Robot follows any object that comes close to it within a range of 20-50 cm, and if none is found, it will first try to find lost objects on the right and left sides with the ultrasonic module. But if again without results, then turn right and try to lock onto an object nearby.
// the robot car can automatically follow people and moving objects
// The program contains the part Keeping distance, from where  are called other subroutines

#include <Servo.h>  //servo library
Servo servo;      // create servo object to control servo

int Echo = A4;  
int Trig = A5; 

#define ENA 5
#define ENB 6
#define IN1 7
#define IN2 8
#define IN3 9
#define IN4 11
// carSpeed 250
int carSpd = 200;   // init speed
//********************followMe variablen
int distanceR = 0, distanceL = 0, distanceM = 0;
const int nomDistance=30, minDistance=20, maxDistance=50, kritDistance=10;
int distance;
//*********************

void setup() { 
  servo.attach(3,500,2400);  // attach servo on pin 3 - 500: 0 degree  2400: 180 degree
  Serial.begin(9600);     
  pinMode(Echo, INPUT);    
  pinMode(Trig, OUTPUT);  
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  stop();
  servo.write(90); //setservo START position             
  delay(500); 
} 

//+++++++++++++++++++++++++++++

void loop() { 
   distanceM = getDistance();
    // =getDistance(); //  getDistance() =Measuring obstacle distance 
    // bei existiert Objekt: keep Distanc 30 cm 
  if(distanceM >maxDistance)   followObjekt();
  
  else if(distanceM >nomDistance)  { forward(); //delay(300);  - >30 Command: forward(false,carSpd);
  }
  else if(distanceM <kritDistance) { back(); 
  // delay(200);     //                                         - <10
  }
  else if(distanceM <minDistance)  stop();    //                - <20
    // delays(10) with getBTData();
    // goto start
  }

//************************************************************************


void followObjekt(){ 
  // followObjekt Objekt left 115,  righ 65, wenn distance smaller as 50 turn on side; and wenn dont find, search left and right
  
    stop();
    servo.write(65);  //setservo position RIGHT according to scaled value
    delay(300);  // delays() with getBTData();
    distanceR = getDistance();
    // getDistance(); // distance_Test() -Measuring obstacle distance *****************************************
    
    if(distanceR <= maxDistance) {
    right();
      }
           
    else {
    servo.write(115); //setservo position LEFT
    delay(500);      
    distanceL = getDistance();
    if(distanceL <= maxDistance) left(); 
      }
        
    delay(200);
    servo.write(90);              
    delay(300); // delays() with getBTData();
    stop(); 
    distanceM = getDistance();
    if(distanceM > maxDistance) searchObjekt();  
    }


  void searchObjekt(){ 
    // wenn folowObjekt lost direktion
   // 1. search left 10 (wenn ok- korrektion Position, 
   // 2. search right 170 (wenn ok- korrektion Position
   // 3.wenn dont found - turn right until distance >50 and put it end
   // getDistance(); //  distance_Test() =Measuring obstacle distance *****************************************

   //1.
    servo.write(10);  //setservo position right
    delay(300); // delays() with getBTData();
    distance = getDistance();
    
    if(distance < maxDistance) {
    right(); //turn wenn OBJEKT existiert
      //delay(400);  
    }
    
    // 2.
    else {
   servo.write(170);  //setservo position left
    delay(300); // delays() with getBTData();
    distance = getDistance();
    if(distance < maxDistance)  
    left();
     // delay(400); //turn wenn OBJEKT =dont existiert
    }
     // 3.
     delay(400);
     stop(); 
    servo.write(90);
    delay(300);
    distance = getDistance();
    if(distance > maxDistance) {  
    do {
    distance = getDistance();
    right();
    delay(100); // delays() with getBTData();
      }
      while (distance > maxDistance);
      }
    //servo.write(90);          
    //delay(300);
    }
    
//+++++++++++++++++++++++++++++

void forward(){ 
  analogWrite(ENA, carSpd);
  analogWrite(ENB, carSpd);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  Serial.println("Forward");
}

void back() {
  analogWrite(ENA, carSpd);
  analogWrite(ENB, carSpd);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  Serial.println("Back");
}

void left() {
  analogWrite(ENA, carSpd);
  analogWrite(ENB, carSpd);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH); 
  Serial.println("Left");
}

void right() {
  analogWrite(ENA, carSpd);
  analogWrite(ENB, carSpd);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  Serial.println("Right");
}

void stop() {
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);
  Serial.println("Stop!");
} 

//Ultrasonic distance measurement Sub function
int getDistance() {
  digitalWrite(Trig, LOW);   
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);  
  delayMicroseconds(20);
  digitalWrite(Trig, LOW);   
  float Fdistance = pulseIn(Echo, HIGH);  
  Fdistance= Fdistance / 58;       
  return (int)Fdistance;
} 

Credits

Kenan Paralija

Kenan Paralija

3 projects • 2 followers
I started my career by designing analog and digital circuits and moved to telecommunication and finally web and IT technology.

Comments

Add projectSign up / Login