Ad Code

Responsive Advertisement

Ticker

6/recent/ticker-posts

How To Make DIY Autonomous Obstacle Avoiding Robot Car using Arduino Nano & Ultrasonic Sensor by WTC Zone

How To Make DIY Autonomous Obstacle Avoiding Robot Car using Arduino Nano & Ultrasonic Sensor by WTC Zone

Here’s a comprehensive guide for your Obstacle Avoider Car project using an ultrasonic sensor, servo motor, L298N motor driver, Arduino Nano, and other components.


Project Overview

The Obstacle Avoider Car moves forward by default and uses the ultrasonic sensor mounted on a servo motor to scan for obstacles. If it detects an obstacle, the car evaluates the surroundings (left and right) and moves in the direction with more space.


Parts Required

  1. Arduino Nano x 1
  2. HC-SR04 Ultrasonic Sensor x 1
  3. Servo Motor x 1 (for ultrasonic sensor scanning)
  4. L298N Motor Driver Module x 1
  5. DC Motors x 2 (connected to wheels)
  6. Robot Chassis x 1 (with wheels and space for components)
  7. Battery Pack (7.4V Li-ion or 9V)
  8. Connecting Wires
  9. Male-to-Male and Male-to-Female Jumper Wires
  10. Breadboard (optional, for prototyping)

Circuit Connections

1. Ultrasonic Sensor (HC-SR04)

  • VCC → Arduino Nano 5V
  • GND → Arduino Nano GND
  • Trigger (Trig) → Arduino Nano D9
  • Echo → Arduino Nano D8

2. Servo Motor

  • Signal → Arduino Nano D10
  • VCC → Arduino Nano 5V (or external 5V supply)
  • GND → Arduino Nano GND

3. L298N Motor Driver

  • IN1 → Arduino Nano D2
  • IN2 → Arduino Nano D3
  • IN3 → Arduino Nano D4
  • IN4 → Arduino Nano D5
  • ENA → Connect to 5V (via jumper or PWM pin for speed control)
  • ENB → Connect to 5V (via jumper or PWM pin for speed control)
  • Motor A (Left Motor) → L298N OUT1, OUT2
  • Motor B (Right Motor) → L298N OUT3, OUT4
  • Power Input → Battery Pack VCC and GND

4. Power Supply

  • Connect the battery pack to the L298N module's power input and ensure common GND between the Arduino and L298N.

Circuit Diagram

Video Link: - Click Here For Video

How To Make DIY Autonomous Obstacle Avoiding Robot Car using Arduino Nano & Ultrasonic Sensor by WTC Zone simple Circuit: -


How To Make DIY Autonomous Obstacle Avoiding Robot Car using Arduino Nano & Ultrasonic Sensor by WTC Zone Arduino Programming: -

Arduino IDE Program

Code for DIY Autonomous Obstacle Avoiding Robot Car using Arduino Nano & Ultrasonic Sensor by WTC Zone

#include <Servo.h>

// Ultrasonic sensor pins
const int trigPin = 9;
const int echoPin = 8;

// Motor driver pins
const int motorLeftForward = 2;
const int motorLeftBackward = 3;
const int motorRightForward = 4;
const int motorRightBackward = 5;

// Servo motor pin
Servo servo;
const int servoPin = 10;

// Variables
long duration;
int distance;
int leftDistance, rightDistance;

// Function to measure distance
int measureDistance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  return duration * 0.034 / 2; // Convert to cm
}

// Motor control functions
void moveForward() {
  digitalWrite(motorLeftForward, HIGH);
  digitalWrite(motorLeftBackward, LOW);
  digitalWrite(motorRightForward, HIGH);
  digitalWrite(motorRightBackward, LOW);
}

void moveBackward() {
  digitalWrite(motorLeftForward, LOW);
  digitalWrite(motorLeftBackward, HIGH);
  digitalWrite(motorRightForward, LOW);
  digitalWrite(motorRightBackward, HIGH);
}

void turnLeft() {
  digitalWrite(motorLeftForward, LOW);
  digitalWrite(motorLeftBackward, HIGH);
  digitalWrite(motorRightForward, HIGH);
  digitalWrite(motorRightBackward, LOW);
}

void turnRight() {
  digitalWrite(motorLeftForward, HIGH);
  digitalWrite(motorLeftBackward, LOW);
  digitalWrite(motorRightForward, LOW);
  digitalWrite(motorRightBackward, HIGH);
}

void stopMotors() {
  digitalWrite(motorLeftForward, LOW);
  digitalWrite(motorLeftBackward, LOW);
  digitalWrite(motorRightForward, LOW);
  digitalWrite(motorRightBackward, LOW);
}

void setup() {
  // Motor pins
  pinMode(motorLeftForward, OUTPUT);
  pinMode(motorLeftBackward, OUTPUT);
  pinMode(motorRightForward, OUTPUT);
  pinMode(motorRightBackward, OUTPUT);

  // Ultrasonic sensor pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  // Attach servo
  servo.attach(servoPin);
  servo.write(90); // Center position

  Serial.begin(9600);
}

void loop() {
  // Measure front distance
  servo.write(90); // Center
  delay(500);
  distance = measureDistance();
  Serial.print("Front Distance: ");
  Serial.println(distance);

  if (distance < 20) {
    // Stop and scan left/right
    stopMotors();
    delay(500);

    servo.write(0); // Look left
    delay(500);
    leftDistance = measureDistance();
    Serial.print("Left Distance: ");
    Serial.println(leftDistance);

    servo.write(180); // Look right
    delay(500);
    rightDistance = measureDistance();
    Serial.print("Right Distance: ");
    Serial.println(rightDistance);

    // Decide direction
    if (leftDistance > rightDistance) {
      turnLeft();
      delay(500);
    } else {
      turnRight();
      delay(500);
    }
  } else {
    moveForward();
  }
  delay(100);
}



-----------------------------------------------------------------------------------------------------------------------------

How It Works

  1. Ultrasonic Sensor:

    • The HC-SR04 measures the distance to obstacles.
    • If an obstacle is closer than 20 cm, the car stops and scans left and right.
  2. Servo Motor:

    • Rotates the ultrasonic sensor to scan the environment.
    • Checks distances to the left and right to decide the optimal direction.
  3. Motor Driver:

    • Controls the two DC motors to move the car forward, backward, or turn left/right based on decisions made by the Arduino.
  4. Obstacle Avoidance:

    • The car moves forward by default.
    • On detecting an obstacle, it evaluates the surroundings and turns in the direction with more space.

Circuit Diagram

You can use tools like Tinkercad or Fritzing to create the circuit diagram, ensuring connections match the details above.


Project Features

  • Fully autonomous robot.
  • Simple and fun to build for all age groups.
  • Can handle obstacles dynamically, making it engaging to watch.

This project is interactive and demonstrates the basics of robotics and obstacle avoidance.

Share, Support, and Subscribe!!! 1. PRO KAM EXPLAINED 2. Knowledge KAM 3. PRO KAM Follow us on 1. Facebook 2. Instagram 3. Pinterest 4. Blogspot 5 Twitter If you have any other ideas to make me design, you can describe them in the comment section and if I can, I will make a designing video on it. So I am waiting #prokam #wtc #Arduino #ideas #projects #diy #how #to #ArduinoProjects #UltrasonicSensor #HeightMeasurement #TechDIY #WeTechCreators #wtczone

Post a Comment

0 Comments

Ad Code

Responsive Advertisement