How to make an Obstacle avoidance and Bluetooth control robot using a robot tank kit

How to make an Obstacle avoidance and Bluetooth control robot using a robot tank kit

Hello, welcome back. In this project, we will learn how to make an obstacle-avoiding and Bluetooth control robot using a robotic tank kit provided by OSEPP. I want to share with you my experience with this robot tank kit. It has an interesting design and is in good condition. I also used the Arduino UNO board and the L293D driver shield to control this robot tank. You can use another Arduino board if you want. But the UNO board and the L293D motor driver shield are very easy to use. Also, I used the HC-05 module to control Bluetooth. Ok, let’s go ahead.

You can buy this robot tank kit using this link – Robot Tank Kit from osepp.com

OK, let’s do this project step by step. The required components are given below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, let’s identify these components.

Step 2

Secondly, unbox this kit and get the components.

Step 3

OK, now let’s assemble this robot tank kit using the guidebook.

  • Firstly, connect the motor supports.
  • Secondly, connect the double beams to the motors.
  • Thirdly, connect the rims for the motors.
  • Then, connect the other rims to the double beams.
  • Now, assemble the middle double beam as follows.
  • Next, connect it to the main double beams.
  • Then, connect the upper double beams to the main part.
  • OK, now connect the battery holder to the base plate.
  • Next, connect it to the top of the robot.
  • Now we will assemble the ultrasonic sensor mount.
  • Then, connect it to another base plate.
  • Next, connect this part to the front of the robot tank.
  • Now we will assemble the track chain of the robot.
  • After, connect it to the robot tank as follows.

Wow. Finally, We did it.

Step 4

Ok, we will attach the electronic parts to the robot tank. First, connect the Arduino UNO board to the motor driver shield. Then, mount it on top of the robot.

Step 5

Then connect the motors and battery holder to the motor driver shield. For that, use the circuit diagram below.

How to make an Obstacle avoidance and Bluetooth control robot using a robot tank kit

Step 6

Next, attach the ultrasonic sensor to the mount and connect it to the motor driver shield. For that, use the circuit diagram above.

Step 7

After, connect the Bluetooth module to the motor driver shield.

Step 8

OK, finally connect this robot to the computer.

Now let’s create the program for this robot. It is as follows.

  • The full program of this robot — Download
/*Obstacle avoidance and Bluetooth control robot
 * https://srituhobby.com
 */
 
#include <Servo.h>
#include <AFMotor.h>
#define Speed 220
#define Echo A0
#define Trig A1
#define Spoint 135
#define MinPoint 80
#define MaxPoint 180

Servo servo;
AF_DCMotor motor1(2);
AF_DCMotor motor2(3);

void setup() {
  Serial.begin(9600);
  servo.attach(10);
  servo.write(Spoint);
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);
  motor1.setSpeed(Speed);
  motor2.setSpeed(Speed);
  delay(2000);
}

void loop() {
  obstacle ();
 // bluetooth();
}

void bluetooth() {
  if (Serial.available() > 0) {
    char value = Serial.read();
    Serial.println(value);
    
    if (value == 'U') {
      forward();
    } else if (value == 'D') {
      backward();
    } else if (value == 'L') {
      left();
    } else if (value == 'R') {
      right();
    } else if (value == 'S') {
      Stop();
    }
  }
}

void obstacle () {
  int Distance = distance();
  if (Distance < 10) {
    Stop();
    Serial.println("Stop");
    delay(500);
    servoLeft();
    int Left = distance();
    Serial.println(Left);
    servoRight();
    int Right = distance();
    Serial.println(Right);
    if (Left < Right) {
      right();
      Serial.println("Right");
      delay(1000);
    } else {
      left();
      Serial.println("Left");
      delay(1000);
    }
  } else {
    forward();
    Serial.println("Forward");
  }
}

void forward() {
  motor1.run(FORWARD);
  motor2.run(FORWARD);
}
void backward() {
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
}
void left() {
  motor1.run(BACKWARD);
  motor2.run(FORWARD);
}
void right() {
  motor1.run(FORWARD);
  motor2.run(BACKWARD);
}
void Stop() {
  motor1.run(RELEASE);
  motor2.run(RELEASE);
}

