Project tutorial
Arduino 4WD Car

Arduino 4WD Car

An Arduino car project based on the RoboRoverM1 chassis.

  • 8,965 views
  • 1 comment
  • 42 respects

Components and supplies

RoboRover M1 Robot
You no need to buy hardware components listed above if you will use that chassis. Please contact with me if you want to buy my chassis or RTR robot. I cut plastic on CNC machine and assembly robots (or just chassis) by myself.
×1
L298N Motordriver
×1
Sharp GP2Y0A21YK0F Analog Distance Sensor 10-80cm
×2
51gzz5eu9pl. sx425
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Line sensor TCRT5000
Digital output
×3
DAGU DC Motor 1:48
You can use 2 or 4 motors
×2
A000066 iso both
Arduino UNO & Genuino UNO
×1
Sensor shield V5
×1

Apps and online services

Ide web
Arduino IDE
You need to install my libraries for the source code

About this project

1. INTRODUCTION

Hi, here is a project of the autonomous car using Arduino Uno.

Very simple project is demonstrating basics of the building Arduino car. For that reason I use RoboRoverM1 chassis, but you can build your own Arduino car using DIY materials. For the guide I will use my own designed robotic chassis RoboRover M1.

My own designed RoboRover M1 4 wheel-drive chassis. It is made from the ABS 4mm plastic. Please contact with me if you want to buy my chassis or RTR robot.

Robot RoboRover M1 with all electronics installed. Also I made big green cover for robot to protect all the electronics inside.

Good design! Like a robot vacuum cleaner.

2. ELECTRONICS

2.1 Sensors of the robot car

In the photo below you can see all sensors of that robotic car. I think it is enough to study basics of the robotic car movement:

3 line sensors to follow the line and study how to follow the line. Model is TCRT5000.

2 infrared distance sensors to study how to measure distance and write obstacle avoidance algorithms. Model is GP2Y0A21YK0F.

1 ultrasonic sensor which is mounted on the servo. Servo can rotate and you can make a simple distance map ahead of the robot. Model of the ultrasonic sensor is HC-SR04 and model of the servo is MG995R.

2.2 Motors of the robot car

To let robot move we need motors, so I use 4 yellow motors DAGU 1:48 (gear ratio) 5V. If you are making your own DIY car you can use 2 motors.

To control motors I use L298N motordriver, not the best one, because it uses a lot of I/O pins of the Arduino car, but it is beginners guide. Later you can upgrade car with the I2C motordriver.

2.3 Power supply of the robot car

To let robot move we need to use battery. I recommend to use 7.4V Li-Po 2S battery, I think it is the best solution for that kind of the robot car. Do not use AA or AAA batteries it is bad choice.

For the Li-Po battery you must use special charger and voltage controller. Li-Po battery worth it!

Use small voltage tester while robot is running. It will indicate you what time to start charge battery. That small thing will help you do not overdischarge battery, because if the Li-Po battery is overdischarged it will be broken!

3. WIRING

3.1 Wiring diagram of the L298N motordriver

Solder all the motors using a small capacitor and install them.

For right motor one wire connect to the OUT3 and second wire connect to the OUT4. The same way connect second right motor.

For the left motor one wire connect to the OUT2 and second wire connect to the OUT1. The same way connect second left motor.

If you made a wrong connection here, after uploading a testing program, just swipe wires where is necessary.

Make that kind of the connector. Use two-position switch (to turn on and turn off motors and servo of the robot). I think you know how to solder. Red connector is for the battery, check which kind of the connector is installed in your battery. I recommend to use T-plug connector.

Do not connect T-plug connectors together now! Make it after all connections are finished, last step!

Then install it into your car. If you use another own DIY chassis mounting can be different.

Red wire connect to the +12V leftmost plug of the 3-pin connector on the L298N motordriver.

Two black wires connect to the GND middle plug of the 3-pin connector on the L298N motordriver.

Then take 6 long 30-40cm wires and connect them to the ENA, IN1, IN2, IN3, IN4, ENB pins on the L298N motordriver.

Wire #7 connect to GND middle plug of the 3-pin connector on the L298N motordriver (the same place where you connected previous black wires).

Finally your connection must look like this.

If you want to use servo in your robot car, you must check that it is 7.4V compatible. Then you can take red wire from the servo connector and connect wire to the +12V leftmost plug of the 3-pin connector on the L298N motordriver (the same place where you connected previous red wire before).

First check voltage of the servo! If it is not compatible, just connect servo to the sensor shield without any changes. In my case servo MG995R (MG995) is 7.4V compatible, unfortunately on that photo I forgot to connect servo.

