Smart Tank Chassis with Ultrasonic Sensor using Arduino

In the previous projects I tried different ways to control the smart tank manually. But how about if the tank makes its own decision and control itself? It should be quite interesting. Ultrasonic sensor can help to do so by sending sound wave in front of the sensor. It receives the wave once the wave meets obstacles and reflects to the sensor so as to determine the distance. So I’ve bought an ultrasonic sensor and start another project.

Smart Tank Chassis with Ultrasonic Sensor

Step 1: Parts

HC-SR04 Ultrasonic Sensor with Servo and Rack

Smart Tank

Arduino Uno

Small Breadboard

Battery Box AA x 4

Battery Box 9-volt

I just add one more sensor on top of the third project actually.

Step 2: Wiring

I leave the servo unwired at this stage. For the sensor the wiring is as follow:

GND > Arduino GND

Echo > Pin 6

Trig > Pin 5

VCC > 5V

For the smart tank I listed the wiring again below. That is the same as the previous projects:

IB on the right side > pin 8

IA on the left side > pin 9

IA on the right side > pin 10

IB on the left side > pin 11

VCC on both side > + of the battery box

GND on both side > – of the battery box and Arduino GND

Step 3: Test the sensor

I wanna test if the sensor works, so I google it and find anything can help. Google is our friend and this link can be one of the reference:

http://arthursrobotorial.blogspot.hk/2012_12_01_archive.html

#define trigPin 5

#define echoPin 6

void setup()

{

Serial.begin (9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

}

int CheckDistance()

{

long duration, distance;

digitalWrite(trigPin, LOW); // Added this line

delayMicroseconds(2); // Added this line

digitalWrite(trigPin, HIGH);

// delayMicroseconds(1000); – Removed this line

delayMicroseconds(10); // Added this line

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

return distance;

}

void loop()

{

int testDistance = CheckDistance(); /// get object distance using ping

/// if object is more than 50 cm away it is out of range

if (testDistance >= 50 || testDistance <= 0) /// if object is more than 50 cm away it is out of range

{

Serial.println(“Out of range”);

}

else /// object is closer than 50cm, print distance

{

Serial.print(testDistance);

Serial.println(” cm”);

delay(500); ///wait half a sec before next ping

}

In Arduino IDE, select Tools > Serial Monitor. It shows the distance if succeeds.

Step 4: Test the sensor with the tank

Smart Tank Chassis with Ultrasonic Sensor circuit

Then I combine the code in the first stage with the code in the third project and revise a little bit: If the distance between the sensor and the object is more than 30cm, it goes forward. Otherwise it turns right.


#define trigPin 5

#define echoPin 6

int motorPin = 8; //right side to IB – forwward

int motorPin2 = 9; //left side to IA – forwward

int motorPin3 = 10; //right side to IA – backward

int motorPin4 = 11; //left side to IB – backward

void setup()

{

Serial.begin (9600);

pinMode(motorPin, OUTPUT);

pinMode(motorPin2, OUTPUT);

pinMode(motorPin3, OUTPUT);

pinMode(motorPin4, OUTPUT);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

}

void forward(){

digitalWrite(motorPin, HIGH);

digitalWrite(motorPin2, HIGH);

digitalWrite(motorPin3, LOW);

digitalWrite(motorPin4, LOW);

}

void backward() {

digitalWrite(motorPin, LOW);

digitalWrite(motorPin2, LOW);

digitalWrite(motorPin3, HIGH);

digitalWrite(motorPin4, HIGH);

}

void turnLeft() {

digitalWrite(motorPin, HIGH);

digitalWrite(motorPin2, LOW);

digitalWrite(motorPin3, LOW);

digitalWrite(motorPin4, HIGH);

}

void turnRight() {

digitalWrite(motorPin, LOW);

digitalWrite(motorPin2, HIGH);

digitalWrite(motorPin3, HIGH);

digitalWrite(motorPin4, LOW);

}

int CheckDistance()

{

long duration, distance;

digitalWrite(trigPin, LOW); // Added this line

delayMicroseconds(2); // Added this line

digitalWrite(trigPin, HIGH);

// delayMicroseconds(1000); – Removed this line

delayMicroseconds(10); // Added this line

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

return distance;

}

void loop()

{

int testDistance = CheckDistance(); /// get object distance using ping

if (testDistance >= 30 || testDistance <= 0) /// if object is more than 30 cm away it goes forward

{

forward();

}

else /// object is closer than 30cm, turns right

{

turnRight();

}

delay(500); ///wait half a sec before next ping

}

The result seems ok.

 

For more detail: Smart Tank Chassis with Ultrasonic Sensor using Arduino


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

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

Scroll to Top