int distance() {
  //pulse output
  digitalWrite(Trig, LOW);
  delayMicroseconds(4);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);

  long t = pulseIn(Echo, HIGH);
  long cm = t / 29 / 2;
  return cm;
}

void servoLeft() {
  for (int a = Spoint; a < MaxPoint; a++) {
    servo.write(a);
    delay(20);
  }
  for (int b = MaxPoint; Spoint < b; b--) {
    servo.write(b);
    delay(20);
  }
}

void servoRight() {
  for (int a = Spoint; a > MinPoint; a--) {
    servo.write(a);
    delay(20);
  }
  for (int b = MinPoint; Spoint > b; b++) {
    servo.write(b);
    delay(20);
  }
}

Code explanation

First, the servo motor and AF motor libraries are included. Later, I defined a few keywords for the program.

#include <Servo.h>
#include <AFMotor.h>
#define Speed 220
#define Echo A0
#define Trig A1
#define Spoint 135
#define MinPoint 80
#define MaxPoint 180

Secondly, objects were created for the libraries.

Servo servo;
AF_DCMotor motor1(2);
AF_DCMotor motor2(3);

In the setup function,

void setup() {
//Start the serial communication
  Serial.begin(9600);
//Set the servo PWM pin
  servo.attach(10);
//Write the start point of servo motor
  servo.write(Spoint);
//Set the Trig pin as the output pin and the Echo pin as the input pin
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);
//Include the speed of the motors
  motor1.setSpeed(Speed);
  motor2.setSpeed(Speed);
//Set the start delay
  delay(2000);
}

In the loop function, It includes two functions. You can upload these functions separately. For that, remove the forward-slash and upload this code. These functions are described below.

void loop() {
  // obstacle ();
 // bluetooth();
}

Bluetooth functions of this robot.

void bluetooth() {
//Check the serial communication is enable or not
  if (Serial.available() > 0) {
//Get the serial value
    char value = Serial.read();
    Serial.println(value);
//Check these values using the IF condition
    if (value == 'U') {
      forward();
    } else if (value == 'D') {
      backward();
    } else if (value == 'L') {
      left();
    } else if (value == 'R') {
      right();
    } else if (value == 'S') {
      Stop();
    }
  }
}

Obstacle avoidance function of this robot.

void obstacle () {
//Get the distance from the ultrasonic sensor
  int Distance = distance();
//Check the distance ahead is less than or greater than to 10 using the IF condition
  if (Distance < 10) {
//Stop the robot
    Stop();
    Serial.println("Stop");
    delay(500);
//Turn the servo motor to the left.After, get the distance.
    servoLeft();
    int Left = distance();
    Serial.println(Left);
//Turn the servo motor to the right.After, get the distance.
    servoRight();
    int Right = distance();
    Serial.println(Right);
//Then check the farthest side using the IF condition. Then turn the robot that way.
    if (Left < Right) {
      right();
      Serial.println("Right");
      delay(1000);
    } else {
      left();
      Serial.println("Left");
      delay(1000);
    }
  } else {
    forward();
    Serial.println("Forward");
  }
}

Step 9

Right now first upload the obstacle avoidance code to the robot tank. To do this, remove the forward-slash of the obstacle function.

  • Now remove the RX and TX jumper wires and Upload this code to the robot.

Okay, now you can enjoy this project as an obstacle-avoiding robot.

Step 10

So let’s upload the Bluetooth control code to the robot tank. To do this, remove the forward-slash of the Bluetooth function.

  • Then select board and port. After, upload this code to the Arduino board.
  • Now, reconnect the RX and TX jumper wires. You must remember it.

Step 11

Ok, now let’s set up the Bluetooth controller app. For that, follow the steps below.

  • First, install this app using this link — Download(Only play store)
  • Next, run this application and click the “Controllers” button.
How to make a simple Bluetooth control car step by step
  • Then, turn on Bluetooth. After, click the gear wheel icon in the upper right corner and select the “Connect to Device” button.
  • Now you can find new Bluetooth devices. To do so, click on the “FIND DEVICES” button. Then, you need to enable Location and Location Permission to find new devices.
  • Now, select the name of your Bluetooth module and enter the PIN as “1234”. Then it will connect.

Now you can control this robot tank using this controller. So, enjoy this project. The full video guide is below. We will meet in the next project.

How to make an Obstacle avoidance and Bluetooth control robot using a robot tank kit

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *