Contents
How to make a multi-function Arduino robot .............................................................................. 2
The process of this robot ...................................................................................................... 2
Step 1 .................................................................................................................................. 3
Step 2 .................................................................................................................................. 8
Step 3 .................................................................................................................................. 9
Step 4 .................................................................................................................................10
Step 5 .................................................................................................................................11
Step 6 .................................................................................................................................13
Step 7 .................................................................................................................................15
Step 8 .................................................................................................................................16
Step 9 .................................................................................................................................18
Step 10 ...............................................................................................................................20
Step 11 ...............................................................................................................................22
Step 12 ...............................................................................................................................23
Code explanation ................................................................................................................27
Step 13 ...............................................................................................................................30
How to make a multi-function Arduino robot
In this tutorial, we will learn how to make a multi-functional robot using the Arduino platform.
That is, this includes obstacle avoidance, Bluetooth control, and voice control functions. Also, It
mainly uses an Ultrasonic sensor and a Bluetooth module. The L293D motor drive shield is used
to drive the motors. Also, in this tutorial, you will be able to easily create this as the tutorial
shows you step by step how to build this robot car.
The process of this robot
We can control this robot car using three methods. That is,
1.Obstacle avoidance
In this case, the robot car moves along using the obstacle avoiding. The ultrasonic sensor is
mainly used for this purpose. Study the previous articles for more information.
2.Bluetooth control
In this case, we can control the robot through an app on the smartphone. The Bluetooth module
is used for this.
3.Voice control
In this case, we can control this robot using several voice commands. This also requires a
Bluetooth module and mobile app.
OK,let’s do this project step by step. The required components are given below.
• Arduino UNO board x 1
• L293D motor driver x 1
• Ultrasonic sensor x 1
• Bluetooth module x 1
• Servo motor x 1
• Gear motor x 4
• Robot wheel x 4
• Li-ion battery holder x 1
• Li-ion battery x 2
• Jumper wires
Step 1
Firstly, identify these components.
L293D motor driver shield
Ultrasonic sensor
Bluetooth module
Servo motor
Li-ion battery
Li-ion battery
Foamboard
Robot wheel
Gear motor
Jumper wires
Arduino UNO board
Step 2
Step 3
Thirdly, glue the four gear motors to the foam board piece.
Step 4
Then, attach the motor shield to the Arduino board and glue it to the robot chassis.
Step 5
Next, dig two holes on either side of the Arduino board and insert the gear motor wire through
these holes.
Step 6
Then, connect the motors to the motor driver shield. To do this, use the circuit diagram below.
Step 7
Afterward, attach the servo motor and ultrasonic sensor as follows.
Step 8
Next, connect the servo motor and the ultrasonic sensor using the circuit diagram above.
×
Step 9
Then, connect the Bluetooth module to the motor driver shield and glue it to the robot chassis.
Step 10
After, glue the battery holder and connect it to the driver shield.
Step 11
Now, attach the robot wheels and put the batteries to the battery holder.
Step 12
So, let’s create the program for this project. This program includes all three functions. We can
run these separately. It is as follows.
• AF motor library
• The complete program of this project
/*obstacle avoiding, Bluetooth control, voice control robot car.
Home Page
*/
#include <Servo.h>
#include <AFMotor.h>
#define Echo A0
#define Trig A1
#define motor 10
#define Speed 170
#define spoint 103
char value;
int distance;
int Left;
int Right;
int L = 0;
int R = 0;
int L1 = 0;
int R1 = 0;
Servo servo;
AF_DCMotor M1(1);
AF_DCMotor M2(2);
AF_DCMotor M3(3);
AF_DCMotor M4(4);
void setup() {
Serial.begin(9600);
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
servo.attach(motor);
M1.setSpeed(Speed);
M2.setSpeed(Speed);
M3.setSpeed(Speed);
M4.setSpeed(Speed);
}
void loop() {
//Obstacle();
//Bluetoothcontrol();
//voicecontrol();
}
void Bluetoothcontrol() {
if (Serial.available() > 0) {
value = Serial.read();
Serial.println(value);
}
if (value == 'F') {
forward();
} else if (value == 'B') {
backward();
} else if (value == 'L') {
left();
} else if (value == 'R') {
right();
} else if (value == 'S') {
Stop();
}
}
void Obstacle() {
distance = ultrasonic();
if (distance <= 12) {
Stop();
backward();
delay(100);
Stop();
L = leftsee();
servo.write(spoint);
delay(800);
R = rightsee();
servo.write(spoint);
if (L < R) {
right();
delay(500);
Stop();
delay(200);
} else if (L > R) {
left();
delay(500);
Stop();
delay(200);
}
} else {
forward();
}
}
void voicecontrol() {
if (Serial.available() > 0) {
value = Serial.read();
Serial.println(value);
if (value == '^') {
forward();
} else if (value == '-') {
backward();
} else if (value == '<') {
L = leftsee();
servo.write(spoint);
if (L >= 10 ) {
left();
delay(500);
Stop();
} else if (L < 10) {
Stop();
}
} else if (value == '>') {
R = rightsee();
servo.write(spoint);
if (R >= 10 ) {
right();
delay(500);
Stop();
} else if (R < 10) {
Stop();
}
} else if (value == '*') {
Stop();
}
}
}
// Ultrasonic sensor distance reading function
int ultrasonic() {
digitalWrite(Trig, LOW);
delayMicroseconds(4);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
long t = pulseIn(Echo, HIGH);
long cm = t / 29 / 2; //time convert distance
return cm;
}
void forward() {
M1.run(FORWARD);
M2.run(FORWARD);
M3.run(FORWARD);
M4.run(FORWARD);
}
void backward() {
M1.run(BACKWARD);
M2.run(BACKWARD);
M3.run(BACKWARD);
M4.run(BACKWARD);
}
void right() {
M1.run(BACKWARD);
M2.run(BACKWARD);
M3.run(FORWARD);
M4.run(FORWARD);
}
void left() {
M1.run(FORWARD);
M2.run(FORWARD);
M3.run(BACKWARD);
M4.run(BACKWARD);
}
void Stop() {
M1.run(RELEASE);
M2.run(RELEASE);
M3.run(RELEASE);
M4.run(RELEASE);
}
int rightsee() {
servo.write(20);
delay(800);
Left = ultrasonic();
return Left;
}
int leftsee() {
servo.write(180);
delay(800);
Right = ultrasonic();
return Right;
}
Code explanation
Firstly, libraries are included.
#include <Servo.h>
#include <AFMotor.h>
Secondly, ultrasonic sensor pins, servo motor pins, motor speed, and servo motor starting point
are defined.
#define Echo A0
#define Trig A1
#define motor 10
#define Speed 170
#define spoint 103
Thirdly, some variables have been created to help the program.
char value;
int distance;
int Left;
int Right;
int L = 0;
int R = 0;
int L1 = 0;
int R1 = 0;
Then, objects are created for the Servo Library and the AFMotor Library.
Servo servo;
AF_DCMotor M1(1);
AF_DCMotor M2(2);
AF_DCMotor M3(3);
AF_DCMotor M4(4);
In the setup function, Ultrasonic pins are set to INPUT and OUTPUT. Also, the gear motor
speeds have been included.
void setup() {
Serial.begin(9600);
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
servo.attach(motor);
M1.setSpeed(Speed);
M2.setSpeed(Speed);
M3.setSpeed(Speed);
M4.setSpeed(Speed);
}
In the loop function, the three main functions are included. we can run these functions one by
one. These are described below.
void loop() {
//Obstacle();
//Bluetoothcontrol();
//voicecontrol();
}
This function includes the Bluetooth control code. The code lines are described one by one in the
code
void Bluetoothcontrol() {
//gets the serial communication values and puts them into the char variable.
if (Serial.available() > 0) {
value = Serial.read();
Serial.println(value);
}
//Next, these values are checked using the IF condition.
//Then, if the char value is 'F', the car moves forward.
if (value == 'F') {
forward();
//If the char value is "B", the car moves backward.
} else if (value == 'B') {
backward();
//If the char value is "L", the car moves left.
} else if (value == 'L') {
left();
//If the char value is "R", the car moves right.
} else if (value == 'R') {
right();
//If the char value is "S", the car is stopped.
} else if (value == 'S') {
Stop();
}
}
This function includes the obstacle-avoiding code. The code lines are described one by one in the
code.
void Obstacle() {
//gets the ultrasonic sensor reading and puts it into the variable.
distance = ultrasonic();
//then, these values are checked using the IF condition.
//If the value is less than or equal to 12,
//the robot is stopped and the servo motor rotate left and right.
// Also, gets both side distance.
if (distance <= 12) {
Stop();
backward();
delay(100);
Stop();
L = leftsee();
servo.write(spoint);
delay(800);
R = rightsee();
servo.write(spoint);
//After, if the left side distance less than the right side distance. The
robot turns right.
if (L < R) {
right();
delay(500);
Stop();
delay(200);
//After, if the left side distance more than the right side distance. The
robot turns left.
} else if (L > R) {
left();
delay(500);
Stop();
delay(200);
}
//Otherwise, the robot moves forward.
} else {
forward();
}
}
This function includes the voice control code. The code lines are described one by one in the
code.
void voicecontrol() {
//gets the serial communication values and puts them into the char variable.
if (Serial.available() > 0) {
value = Serial.read();
Serial.println(value);
//If the char value is "^", the car moves forward.
if (value == '^') {
forward();
//If the char value is "-", the car moves backward.
} else if (value == '-') {
backward();
//If the char value is "<", the car moves left.
} else if (value == '<') {
L = leftsee();
servo.write(spoint);
if (L >= 10 ) {
left();
delay(500);
Stop();
} else if (L < 10) {
Stop();
}
//If the char value is ">", the car moves right.
} else if (value == '>') {
R = rightsee();
servo.write(spoint);
if (R >= 10 ) {
right();
delay(500);
Stop();
} else if (R < 10) {
Stop();
}
//If the char value is "*", the car is stopped.
} else if (value == '*') {
Stop();
}
}
}
Step 13
Obstacle avoidance program
OK, now connect this robot car to the computer. Then, remove the two forward slashes in front
of the “obstacle” function. Next, remove the RX and TX jumper wires connected to the
Bluetooth module.
Now, select board and port. After, upload this code to the robot and reconnect the RX and TX
jumper wires.
•
•
• • •
• Now, power On this robot and enjoy it.
Bluetooth control program
OK, now connect this robot car to the computer. Then, remove the two forward slashes in front
of the “Bluetooth control” function. Next, remove the RX and TX jumper wires connected to
the Bluetooth module.
Now, select board and port. After, upload this code to the robot and reconnect the RX and TX
jumper wires.
×
• • • • • OK, now download and install the app below. Then, follow the steps below.
×
After, run this application and click the Settings button. Then, click the “Connect to Car” button
and select the name of the Bluetooth module. Now, you can see the green bulb in the corner.
×
OK, now click the controller buttons and enjoy it.
Voice control program
OK, now connect this robot car to the computer. Then, remove the two forward slashes in front
of the “voicecontrol” function. Next, remove the RX and TX jumper wires connected to the
Bluetooth module.
Now, select board and port. After, upload this code to the robot and reconnect the RX and TX
jumper wires.
×
• • •
• • OK, now download and install the app below. Then, follow the steps below.
After, run this application and click the setting button. Then, click the “voice commands
configuration ” button and include the commands one by one. These are as follows.