How to make a multi-function Arduino robot

How to make a multi-function Arduino robot

Hello, welcome back to another interesting tutorial from the SriTu Hobby. 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.

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

Step 1

Firstly, identify these components.

Step 2

Secondly, cut the foam board piece as follows.

How to make a multi-function Arduino robot

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.

How to make a multi-function Arduino robot
How to make a multi-function Arduino robot

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 — Download
  • The complete program of this project – Download
/*obstacle avoiding, Bluetooth control, voice control robot car.
   https://srituhobby.com
*/
#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 pin, 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.

OK, now click the voice control button and enjoy this project. The full video guide is given below. So, we will meet in the next tutorial.

How to make a multi-function Arduino robot

Similar Posts

20 Comments

  1. Hello, this is an excellent project/tutorial. I made it and it works great. However, I tried to expand some of the commands e.g front lights ON/OFF but I did not see the code (“W”) in the serial monitor. I would also like to use the Horn and the slider but I don’t get any serial data from those controls. I only see the Forward, Reverse, Left, Right and Stop commands through the serial port.

    Could you explain why this happens. Thanks very much.

    1. For that, you need to modify this code. First, you need to select the commands that you have with the Bluetooth app. After, those commands must be entered using the IF condition

  2. Hi, I have the same project. I downloaded the library and put the codes. I tried to compile but I got an error message saying that the ‘A1’ is not defined. That is the trig a1. What do I do from here, please? it is quite urgent

  3. hy..hiii i found the project interesting….i am planning to do this project…can i get the block diagram and if u had done any software simulation of the project……

  4. error-Arduino: 1.8.18 (Windows 10), Board: “Arduino Uno”

    bluetooth_control_voice_obstacle:5:10: fatal error: AFMotor.h: No such file or directory

    #include

    ^~~~~~~~~~~

    compilation terminated.

    exit status 1

    AFMotor.h: No such file or directory

    This report would have more information with
    “Show verbose output during compilation”
    option enabled in File -> Preferences.

  5. I am working on this project but while working on obstacle avoiding part, I am unable to get values from my ultrasonic sensor,the sensor is perfectly fine I tested it through arduino.

  6. I made everything like you show in video but I used 18.5Cm X 14Cm cardboard car is not taking left or right only forward and backword is moving how to resolve

  7. I Made this project its really awesome but am getting an error where the power supply for the Bluetooth from gnd and vcc is done and i have connected RXD to D1 and TXD to DO but it isnt working as the Bluetooth is connecting but there is no operation. Is it okay to change RXD to D3 and TXD to D2 will it work?

  8. Hey could i please contact you as im working on an avoidance obstacle with the servo and ultrasonic but though a raspeberry pi and would really love some insight on my code , my email is below.

Leave a Reply

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