Project showcase
Obstacle Avoiding Car

Obstacle Avoiding Car © GPL3+

An Arduino controlled car that avoids obstacles (ie. walls or objects in front of it).

  • 18,676 views
  • 3 comments
  • 32 respects

Components and supplies

Necessary tools and machines

Hy gluegun
Hot glue gun (generic)
super glue
tiny screwdriver

Apps and online services

About this project

Code

Arduino Obstacle avoidance Car codeArduino
Use it to Control the components on the car.
You need the New Ping Library for the ultrasonic sensor.
The maximum distance between the car and an object is set in the code and the reaction of the car too. The reaction of the car to an obstacle is : go back, look right, look left and go where it is clear. If both sides are clear, it will go forward.
#include <Servo.h>          //standard library for the servo
#include <NewPing.h>        //for the Ultrasonic sensor function library.

//L298N motor control pins
const int LeftMotorForward = 6;
const int LeftMotorBackward = 7;
const int RightMotorForward = 5;
const int RightMotorBackward = 4;

//sensor pins
#define trig_pin A1 //analog input 1
#define echo_pin A2 //analog input 2

#define maximum_distance 200
boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; 

void setup(){

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  
  servo_motor.attach(9); //our servo pin

  servo_motor.write(115);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop(){

  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);

  if (distance <= 20){
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);

    if (distance >= distanceLeft){
      turnRight();
      moveStop();
    }
    else{
      turnLeft();
      moveStop();
    }
  }
  else{
    moveForward(); 
  }
    distance = readPing();
}

int lookRight(){  
  servo_motor.write(50);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
}

int lookLeft(){
  servo_motor.write(170);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
  delay(100);
}

int readPing(){
  delay(70);
  int cm = sonar.ping_cm();
  if (cm==0){
    cm=250;
  }
  return cm;
}

void moveStop(){
  
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
}

void moveForward(){

  if(!goesForward){

    goesForward=true;
    
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
  
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW); 
  }
}

void moveBackward(){

  goesForward=false;

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
  
}

void turnRight(){

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorForward, LOW);
  
  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
 
  
  
}

void turnLeft(){

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
}

Schematics

Schematics
Usually people use two power sources, one for the Arduino and another one for the L298N Motor Driver. That's not necessary as the L298n has a 5v regulator that can power the Arduino, so i took advantage of that.
Arduino fritzing bb rxwg3vawsc

Comments

Similar projects you might like

Project 1: 2WD Obstacle Avoiding Robot

Project showcase by HDA Robotics

  • 7,535 views
  • 6 comments
  • 17 respects

Obstacle Avoiding Robot Car

Project showcase by Aritra_B

  • 2,693 views
  • 3 comments
  • 12 respects

Robotic Car controlledover Bluetooth with Obstacle Avoidance

Project showcase by S.Ranjith Reddy

  • 9,583 views
  • 5 comments
  • 17 respects

Ground-Based Obstacle Avoiding Robot

Project showcase by Xavier Tan

  • 5,312 views
  • 1 comment
  • 20 respects

Autonomous obstacle avoiding car

Project tutorial by Embrio

  • 8,787 views
  • 6 comments
  • 31 respects

Arduino 4WD RC Car

Project tutorial by Andriy Baranov

  • 13,135 views
  • 3 comments
  • 43 respects
Add projectSign up / Login