Arduino Tank Car Lesson 6- Obstacle Avoidance

About: Osoyoo brand products are mainly focused on STEM education.User experience and customer feeling are always our first priority. Our goal is to create the best designed product to help students and hobbyist to...

Intro: Arduino Tank Car Lesson 6- Obstacle Avoidance

In this lesson, we will use a servo motor, an ultrasonic module and a buzzer to detect the obstacle and measure the distance. If the distance is less than predefined threshold value, the buzzer will beep and the car will turn around from the obstacle automatically.

Detailed Tutorials: http://kookye.com/?p=5794

Buy it for USA : Amazon.com

Step 1: How It Work

The ultrasonic sensor module have two parts of ultrasonic transmitter and receiver. It uses ultrasonic echo and reflection of the time difference to measure the distance test.

The ultrasonic sensor is to be installed on SG90 servo motor, the sensor can detect obstacles in the range of 0-180 degree. When the obstacles detected, the car will stop to alarm and the SG90 servo motor will rotate around. Then the car will run to the direction without obstacles.

Detailed Tutorials: http://kookye.com/?p=5794

Buy it for USA : Amazon.com

Step 2: Software Installation

The SG90 Servo can rotate approximately 180 degrees. Firstly we

make the servo motor go to 90 degree position(middle position), then manually adjust the ultasonic sensor to straight forward direction. Thus to make sure the ultrasonic module can rotate to the left 90 degrees and also can rotate to the right 90 degrees. Download the servo adjust program from link:

http://www.kookye.com/download/car/servo_adjust.zi...

Download and unzip, then upload the code to Uno board using Arduino IDE, turn on the power switch on the battery box. the servo motor will go to 90 degree position(middle position), At this time, if the ultrasonic sensor is not facing front direction of the car, you need manually adjust the sensor to straight forward direction.

Step 1:Download Lesson 6 sample code from http://www.kookye.com/download/car/tank_robot_les... unzip the download zip file tank_robot_lesson6.zip, you will see a folder called tank_robot_lesson6.
Step 2: Connect UNO R3 board to PC with USB cable, Open Arduino IDE -> click file -> click Open -> choose code “tank_robot_lesson6.ino” in tank_robot_lesson6 folder, load the code into arduino.

Step 3: Choose corresponding board and port for your project,upload the sketch to the board.

Step 3: Understand the Code

Step 1: Define the pinout of SG90 servo motor, IR receiver and ultrasonic sensor and buzzer.

#define SERVO 11 //servo connect to D11

#define IRPIN 13 //IR receiver Signal pin connect to Arduino pin 13

#define echo A3 // Ultrasonic Echo pin connect to A2

#define trig A2 // Ultrasonic Trig pin connect to A3

#define buzzer 7 //buzzer connect to D7

Step 2: Define the variable to store the value of the distance in different direction and tha maxium value of the distance between the car and the obstacle.

int leftscanval, centerscanval, rightscanval, ldiagonalscanval, rdiagonalscanval;

const int distancelimit = 30; //distance limit for obstacles in front

const int sidedistancelimit = 18; //minimum distance in cm to obstacles at both sides (the car will allow a shorter distance sideways)

Step 3: The structure to test the distance between the car and the obstacle.

/*detection of ultrasonic distance*/

int watch()

{ long howfar;

digitalWrite(trig, LOW);

delayMicroseconds(5);

digitalWrite(trig, HIGH);

delayMicroseconds(15);

digitalWrite(trig, LOW);

howfar = pulseIn(echo, HIGH);

howfar = howfar * 0.01657;

//how far away is the object in cm Serial.println((int)howfar);

return round(howfar);}

Step 4:Rotate the servo motor and call the structure of what() in the structure of auto_avoidance(), it can detect and test the distance between the car and the obstacle to avoid obstacles.

void auto_avoidance()

{head.write(90);

delay(100);

centerscanval = watch();

if (centerscanval >= distancelimit)

{ set_motorspeed(LSPEED, RSPEED);

go_ahead(); }

else{ go_stop();

alarm();

head.write(120);

delay(150);

ldiagonalscanval = watch();

head.write(180);

delay(150);

leftscanval = watch();

head.write(90);

delay(150);

head.write(60);

delay(150);

rdiagonalscanval = watch();

head.write(0);

delay(150);

rightscanval = watch();

head.write(90);

if (ldiagonalscanval >= sidedistancelimit && leftscanval >= sidedistancelimit)

{ set_motorspeed(LSPEED, RSPEED);

go_back();

delay(200);

turn_left();

delay(500); }

else if (rdiagonalscanval >= sidedistancelimit && rightscanval >= sidedistancelimit)

{ set_motorspeed(LSPEED, RSPEED);

go_back();

delay(200);

turn_right();

delay(500); }

}

}

Step 4: Hardware Installation

Step 1: Install Expansion Board on UNO R3 board.

Step 2: Turn the switch of expansion board to "1" and "2" position, as the following photo shows.

Step 3: Move the wire connected to pinout(VCC,Trig,Echo,GND) in Ultrasonic sensor to the counterpart pin in expansion Board as the following picture.

Ultrasonic sensor - ESP8266 expansion board

Step 5:Move the wire connected to pinout(GND,VCC,S) in SG90 Servo to the counterpart pin in Driver Board as the following picture.

Step 6: Put two 12865 batteries into battery box and turn the swith of box to "ON".

(If you have finished the above steps on lesson one, please skip these step)

Step 7: Press the button of "OK" to make the car avoid obstacles and press the button of "0" to make the car stop.

Step 8: Testing.Please check the wire connection if the car can not be run as expected.

Share

    Recommendations

    • Furniture Contest 2018

      Furniture Contest 2018
    • Audio Contest 2018

      Audio Contest 2018
    • Tiny Home Contest

      Tiny Home Contest

    Discussions