pat
Published © LGPL

Obstacle-Avoiding 2-Wheel Car

Using ultrasonic sensors and Arduino, it navigates smoothly around obstacles, perfect for any enviro

BeginnerFull instructions provided218
Obstacle-Avoiding 2-Wheel Car

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Dual H-Bridge motor drivers L293D
Texas Instruments Dual H-Bridge motor drivers L293D
×1
Ultrasonic Sensor - HC-SR04
SparkFun Ultrasonic Sensor - HC-SR04
×1
9V battery (generic)
9V battery (generic)
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
DC Motor, 12 V
DC Motor, 12 V
×2

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Servo Motor, Premium Male/Male Jumper Wires
Servo Motor, Premium Male/Male Jumper Wires
Solder Wire, Lead Free
Solder Wire, Lead Free
10 Pc. Jumper Wire Kit, 5 cm Long
10 Pc. Jumper Wire Kit, 5 cm Long

Story

Read more

Schematics

Schematic

Code

Code

C/C++
#include <AFMotor.h>
#include <Servo.h>
#include <NewPing.h>

// Define pins for ultrasonic sensor
const int trigPin = A1;
const int echoPin = A2;
const int maxDistance = 40; // cm
NewPing sonar(trigPin, echoPin, maxDistance);

// Define motor control pins
AF_DCMotor leftMotor(1);
AF_DCMotor rightMotor(2);

// Attach servo motor to motor shield's servo pin
Servo servoMotor;

void setup() {
  // Set motor speeds
  leftMotor.setSpeed(200); // Set speed values from 0-255
  rightMotor.setSpeed(200);
  
  // Attach servo motor to motor shield's servo pin
  servoMotor.attach(10); // Assuming pin 10 on the shield is the servo pin
  servoMotor.write(115); // Center the servo motor
  delay(2000); // Wait for servo to center
}

void loop() {
  // Get distance from ultrasonic sensor
  int distance = sonar.ping_cm();
  
  if (distance <= 20) {
    moveStop(); // Stop the robot
    delay(300);
    moveBackward(); // Move backwards
    delay(400);
    moveStop();
    delay(300);
    
    // Check distances to the right and left
    int distanceRight = lookRight();
    delay(300);
    int distanceLeft = lookLeft();
    delay(300);
    
    // Turn towards the direction with more space
    if (distanceRight >= distanceLeft) {
      turnRight();
    } else {
      turnLeft();
    }
  } else {
    moveForward(); // Move forward
  }
}

void moveStop() {
  leftMotor.run(RELEASE);
  rightMotor.run(RELEASE);
}

void moveForward() {
  leftMotor.run(FORWARD);
  rightMotor.run(FORWARD);
}

void moveBackward() {
  leftMotor.run(BACKWARD);
  rightMotor.run(BACKWARD);
}

void turnRight() {
  moveStop();
  delay(300);
  leftMotor.run(FORWARD);
  rightMotor.run(BACKWARD);
  delay(500);
  moveStop();
}

void turnLeft() {
  moveStop();
  delay(300);
  rightMotor.run(FORWARD);
  leftMotor.run(BACKWARD);
  delay(500);
  moveStop();
}

int lookRight() {
  servoMotor.write(50);
  delay(500);
  int distance = sonar.ping_cm();
  servoMotor.write(115);
  return distance;
}

int lookLeft() {
  servoMotor.write(170);
  delay(500);
  int distance = sonar.ping_cm();
  servoMotor.write(115);
  return distance;
}

Credits

pat
13 projects • 4 followers
AI Convergence Engineering, Software Engineering, ML, deep learning, image processing, computer vision, circuit design, and Edge AI devices.

Comments