3.2 Wiring of the sensor shield of Arduino Uno

3.2.1 Motordriver

Use simple sensor shield for the Arduino Uno to connect all sensors and motordriver wires. My model is Arduino Sensor Shield V5.

Wire from the motordriver you need to connect to the digital pins on the sensor shield:

ENA to D5

IN1 to D2

IN2 to D8

ENB to D3

IN3 to D11

IN4 to D10

3.2.2 Line sensors

Line sensor is digital, but we can use analog inputs of the Arduino Uno to read digital signals. We connect GND pin of the sensor to GND pin on the shield, VCC pin of the sensor to the VCC pin on the shield 5V.

Signals of the line sensors we connect to the analog pins on the sensor shield:

Line sensor (left) to A3

Line sensor (center) to A4

Line sensor (right) to A5

3.2.3 Infrared sensors

Analog Sharp infrared distance sensors we connect the same way. Please note that in that Sharp sensors middle wire (black) is GND, left wire (red) is VCC and right wire (yellow) is signal output.

We connect GND pin of the sensor to GND pin on the shield, VCC pin of the sensor to the VCC pin on the shield 5V.

Signals of the infrared sensors we connect to the analog pins on the sensor shield:

Sensor left to A0

Sensor right to A1

3.2.4 Ultrasonic sensor and servo

Ultrasonic sensor connect to the next pins:

Connect GND pin of the sensor to GND pin on the shield, VCC pin of the sensor to the VCC pin on the shield 5V. Other pins we connect to the digital inputs on the shield.

Trig pin to the D7

Echo pin to the D4

Servo connect to the next pins:

Connect GND wire (black or brown) of the servo to GND pin on the shield.

If servo is 7.4V compatible connect VCC wire (red or orange) to the +12V leftmost plug of the 3-pin connector on the L298N motordriver. If servo is not compatible connect VCC wire of the servo to the VCC pin on the servo shield.

Signal wire (yellow, white, or orange) connect to the digital pin on the shield D9.

Congratulations! Your robot is finished!

You can plug black connector to the Arduino to turn it on.

Connect battery with the T-plug connector (or any other). Turn on motors using that switch. Now let's start coding! Hope, no fire after you turned on your electronics of the robot.

4. CODING

4.1 Install libraries

All libraries you can download from the attachments section of the project.

I created libraries for that robot to study programming in easy and fun way. I made my custom libraries for the next modules: Servo, Motordriver, Sharp infrared distance sensor, Ultrasonic distance sensor, Servo.

I made that libraries for the easy code understanding if you are not an Arduino expert, but you can avoid using that libraries. You can read comments in libraries to better understand how they work.

Before you will try to play with the code below install all the libraries. You can install them like you install all Arduino libraries.

4.2 Code samples

Here I will give you some code examples. Please read comments in the code carefully.

Code#1 Test motors

First test your motors, if they are rotating in a wrong way just swipe the wires.

Upload program, turn on Arduino Uno and turn on motors, press RESET button on the Arduino board to start code running from the very beginning.

/* Program for the robot RoboRover M1 test motors forward backward
  If motors are rotating wrong swipe wires connected to the L298N motordriver
*/
//Include all necessary libraries to control motoriver L298N
#include <MotorL298NRobotCar.h>
//Create class instances for the motordriver L298N
Motors myMotors;
//Setup all class instances and attach them to the necessary ports
void setup() {
 /* Motor digital pins are hardcoded in the library, for your reference pins connections is the next:
    Motor 1: enA = 5, in1 = 2, in2 = 8
    Motor 2: enB = 3, in3 = 11, in4 = 10 */
 myMotors.attach();
}
int speed = 0; //variable to make accleration smoothly
void loop() {
 /* How program runs:
    Motors are rotating forward during 5 seconds on maximum possible speed 255
    Motors are rotating backward during 5 seconds on maximum possible speed --255 */
 for (speed; speed < 255; speed++)
 {
   myMotors.write(speed, speed); // Rotate motors forward and gain speed smoothly using for
   delay(15); // delay to make accleration smoothly
 }
 delay(5000); //delay 5 seconds
 for (speed; speed > -255; speed--)
 {
   myMotors.write(speed, speed); // Rotate motors forward and gain speed smoothly using for
   delay(15); // delay to make accleration smoothly
 }
 delay(5000); //delay 5 seconds
}

Code#2 Test Sharp infrared distance sensors

Test infrared sensors to understand if they work properly. All information will go to the Serial port.

Upload program and open Serial port in the Arduino IDE to see the distance measurements.

