Project tutorial
How to Make Obstacle Avoiding Robot

How to Make Obstacle Avoiding Robot © GPL3+

A self-driven robot car that can detect and automatically avoid obstacles that it hits or gets in its way.

  • 26,644 views
  • 9 comments
  • 45 respects

Components and supplies

About this project

The Project

This project is for my Artificial Intelligence subject in my school and it's my first Arduino project, so I would like to share this project with you.

Step 1

First build the Arduino robot chassis. The kit contains the chassis, two motors with the wheels attached, a front wheel, a battery holder, some screws and wires.

Step 2

Solder the thick black and red wires at the motors.

Step 3

Attach the front wheel. After, attach the rear wheels according to the instructions leaflet that comes with kit.

Step 4

The next step is to attach the switch to the battery holder. Cut the battery holder black wire and solder the one end to separate side of the switch. Our switch is ready. Then attach it to the battery holder using the screw that comes with the chassis kit.

Step 5

The next step is to connect the motors. We connect the left motor to the M1 connector of the shield like this. We connect the right motor to the M3 connector of the shield this way.

Step 6

Move on to the motor shield. This shield provides power to the motors and the servo motor, and makes our project easier. The motors need a lot of current, and this shield can provide up to 600mA of current to each motor, that’s why we need it. We have to solder 4 wires to the motor shield. These wires will be then connected with the supersonic sensor. We solder one to 5V, one to GND, one to Analog Pin 5, and the last one to analog pin 4.

Step 7

Next we attach the motor shield to the Arduino Uno board. With a double sided tape, attach the Arduino Uno and the battery holder to the chassis. With a double sided tape, also attach the supersonic sensor to the servo motor, and both of them to the robot chassis. I used a hot glue gun to attach the servo motor to the chassis.

Supersonic Sensor

We need to follow these steps to configure the supersonic sensor.

Step 1

Supersonic sensor. Vcc goes to 5V via the wire we attached earlier. GND goes to the GND wire. ECHO pin goes to Analog Pin 5, and TRIG pin goes to Analog Pin 4.

Step 2

Next, to connect the power. We connect the black wire from the switch to the motor shield. It goes to the GND pin of this external power connector. The red wire from the switch goes to the M+ connector. If we open the switch, we can see that the green LED lights up, the Arduino is receiving power from the batteries.

Step 3

Lastly we connect the servo motor to the servo_2 slot. The darker color on the right end side red in the middle and orange in the left side order.

Code

The code uses three libraries. Two of them must be downloaded in order the program to compile. The first one is the motor shield driver from Adafruit. The second library is the NewPing library for the supersonic distance sensor. You can find the links for both libraries in here:

Download Links

To view this project working, see the link below.

https://www.youtube.com/watch?v=0qydiM1N17o

NOTE! To be updated for my future projects and have inquires. Please contact me thru this following social media account.

Subscribe to my YouTube channel here.

Like my FaceBook.

Follow me on Twitter.

Code

Arduino Obstacle Avoidance RobotArduino
Copy and Paste the code in Arduino IDE then upload to your Arduino board
#include <AFMotor.h> 
#include <Servo.h>    
#include <NewPing.h>

#define TRIG_PIN A4 
#define ECHO_PIN A5
#define MAX_DISTANCE_POSSIBLE 1000 
#define MAX_SPEED 150 // 
#define MOTORS_CALIBRATION_OFFSET 3
#define COLL_DIST 20 
#define TURN_DIST COLL_DIST+10 
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE_POSSIBLE);

AF_DCMotor leftMotor(4, MOTOR12_8KHZ); 
AF_DCMotor rightMotor(3, MOTOR12_8KHZ); 
Servo neckControllerServoMotor;

int pos = 0; 
int maxDist = 0;
int maxAngle = 0;
int maxRight = 0;
int maxLeft = 0;
int maxFront = 0;
int course = 0;
int curDist = 0;
String motorSet = "";
int speedSet = 0;


void setup() {
  neckControllerServoMotor.attach(10);  
  neckControllerServoMotor.write(90); 
  delay(2000);
  checkPath(); 
  motorSet = "FORWARD"; 
  neckControllerServoMotor.write(90);
  moveForward();
}



