Arduino Obstacle Avoiding Robot(Upgrade Version)

Hello friends ,Today I made a upgrade version of Arduino Obstacle Avoiding Robot.
This is simple but some feature and ultrasonic system are changed in code. This robot work fine with servo head.
You can also upgrade with Bluetooth and WiFi by using HC-05 and ESP8266 and many much you can change.This is simple but not more high level project for all Arduino Lover and this is very low cost project.
Simply we move to see parts list.







 Parts Needed

1X Arduino Uno http://amzn.to/2DgonpH
1X Ultrasonic Sensor http://amzn.to/2Dn9nFQ
1X Servo Motor http://amzn.to/2EL2Npy
2X BO Motor with Gear http://amzn.to/2DeT6Ue
Caster Wheel
Li-Po Battery http://amzn.to/2Dm7Orq
1X L293d i.c. http://amzn.to/2mIJ1EE
You can buy from this websites or you can buy from general Hobby Stores and Electronic Stores.

 The Chassis

Now mount the motors, arduino uno, Servo motor with Ultrasonic Sensor or only ultrasonic sensor(its optional) and the half pcb board.
The DC motors attach to the chassis.The Arduino attach to the chassis. The Servo motor attach to the chassis. The HC-SR04 holder attach to Servo .The HC-SR04 sensor attach to Servo.

 Circuit Diagram


Now all electronic in circuit diagram. I am not made in Breadboard just I simply use PCB .
But this not true you can't made in breadboard. Connect pins like that on your breadboard.

 The Code


#include <Servo.h>          //Servo motor library. This is standard library
#include <NewPing.h>        //Ultrasonic sensor function library. You must install this library

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

//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; //our servo name


void setup(){

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  
  servo_motor.attach(10); //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);
}

Click >>>> New Paste the code. Go To Tools >>>>Select the board>>>>Arduino Uno/Nano you choose. Port>>>>COM4 . Just Upload. This is very simple steps for all Arduino IDE User. If have no any software so go to this link Arduino.cc

 Know More About L293D

L293D Description. L293D is a typical Motor driver or Motor Driver IC which allows DC motor to drive on either direction. L293D is a 16-pin IC which can control a set of two DC motors simultaneously in any direction. ... Dual H-bridge Motor Driver integrated circuit (IC).

 Battery Pack

Simply I used a 8 volts ,2 amps powered Lead Acid Battery for this robot means I Combined 2, 4 volts battery first I was bought and charge for 3 Hour all of both batteries and then combined it. The battery can work 1 and half hour but now I am charging with 12 volts SMPS that can charge in 30 minutes . You can simply buy 11.1 Volts Li-PO battery for good result and also buy the charger.

 Join Us

Now join us in our facebook page https://www.facebook.com/True.Science.22and support us.
We are waiting for your feedback. Thank you very much.

Thanks For Supporting Us.
Save Tree. Save Life