Obstacle Avoiding Robot Car Arduino Project

OBSTACLE AVOIDING CAR 




COMPONENTS REQUIRED:-

Arduino Uno

L293D Motor Driver Shield

4 DC Motors

1 Servo Motor

1 Ultrasonic Sensor (HC-SR04)


CONNECTIONS:-


COMPONENTS                     L293D MOTOR 

                                                DRIVER SHIELD 
Motor 1(Front Left)            M1

Motor 2(Rear Left)              M2

Motor 3(Front Right)          M3

Motor 4(Rear Right)            M4


Survo Motor.                         SURVO_ 2


Ultrasonic Sensor 

Trig                                         A0

Echo                                        A1

Power Supply                        5V+GND 


Required Libraries :

Library name:

Adafruit Motor Shield library (v1)

Header used in This code:

#include <AFMotor.h>


CODE FOR OBJECT AVOIDING CAR:-


#include <AFMotor.h>
#include <Servo.h>
// Create motor objects for L293D Shield
AF_DCMotor motor1(1); // M1
AF_DCMotor motor2(2); // M2
AF_DCMotor motor3(3); // M3
AF_DCMotor motor4(4); // M4
Servo myServo; // Servo motor for sensor
// Ultrasonic pins
#define trigPin A0
#define echoPin A1
long duration;
int distance;
int leftDistance, rightDistance, centerDistance;
void setup() {
  Serial.begin(9600);
  myServo.attach(9); // Servo pin on L293D Shield
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  // Stop all motors initially
  stopCar();
  delay(1000);
}
void loop() {
  // Scan center
  myServo.write(90);
  delay(300);
  centerDistance = getDistance();
  if (centerDistance > 20) {
    moveForward();
  } else {
    stopCar();
    delay(300);
    // Scan left
    myServo.write(150);
    delay(500);
    leftDistance = getDistance();
    // Scan right
    myServo.write(30);
    delay(500);
    rightDistance = getDistance();
    // Return servo to center
    myServo.write(90);
    delay(300);
    if (leftDistance > rightDistance) {
      turnLeft();
      delay(600);
    } else {
      turnRight();
      delay(600);
    }
  }
}
int getDistance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;
  return distance;
}
void moveForward() {
  motor1.setSpeed(180);
  motor2.setSpeed(180);
  motor3.setSpeed(180);
  motor4.setSpeed(180);
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}
void turnLeft() {
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}
void turnRight() {
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
}
void stopCar() {
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}


CIRCUIT DIAGRAM:-






MY YOUTUBE CHANNEL 👇 

https://youtube.com/@ayushrcgadgetcreator?si=ZUq5O0r_blbfxOpH


THANKYOU...

Comments

Popular posts from this blog

4 Best Science Projects For School Without Using Arduino

SENSOR TUTORIAL

AUTOMATIC TOLL GATE PROJECT

ARDUINO TUTORIAL

Drone Tech | How To Make Mini Drone For Science Exhibition It Can Fly || Remote Control Drone

RAIN ALARM PROJECT

BASIC SERVO MOTOR TEST

SMART IRRIGATION FOR FLYOVER

KIKIBOT Robo Car ESP32 Project