void loop() {
  checkForward();  
  checkPath();
}

void checkPath() {
  int curLeft = 0;
  int curFront = 0;
  int curRight = 0;
  int curDist = 0;
  neckControllerServoMotor.write(144);
  delay(120); 
  for(pos = 144; pos >= 36; pos-=18)
  {
    neckControllerServoMotor.write(pos);
    delay(90);
    checkForward(); 
    curDist = readPing();
    if (curDist < COLL_DIST) {
      checkCourse();
      break;
    }
    if (curDist < TURN_DIST) {
      changePath();
    }
    if (curDist > curDist) {maxAngle = pos;}
    if (pos > 90 && curDist > curLeft) { curLeft = curDist;}
    if (pos == 90 && curDist > curFront) {curFront = curDist;}
    if (pos < 90 && curDist > curRight) {curRight = curDist;}
  }
  maxLeft = curLeft;
  maxRight = curRight;
  maxFront = curFront;
}

void setCourse() {
    if (maxAngle < 90) {turnRight();}
    if (maxAngle > 90) {turnLeft();}
    maxLeft = 0;
    maxRight = 0;
    maxFront = 0;
}

void checkCourse() {
  moveBackward();
  delay(500);
  moveStop();
  setCourse();
}

void changePath() {
  if (pos < 90) {lookLeft();} 
  if (pos > 90) {lookRight();}
}


int readPing() {
  delay(70);
  unsigned int uS = sonar.ping();
  int cm = uS/US_ROUNDTRIP_CM;
  return cm;
}

void checkForward() { if (motorSet=="FORWARD") {leftMotor.run(FORWARD); rightMotor.run(FORWARD); } }

void checkBackward() { if (motorSet=="BACKWARD") {leftMotor.run(BACKWARD); rightMotor.run(BACKWARD); } }

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

void moveForward() {
    motorSet = "FORWARD";
    leftMotor.run(FORWARD);
    rightMotor.run(FORWARD);
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2)
  {
    leftMotor.setSpeed(speedSet+MOTORS_CALIBRATION_OFFSET);
    rightMotor.setSpeed(speedSet);
    delay(5);
  }
}

void moveBackward() {
    motorSet = "BACKWARD";
    leftMotor.run(BACKWARD);
    rightMotor.run(BACKWARD);
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2)
  {
    leftMotor.setSpeed(speedSet+MOTORS_CALIBRATION_OFFSET);
    rightMotor.setSpeed(speedSet);
    delay(5);
  }
}  

void turnRight() {
  motorSet = "RIGHT";
  leftMotor.run(FORWARD);
  rightMotor.run(BACKWARD);
  delay(400);
  motorSet = "FORWARD";
  leftMotor.run(FORWARD);
  rightMotor.run(FORWARD);      
}  

void turnLeft() {
  motorSet = "LEFT";
  leftMotor.run(BACKWARD);
  rightMotor.run(FORWARD);
  delay(400);
  motorSet = "FORWARD";
  leftMotor.run(FORWARD);
  rightMotor.run(FORWARD);
}  

void lookRight() {rightMotor.run(BACKWARD); delay(400); rightMotor.run(FORWARD);}
void lookLeft() {leftMotor.run(BACKWARD); delay(400); leftMotor.run(FORWARD);}

Schematics

Schematic Diagram
Schematic t3e88fhfow

Comments

Similar projects you might like

Ground-Based Obstacle Avoiding Robot

Project showcase by Xavier Tan

  • 6,107 views
  • 1 comment
  • 21 respects

Project 1: 2WD Obstacle Avoiding Robot

Project showcase by HDA Robotics

  • 12,075 views
  • 9 comments
  • 25 respects

Obstacles Avoiding Robot With Servo Motor

Project tutorial by Sora JY

  • 6,606 views
  • 3 comments
  • 20 respects

Transformer's Baby: Arduino Avoiding Obstacle Robot

Project tutorial by diyProjectsStepByStep

  • 2,969 views
  • 0 comments
  • 13 respects

Bluetooth Controlled & Obstacle Avoiding Arduino Robot

Project showcase by KureBas Robotics

  • 8,435 views
  • 11 comments
  • 20 respects
Add projectSign up / Login