/* Program for the robot test infrared distance sensors
*  using two infrared Sharp A21 distance sensors 
*  All measurements are going to the Serial port speed is 9600
*/
//Include all necessary files to control Sharp distance sensor
#include <SharpA21RobotCar.h>
//Create class instances for the Sharp distance sensors
Sharp mySharpA0;
Sharp mySharpA1;
//Setup all class instances and attach them to the necessary ports
void setup() {
 //Serial port attach on the speed 9600
 Serial.begin(9600);
 //You can choose pins from A0 to A5 for the Arduino Uno and add more distance sensors
 mySharpA0.attach(A0);
 mySharpA1.attach(A1);
}
void loop() {
 Serial.print("Infrared sensor left: ");
 Serial.println(mySharpA0.read());
 Serial.print("Infrared sensor right: ");
 Serial.println(mySharpA1.read());
 Serial.println("");
 delay(500);

Code#3 Radar ultrasonic distance sensor

This code will test ultrasonic sensor and will test rotation of the servo. It is a simple radar. Servo rotates left, right or middle position, ultrasonic sensor measures distance.

Upload program, open Serial port in Arduino IDE, turn on Arduino Uno and turn on motors, press RESET button on the Arduino board to start code running from the very beginning.

/* Program for the robot test ultrasonic distance sensors
   All measurements are going to the Serial port speed is 9600
*/
//Include all necessary files to control ultrasonic distance sensor and rotate servo
#include <HCSR04RobotCar.h>
#include <ServoRobotCar.h>
//Create class instance for the ultrasonic distance sensor
Ultrasonic myUltrasonic;
//Create class instance for the servo
ServoMotor myServo;
//Setup all class instances and attach them to the necessary ports
void setup() {
 //Serial port attach on the speed 9600
 Serial.begin(9600);
 //Attach sensor and servo
 //Pins for the ultrasonic sensor and servo are hardcoded in the library
 myUltrasonic.attach();
 myServo.attach();
 //Set servo in the middle position
 myServo.write(90);
 //Wait 2 seconds before program starts
 delay(2000);
}
void loop() {
 myServo.write(90); //Set servo in the middle position
 Serial.print("Distance center ");
 Serial.println(myUltrasonic.read());
 delay(1000); //Wait 1 second
 myServo.write(130);  //Turn servo left
 Serial.print("Distance left ");
 Serial.println(myUltrasonic.read());
 delay(1000); //Wait 1 second
 myServo.write(50); //Turn servo right
 Serial.print("Distance right ");
 Serial.println(myUltrasonic.read());
 delay(1000); //Wait 1 second
}

Code#4 Test line sensors

/* Program for the robot test line sensors
   using three line sensors TCRT5000
   All measurements are going to the Serial port speed is 9600
*/
//Include all necessary files to control line sensors
#include <LineSensorRobotCar.h>
//Create class instances for the line sensors
LineSensor myLineSensorA3;
LineSensor myLineSensorA4;
LineSensor myLineSensorA5;
//Setup all class instances and attach them to the necessary ports
void setup() {
 //Serial port attach on the speed 9600
 Serial.begin(9600);
 //You can choose pins from A0 to A5 for the Arduino Uno and add more sensors
 myLineSensorA3.attach(A3);
 myLineSensorA4.attach(A4);
 myLineSensorA5.attach(A5);
}
void loop() {
 Serial.print("Line sensor left: ");
 Serial.println(myLineSensorA3.read());
 Serial.print("Line sensor center: ");
 Serial.println(myLineSensorA4.read());
 Serial.print("Line sensor right: ");
 Serial.println(myLineSensorA5.read());
 Serial.println("");
 delay(500);
}

Code#5 Robot moving obstacle avoidance using infrared distance sensors

This program will allow robot to avoid obstacles using infrared distance sensors.

Upload program, turn on Arduino Uno and turn on motors, press RESET button on the Arduino board to start code running from the very beginning. Robot will start moving and avoid obstacles!

/* Program for the two-wheeled robot and obstacle avoidance (with backward)
 using two infrared Sharp A21 distance sensors. */
//Include all libraries to control Sharp distance sensor, and motordriver L298N
#include <SharpA21RobotCar.h>
#include <MotorL298NRobotCar.h>
//Create class instances for the Sharp distance sensors and motordriver L298N
Sharp mySharpA0;
Sharp mySharpA1;
Motors myMotors;
//Setup all class instances and attach them to the necessary ports
void setup() {
Serial.begin(9600);
//You can choose pins from A0 to A5 for the Arduino Uno and add more distance sensors
mySharpA0.attach(A0);
mySharpA1.attach(A1);
/* Motor digital pins are hardcoded in the library, for your reference pins connections is the next:
   Motor 1: enA = 5, in1 = 2, in2 = 8
   Motor 2: enB = 3, in3 = 11, in4 = 10 */
myMotors.attach();
}
void loop() {
/* How program runs:
   If distance from the left sensor less than 60cm (true)
   or distance from the right sensor less than 60cm (true), 
   we are going to the next enclosured "if" conditional statement.
   If distance from both sensors is less than 35cm (true), robot is going backward.
   Else we are going to the next enclosured "if" conditional statement.
   If distance from the left sensor is less or equal to the distance from the right sensor, 
   robot turns to the right.
   If distance from the left sensor is bigger than right sensor, robot turns left.
   If distance from the both sensors more than 60cm, robot is running straight forward. */
if (mySharpA0.read() <= 60 || mySharpA1.read() <= 60) {
  if (mySharpA0.read() <= 35 && mySharpA1.read() <= 35) {
    //Robot is going backward myMotors.write(LEFT MOTOR, RIGHT MOTOR)
    myMotors.write(-220, -220); 
  } else {
    if (mySharpA0.read() <= mySharpA1.read()) {
      myMotors.write(255, -100); //Robot turns right
    }
    if (mySharpA0.read() > mySharpA1.read()) {
      myMotors.write(-100, 255); //Robot turns left
    }
  }
} else {
  myMotors.write(220, 220); //Robot is running straight forward
}
}

Code#6 Robot moving Line following

This program will allow robot to follow the line using line sensors. Make a small track with a black line.

Upload program, turn on Arduino Uno and turn on motors, press RESET button on the Arduino board to start code running from the very beginning. Robot will start following the black line if it is detected by line sensors.

 /* Program for the robot line following
   using three line sensors TCRT5000
*/
//Include all necessary files to control line sensors and motordriver
#include <LineSensorRobotCar.h>
#include <MotorL298NRobotCar.h>
//Create class instances for the line sensors and motordriver
LineSensor myLineSensorA3;
LineSensor myLineSensorA4;
LineSensor myLineSensorA5;
Motors myMotors;
//Setup all class instances and attach them to the necessary ports
void setup() {
 //Serial port attach on the speed 9600
 Serial.begin(9600);
 //You can choose pins from A0 to A5 for the Arduino Uno and add more sensors
 myLineSensorA3.attach(A3);
 myLineSensorA4.attach(A4);
 myLineSensorA5.attach(A5);
 /* Motor digital pins are hardcoded in the library, for your reference pins connections is the next:
   Motor 1: enA = 5, in1 = 2, in2 = 8
   Motor 2: enB = 3, in3 = 11, in4 = 10 */
myMotors.attach();
}
void loop() {
 /* How program runs:
    If value from the center sensor (A4) is 0,
    it means black line, robot is going forward.
    Else, if value from the left sensor (A3) is 0 (black line detected),
    while center sensor is on white surface, robot turns left.
    Else, if value from the right sensor (A5) is 0 (black line detected),
    while center sensor is on white surface, robot turns right. */
 if (myLineSensorA4.read() == 0) {
   myMotors.write(120, 120); //Robot is going forward
 } else {
   if (myLineSensorA3.read() == 0) {
     while (myLineSensorA4.read() == 1) {
       myMotors.write(-180, 180);  //Robot turns left
     }
   } else {
     if (myLineSensorA5.read() == 0) {
       while (myLineSensorA4.read() == 1) {
         myMotors.write(180, -180); //Robot turns right
       }
     }
   }
 }
}

Well done! Hope everything works fine!

If you have any questions, please write me, I will help you to set up your robot!

Code

Libraries v1Arduino
No preview (download only).
Programs v1Arduino
No preview (download only).

Comments

Similar projects you might like

Arduino 4WD RC Car

Project tutorial by Andriy Baranov

  • 13,151 views
  • 3 comments
  • 44 respects

Smartphone Controlled Arduino 4WD Robot Car

Project in progress by Andriy Baranov

  • 53,240 views
  • 43 comments
  • 98 respects

Turn your RC Car to Bluetooth RC car

Project tutorial by Prajjwal Nag

  • 12,108 views
  • 1 comment
  • 12 respects

RC Car to BT Car Controlled with Blynk

Project tutorial by Team Daxes Hacks

  • 3,598 views
  • 2 comments
  • 6 respects

Bluetooth Controlled Car

Project tutorial by JANAK13

  • 4,742 views
  • 8 comments
  • 25 respects
Add projectSign up / Login