How to make a DIY Multi-functional robot car using Arduino
Hello, welcome back. In this tutorial, we will learn how to make a multi-functional robot car using Arduino. Also, this robot car includes three functions. That is, line-following, recognizing obstacles, and controlling the robotic arm. For that, it uses two IR infrared sensors, one ultrasonic sensor, and one robotic arm. For the robot arm, we have used the assembled robot arm kit in a previous tutorial. If you want to visit that tutorial. Use this link. Keep reading.
The process of this robot car
When this robot car is activated, it starts to move forward on the black line. Two IR infrared sensors were used for this purpose. Going forward in this way, the ultrasonic sensor calculates the distance ahead. In this case, when there is an obstacle in front of 9 cm, the robot stops. Then, the obstacle is removed using the robotic arm. After, the robot will move forward freely again. This process continues. Also, all of these components are controlled by the Arduino Uno board and the l293d motor driver shield.
OK, let’s do this project step by step. The required components are given below.
- Arduino UNO board x 1 — Our Store / Amazon
- L293D motor driver x 1 — Our Store / Amazon
- PWM servo motor driver — Our Store / Amazon
- servo motor x 4 — Our Store / Amazon
- Robot arm x 1 — Our Store / Amazon
- IR infrared sensor x 2 — Our Store / Amazon
- Ultrasonic sensor x 1 — Our Store / Amazon
- Gear motor x 2 — Our Store / Amazon
- Robot wheel x 3 — Our Store / Amazon
- Li-ion battery x 2 — Amazon
- Li-ion battery holder x 1 — Our Store / Amazon
- Foam board — Amazon
- Jumper wires — Our Store / Amazon
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 a piece of foam board according to the size below.
Step 3
Thirdly, attach the gear motors to the foam board.
Step 4
Then, attach the robot wheels as follows.
Step 5
Next, cut a 10 cm long piece of foam board and attach the two IR sensors and the ultrasonic sensor to it.
Step 6
Then, cut out the following sizes and glue them as shown below.
Step 7
Now, glue these parts to the front of the robot.
Step 8
Next, attach the robot arm to the robot car chassis. For that, you can use a robot arm kit.
Step 9
After, connect the motor driver shield to the Arduino UNO board. After, glue it to the robot and connect the gear motors to it.
Step 10
Next, attach the PWM servo motor driver and connect the servo motors to it. For that, use the circuit diagram below.
Step 11
Then, connect the two IR sensors and the ultrasonic sensor to the motor driver shield. To do this, use the circuit diagram above.
Step 12
Next, attach the li-ion battery holder to this robot car.
Step 13
Now, connect this robot car to the computer and upload the program for this robot. It is as follows.
- PWM servo motor library — Download
- AFmotor library — Download
- The complete program of this project – Download
/*Multi function robot with Arduino.
* https://srituhobby.com
*/
#include <AFMotor.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver srituhobby = Adafruit_PWMServoDriver();
AF_DCMotor motor1(2);
AF_DCMotor motor2(3);
#define Echo A0
#define Trig A1
#define S1 A2
#define S2 A3
#define Speed 150
#define servo1 0
#define servo2 1
#define servo3 2
#define servo4 3
void setup() {
pinMode(S1, INPUT);
pinMode(S2, INPUT);
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
srituhobby.begin();
srituhobby.setPWMFreq(60);
srituhobby.setPWM(servo1, 0, 340);
srituhobby.setPWM(servo2, 0, 150);
srituhobby.setPWM(servo3, 0, 300);
srituhobby.setPWM(servo4, 0, 290);
motor1.setSpeed(Speed);
motor2.setSpeed(Speed);
}
void loop() {
int distance = obstacle();
Serial.println(distance);
if (distance <= 9) {
motor1.run(RELEASE);
motor2.run(RELEASE);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
delay(100);
motor1.run(RELEASE);
motor2.run(RELEASE);
delay(100);
robotArm();
delay(100);
motor1.run(FORWARD);
motor2.run(FORWARD);
delay(100);
} else {
linefollower();
}
}
void linefollower() {
bool value1 = digitalRead(S1);
bool value2 = digitalRead(S2);
if (value1 == 0 && value2 == 0) {
motor1.run(FORWARD);
motor2.run(FORWARD);
} else if (value1 == 1 && value2 == 1) {
motor1.run(RELEASE);
motor2.run(RELEASE);
} else if (value1 == 1 && value2 == 0) {
motor1.run(BACKWARD);
motor2.run(FORWARD);
} else if (value1 == 0 && value2 == 1) {
motor1.run(FORWARD);
motor2.run(BACKWARD);
}
}
int obstacle() {
digitalWrite(Trig, LOW);
delayMicroseconds(4);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
long t = pulseIn(Echo, HIGH);
int cm = t / 29 / 2;
return cm;
}
void robotArm() {
for (int S4value = 290; S4value <= 490; S4value++) {
srituhobby.setPWM(servo4, 0, S4value);
delay(10);
}
for (int S3value = 300; S3value <= 450; S3value++) {
srituhobby.setPWM(servo3, 0, S3value);
delay(10);
}
for (int S2value = 150; S2value <= 190; S2value++) {
srituhobby.setPWM(servo2, 0, S2value);
delay(10);
}
for (int S4value = 490; S4value > 290; S4value--) {
srituhobby.setPWM(servo4, 0, S4value);
delay(10);
}
for (int S3value = 450; S3value > 300; S3value--) {
srituhobby.setPWM(servo3, 0, S3value);
delay(10);
}
for (int S2value = 190; S2value <= 320; S2value++) {
srituhobby.setPWM(servo2, 0, S2value);
delay(10);
}
for (int S1value = 340; S1value >= 150; S1value--) {
srituhobby.setPWM(servo1, 0, S1value);
delay(10);
}
for (int S3value = 300; S3value <= 410; S3value++) {
srituhobby.setPWM(servo3, 0, S3value);
delay(10);
}
for (int S4value = 290; S4value <= 490; S4value++) {
srituhobby.setPWM(servo4, 0, S4value);
delay(10);
}
for (int S4value = 490; S4value > 290; S4value--) {
srituhobby.setPWM(servo4, 0, S4value);
delay(10);
}
for (int S3value = 410; S3value > 300; S3value--) {
srituhobby.setPWM(servo3, 0, S3value);
delay(10);
}
for (int S2value = 320; S2value > 150; S2value--) {
srituhobby.setPWM(servo2, 0, S2value);
delay(10);
}
for (int S1value = 150; S1value < 340; S1value++) {
srituhobby.setPWM(servo1, 0, S1value);
delay(10);
}
}
Code explanation
Firstly, library files are included.
#include <AFMotor.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Secondly, objects are created for these libraries.
Adafruit_PWMServoDriver srituhobby = Adafruit_PWMServoDriver();
AF_DCMotor motor1(2);
AF_DCMotor motor2(3);
Thirdly, the sensor pins and servo motor pins are defined.
#define Echo A0
#define Trig A1
#define S1 A2
#define S2 A3
#define Speed 150
#define servo1 0
#define servo2 1
#define servo3 2
#define servo4 3
In the setup function,
void setup() {
//The sensor pins are set as input and output pins
pinMode(S1, INPUT);
pinMode(S2, INPUT);
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
//The servo motor library is begin
srituhobby.begin();
srituhobby.setPWMFreq(60);
//This code sets the robot arm start point
srituhobby.setPWM(servo1, 0, 340);
srituhobby.setPWM(servo2, 0, 150);
srituhobby.setPWM(servo3, 0, 300);
srituhobby.setPWM(servo4, 0, 290);
//Sets the gear motor speeds
motor1.setSpeed(Speed);
motor2.setSpeed(Speed);
}
In the loop function,
void loop() {
//Gets the distance from the ultrasonic sensor function
int distance = obstacle();
//Distance values is checked using the IF condition. If the distance value is less than or equal to 9, the robot is stops and the robot arm function is called
if (distance <= 9) {
motor1.run(RELEASE);
motor2.run(RELEASE);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
delay(100);
motor1.run(RELEASE);
motor2.run(RELEASE);
delay(100);
robotArm();
delay(100);
motor1.run(FORWARD);
motor2.run(FORWARD);
delay(100);
//Otherwise, the line-following function is called
} else {
linefollower();
}
}
This is the line-follower function.
void linefollower() {
//Gets the sensor values
bool value1 = digitalRead(S1);
bool value2 = digitalRead(S2);
//The sensor values are checked using the IF condition. If the values of both sensors are 0, the robot moves forward.
if (value1 == 0 && value2 == 0) {
motor1.run(FORWARD);
motor2.run(FORWARD);
//If the values of both sensors are 1, the robot is stops.
} else if (value1 == 1 && value2 == 1) {
motor1.run(RELEASE);
motor2.run(RELEASE);
//If the left sensor value is 1, the robot turns to the right.
} else if (value1 == 1 && value2 == 0) {
motor1.run(BACKWARD);
motor2.run(FORWARD);
//If the right sensor value is 1, the robot turns to the left.
} else if (value1 == 0 && value2 == 1) {
motor1.run(FORWARD);
motor2.run(BACKWARD);
}
}
This is the ultrasonic sensor function.
int obstacle() {
//This code sends a pulse
digitalWrite(Trig, LOW);
delayMicroseconds(4);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
//Gets the time as the distance
long t = pulseIn(Echo, HIGH);
//This code calculate the distance using that time
int cm = t / 29 / 2;
//Returns this distance to the loop function
return cm;
}
This is the robot arm function. You can change the following values as you like.
void robotArm() {
for (int S4value = 290; S4value <= 490; S4value++) {
srituhobby.setPWM(servo4, 0, S4value);
delay(10);
}
for (int S3value = 300; S3value <= 450; S3value++) {
srituhobby.setPWM(servo3, 0, S3value);
delay(10);
}
for (int S2value = 150; S2value <= 190; S2value++) {
srituhobby.setPWM(servo2, 0, S2value);
delay(10);
}
for (int S4value = 490; S4value > 290; S4value--) {
srituhobby.setPWM(servo4, 0, S4value);
delay(10);
}
for (int S3value = 450; S3value > 300; S3value--) {
srituhobby.setPWM(servo3, 0, S3value);
delay(10);
}
for (int S2value = 190; S2value <= 320; S2value++) {
srituhobby.setPWM(servo2, 0, S2value);
delay(10);
}
for (int S1value = 340; S1value >= 150; S1value--) {
srituhobby.setPWM(servo1, 0, S1value);
delay(10);
}
for (int S3value = 300; S3value <= 410; S3value++) {
srituhobby.setPWM(servo3, 0, S3value);
delay(10);
}
for (int S4value = 290; S4value <= 490; S4value++) {
srituhobby.setPWM(servo4, 0, S4value);
delay(10);
}
for (int S4value = 490; S4value > 290; S4value--) {
srituhobby.setPWM(servo4, 0, S4value);
delay(10);
}
for (int S3value = 410; S3value > 300; S3value--) {
srituhobby.setPWM(servo3, 0, S3value);
delay(10);
}
for (int S2value = 320; S2value > 150; S2value--) {
srituhobby.setPWM(servo2, 0, S2value);
delay(10);
}
for (int S1value = 150; S1value < 340; S1value++) {
srituhobby.setPWM(servo1, 0, S1value);
delay(10);
}
}
Step 14
Now, select board and port. After, upload this code to the Arduino board.
Step 15
Finally, put the batteries into the battery holder and enjoy this multi-functional robot car project. The full video guide is given below. So, we will meet in the next tutorial.
How to make a DIY Multi-functional robot car using Arduino
Hi,
nice work. will the code work for a similar project using raspberry pi?
No. Thank you.
Hi dear,
i assembled a similar robot for a project but it doesn’t work accordingly. i checked wiring…no issues which means the issue might come from the code. I would like and truly appreciate if you could share the complete code to me, i’m also willing to pay for if that’s the case.
Thank you, best regards.
Please check the hardware parts
will it work with 6vdc?
Yes
Can i use 9V battery for this? Thank you
Yes of course
Can it work with 9v battery? Thank you
Yes of course