Skip to main content

Obstacle Avoiding Robot Car Using Arduino - How To Make a Smart Car


How To Make Smart Robot Car With Arduino

Obstacle avoiding robot car using Arduino, how to make a smart car with ultrasonic sensor, servo motor, and L298N motor driver module.

Arduino UNO :- https://amzn.to/3MF9jky

SG90 Servo Motor And HC-SR04 Ultrasonic Sensor :- https://amzn.to/3EX03pu

LM298N motor driver module :- https://amzn.to/3y2tx3Y

BO Motor with Wheel Pair :- https://amzn.to/38zFMu6

arduino smart car kit :- https://amzn.to/3kmtZlk


Project 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);
}


Comments

Popular posts from this blog

Clap On Off Switch Circuit Using Arduino

  Arduino Clap LED program #define MicAO 8 int ledPin = 13; int clap = 0; long detection_range_start = 0; long detection_range = 0; boolean status_lights = false; void setup() {   pinMode(MicAO, INPUT);   pinMode(ledPin,OUTPUT);   } void loop() { int status_MicAO = digitalRead(MicAO); if (status_MicAO == 0) { if (clap == 0) { detection_range_start = detection_range = millis(); clap++; } else if (clap > 0 && millis()-detection_range >= 50) { detection_range = millis(); clap++; } } if (millis()-detection_range_start >= 400) { if (clap == 2) { if (!status_lights) { status_lights = true; digitalWrite(ledPin, HIGH); } else if (status_lights) { status_lights = false; digitalWrite(ledPin, LOW); } } clap = 0; } }

Bluetooth Robot Car Using Arduino Project Step By Step

Bluetooth Controlled Robot Car Using Arduino Bluetooth Controlled Robot Car Using Arduino Project using LM298n motor deriver and hc-05 bluetooth module step by step tutorial. Code Download :- Click Here Arduino bluetooth controller app Arduino bluetooth controller App

IOT Based Water Level Indicator Project Using Esp32 And Ultrasonic Sensor

  Water Level Indicator Project Using ESP32   Water Level Indicator with Alarm on Tank Full.   IoT Wi-Fi smart water level indicator for tanks with mobile app control . 📟 OLED Display (0.96" I2C SSD1306)   OLED Pin         ESP32 Pin VCC                     3.3V GND                     GND SDA                    GPIO 21 SCL                      GPIO 22     🔔 BUZZER   Buzzer Pin        ESP32 Pin   +           GPIO 26 -            GND         Each LED connects to an ESP32 GPIO pin via a current-limiting resistor.   ...