Sunday 1 December 2019

4WD Arduino Car Project

This is an excellent project which demonstrates how to control a 4WD Arduino Car.

However this car cannot spin but it is easy to modify the code to allow it to spin instead of just turning left and right.
 
This Arduino car has a better turning radius than cars that have steering wheels because it can spin.
If the left wheels are run forward and the right wheels are run reversed, the car will spin.

It allows the car to manoeuvre in tight spaces, but when spinning it cannot move forward at the same time.

To left or right while moving forward, just move the left wheels at a different speed from that of the right wheels.

It means that there is no need to use 4 speed controllers. Only 2 speed controllers are required. One speed controller for the left and another for the right wheels.

If we can control the speed of 3 wheels independently, we can spin the car around one wheel. I do not see much advantage out of it. 

Building a 4WD Autonomous Car with Arduino

In this tutorial you will learn to make a 4WD robotic car with built-in obstacle avoidance. This project involves building a 4WD car with an ultrasonic sensor that can detect nearby objects and change its direction to avoid these objects. The ultrasonic sensor will be attached to a servo motor which is constantly scanning left and right looking for objects in its way.
4WD autonomous robotic car
4WD autonomous robotic car
You can extend this project by adding more sensors such as ultrasonic sensor to the rear so that the car doesn’t reverse into objects, speed detection sensors to determine speed, line following sensors, and LED brake lights. You could even add light sensors and LEDs to turn headlights on in low-light conditions or a Bluetooth connection to control the car through a mobile app.

Parts required

Here is what you will need:
  • 1x Arduino Uno board
  • 1x 4WD Robotic Car kit (4 wheels, 4 motors, chassis, AA battery enclosure, screws)
  • 6x AA batteries
  • 1x 9V battery
  • 1x 9V battery power cable barrel jack connector
  • 1x L298N Motor Module
  • 1x Arduino Sensor Shield v5.0
  • 1x HC-SR04 Ultrasonic Sensor
  • 1x Servo motor (any small servo motor will do but if you don’t have one you can leave it out)
  • Assorted colour wires:
    • 4x female-to-female wires for Ultrasonic Sensor
    • 8x female-to-female wires for the Motor Module to Sensor Shield
    • 8x bare-ended wires to go from the four motors to the Motor Module
    • 2x bare-ended wires to go from the Motor Module to Sensor Shield
    • 3x wires for the servo motor with female end attaching to Sensor Shield (should already be included with the servo motor)
    • 2x bare-ended wires to go from AA Battery enclosure to the motor Module (should already be included with the battery enclosure)
  • Arduino IDE software – free at http://www.arduino.cc
  • 1x USB cable (A male plug to B male plug)
  • A computer to program with
Note: Make sure you get a range of different colours for the wires (eg. red for voltage, black for ground, etc.). Some items such as the battery enclosure, motors and servo motor may come with wires so you may not need to purchase these separately. Some motor modules or sensor shields may use different pins (eg. male or female or just bare-ended wires).
Below are images of the main components you need (apart from the wiring and batteries) – the different robot parts, shields, modules and sensors.
Click for larger image
Click for larger image

Assembling the robotic car

The first thing to do is to assemble all the car chassis parts and attach the motors to the wheels. This is explained in detail in the video below.
Video Player
00:00
15:59

Wiring it up

Now that the chassis is assembled and the wheels and motors are attached, you will need to wire up all of the components. You can watch the video below or scroll down for the step-by-step instructions and wiring diagram.
Video Player
00:00
18:38
  1. Connect the red wire (voltage) from the battery enclosure to VCC on the L298N Motor Module.
  2. Connect the black wire (ground) from the battery enclosure to GND on the Motor Module.
  3. Connect the red wire from Motor A1 (rear left) and the red wire from Motor A2 (front left) to OUT1 on the Motor Module (both wires can be twisted together and will go into OUT1 – the wiring diagram further down this page shows how to do this).
  4. Connect the black wire from Motor A1 (rear left) and the black wire from Motor A2 (front left) to OUT2 on the Motor Module.
  5. Connect the red wire from Motor B1 (rear right) and the red wire from Motor B2 (front right) to OUT3 on the Motor Module.
  6. Connect the black wire from Motor B1 (rear right) and the black wire from Motor B2 (front right) to OUT4 on the Motor Module.
  7. Connect a female-to-female end wire from ENA (Engine A) on the Motor Module to signal pin 1 on the Sensor Shield (the #2 pin in the S row. There are three rows – S stands for Signal, V for Voltage and G for Ground).
  8. Connect a female-to-female end wire from 5V (next to ENA) on the Motor Module to voltage pin 1 on the Sensor Shield (it doesn’t really matter which pin for voltage as long as it is in the V row).
  9. Connect a female-to-female end wire from IN1 on the Motor Module to signal pin 2 on the Sensor Shield.
  10. Connect a female-to-female end wire from IN2 on the Motor Module to signal pin 3 on the Sensor Shield.
  11. Connect a female-to-female end wire from IN3 on the Motor Module to signal pin 4 on the Sensor Shield.
  12. Connect a female-to-female end wire from IN4 on the Motor Module to signal pin 5 on the Sensor Shield.
  13. Connect a female-to-female end wire from 5V (next to ENB) on the Motor Module to voltage pin 6 on the Sensor Shield (it doesn’t really matter which pin for voltage as long as it is in the V row).
  14. Connect a female-to-female end wire from ENB (Engine B) on the Motor Module to signal pin 6 on the Sensor Shield.
  15. Connect the signal wire from the servo motor to signal (S) pin 7 on the Sensor Shield, the voltage wire from the servo motor to the voltage (V) pin 7 on the Sensor Shield, and the ground wire from the servo motor to the GND (G) pin 7 on the Sensor Shield.
  16. Attach the Ultrasonic Sensor to the servo motor and then attach the servo motor to the front of the car so that it can scan left and right looking for objects in its path.
    You can use sticky tape, blu-tack, glue or a 3D printed/laser cut mount or whatever you like to attach everything, as long as everything is firmly mounted.
  17. Use female-to-female end plugs to connect the Ultrasonic sensor to the Sensor Shield. Firstly, attach the TRIG pin from the Ultrasonic sensor to signal (S) pin 8 on the Sensor Shield. Attach the ECHO pin from the ultrasonic sensor to signal (S) pin 9 on the Sensor Shield. Attach the VCC pin from the Ultrasonic Sensor to the voltage (V) pin 10 on the Sensor Shield. Lastly, attach the GND pin from the Ultrasonic Sensor to the GND (G) pin 11 on the Sensor Shield.
  18. Now attach the Sensor Shield on top of the Arduino Uno board making sure to not bend any pins. Connect a 9V Battery Barrel Jack Connector to a 9V battery and then plug this in to the Arduino’s barrel jack (see diagram below).
  19. That’s it! Now all you need is batteries and the code!
Please note: There are several variations of the sensors and modules and several different ways to wire this up. For example, you can add a charging component to charge batteries and you can also add different switches. There may be other better methods to use, but I have gone for the simplest way I know of to set this up.

Wiring diagram

Click for larger image
Click for larger image

Writing the code

Here is the code to get your robot moving. The code is written in C language and is written in the Arduino IDE software. The code for a robot with a servo motor and the code for a robot without a servo motor is shown below. If you don’t have an ultrasonic sensor and just want to use Bluetooth control via a phone app, then you can just extract the parts of the code that you need and then add the Bluetooth code.

Code to use with a servo motor

  1. // Setup the servo motor
  2. #include <Servo.h>
  3. Servo myservo;
  4. int servposnum = 0;
  5. int servpos = 0;

  6. // Setup Motor A (front and rear) pins
  7. int enableA = 1;
  8. int pinA1 = 3;
  9. int pinA2 = 2;

  10. // Setup Motor B (front and rear) pins
  11. int enableB = 6;
  12. int pinB1 = 5;
  13. int pinB2 = 4;

  14. // Setup Ultrasonic Sensor pins
  15. #define trigPin 8
  16. #define echoPin 9


  17. void setup() {
  18. // The setup code goes here and runs once only
  19. // Configure the pin modes for each drive motor
  20. pinMode (enableA, OUTPUT);
  21. pinMode (pinA1, OUTPUT);
  22. pinMode (pinA2, OUTPUT);

  23. pinMode (enableB, OUTPUT);
  24. pinMode (pinB1, OUTPUT);
  25. pinMode (pinB2, OUTPUT);

  26. // Configure the pin modes for the Ultrasonic Sensor
  27. pinMode(trigPin, OUTPUT);
  28. pinMode(echoPin, INPUT);

  29. // Turn pin into servo driver. Calls pinMode. Returns 0 on failure.
  30. myservo.attach(7);
  31. }

  32. void loop() {
  33. // Main code goes here and will run repeatedly:

  34. car(); // function keeps moving car forward while distance > 15cm
  35. avoid(); // function makes car go back, turn slightly right to move forward in new direction
  36. }

  37. // Create motor functions
  38. void motorAforward() {
  39. digitalWrite (pinA1, HIGH);
  40. digitalWrite (pinA2, LOW);
  41. }
  42. void motorBforward() {
  43. digitalWrite (pinB1, LOW);
  44. digitalWrite (pinB2, HIGH);
  45. }
  46. void motorAbackward() {
  47. digitalWrite (pinA1, LOW);
  48. digitalWrite (pinA2, HIGH);
  49. }
  50. void motorBbackward() {
  51. digitalWrite (pinB1, HIGH);
  52. digitalWrite (pinB2, LOW);
  53. }
  54. void motorAstop() {
  55. digitalWrite (pinA1, HIGH);
  56. digitalWrite (pinA2, HIGH);
  57. }
  58. void motorBstop() {
  59. digitalWrite (pinB1, HIGH);
  60. digitalWrite (pinB2, HIGH);
  61. }
  62. void motorAcoast() {
  63. digitalWrite (pinA1, LOW);
  64. digitalWrite (pinA2, LOW);
  65. }
  66. void motorBcoast() {
  67. digitalWrite (pinB1, LOW);
  68. digitalWrite (pinB2, LOW);
  69. }
  70. void motorAon() {
  71. digitalWrite (enableA, HIGH);
  72. }
  73. void motorBon() {
  74. digitalWrite (enableB, HIGH);
  75. }
  76. void motorAoff() {
  77. digitalWrite (enableA, LOW);
  78. }
  79. void motorBoff() {
  80. digitalWrite (enableB, LOW);
  81. }

  82. // Setup movement functions
  83. void forward (int duration) {
  84. motorAforward();
  85. motorBforward();
  86. delay (duration);
  87. }
  88. void backward (int duration) {
  89. motorAbackward();
  90. motorBbackward();
  91. delay (duration);
  92. }
  93. void right (int duration) {
  94. motorAbackward();
  95. motorBforward();
  96. delay (duration);
  97. }
  98. void left (int duration) {
  99. motorAforward();
  100. motorBbackward();
  101. delay (duration);
  102. }
  103. void coast (int duration) {
  104. motorAcoast();
  105. motorBcoast();
  106. delay (duration);
  107. }
  108. void breakRobot (int duration) {
  109. motorAstop();
  110. motorBstop();
  111. delay (duration);
  112. }
  113. void disableMotors() {
  114. motorAoff();
  115. motorBoff();
  116. }
  117. void enableMotors() {
  118. motorAon();
  119. motorBon();
  120. }

  121. // Setup Ultrasonic Sensor distance measuring
  122. int distance() {
  123. int duration, distance;
  124. digitalWrite(trigPin, HIGH);
  125. delayMicroseconds(1000);
  126. digitalWrite(trigPin, LOW);
  127. duration = pulseIn(echoPin, HIGH);
  128. distance = (duration/2) / 29.1;
  129. return distance;
  130. }

  131. // Setup the main car function
  132. void car() {
  133. int distance_0;
  134. distance_0 = distance();
  135. // Keep moving forward in a straight line while distance of objects > 15cm
  136. while(distance_0 > 15)
  137. {
  138. // Keep moving servo motor back and forth to scan surroundings
  139. // This allows the ultrasonic sensor to see more to its left and right
  140. if(servposnum == 0)
  141. {
  142. myservo.writeMicroseconds (1900);
  143. servposnum = 1;
  144. delay(100);
  145. }
  146. else if(servposnum == 1)
  147. {
  148. myservo.writeMicroseconds (2200);
  149. servposnum = 2;
  150. delay(100);
  151. }
  152. else if(servposnum == 2)
  153. {
  154. myservo.writeMicroseconds (1900);
  155. servposnum = 3;
  156. delay(100);
  157. }
  158. else if(servposnum == 3)
  159. {
  160. myservo.writeMicroseconds (1600);
  161. servposnum = 1;
  162. delay(100);
  163. }
  164. motorAon();
  165. motorBon();
  166. forward(1);
  167. distance_0 = distance();
  168. }
  169. breakRobot(0);

  170. }
  171. void avoid()
  172. {
  173. // Go back and turn slightly right to move car in new direction if object detected < 15cm away
  174. backward(500);
  175. right(360);
  176. }

Code to use without a servo motor

  1. // Setup Motor A (front and rear) pins
  2. int enableA = 1;
  3. int pinA1 = 3;
  4. int pinA2 = 2;

  5. // Setup Motor B (front and rear) pins
  6. int enableB = 6;
  7. int pinB1 = 5;
  8. int pinB2 = 4;

  9. // Setup Ultrasonic Sensor pins
  10. #define trigPin 8
  11. #define echoPin 9

  12. void setup() {
  13. // The setup code goes here and runs once only
  14. // Configure the pin modes for each drive motor
  15. pinMode (enableA, OUTPUT);
  16. pinMode (pinA1, OUTPUT);
  17. pinMode (pinA2, OUTPUT);

  18. pinMode (enableB, OUTPUT);
  19. pinMode (pinB1, OUTPUT);
  20. pinMode (pinB2, OUTPUT);

  21. // Configure the pin modes for the Ultrasonic Sensor
  22. pinMode(trigPin, OUTPUT);
  23. pinMode(echoPin, INPUT);
  24. }

  25. void loop() {
  26. // Main code goes here and will run repeatedly:
  27. car(); // function keeps moving car forward while distance of objects in front are > 15cm away
  28. avoid(); // function makes car go back, turn slightly right to move forward in new direction
  29. }

  30. // Create motor functions
  31. void motorAforward() {
  32. digitalWrite (pinA1, HIGH);
  33. digitalWrite (pinA2, LOW);
  34. }
  35. void motorBforward() {
  36. digitalWrite (pinB1, LOW);
  37. digitalWrite (pinB2, HIGH);
  38. }
  39. void motorAbackward() {
  40. digitalWrite (pinA1, LOW);
  41. digitalWrite (pinA2, HIGH);
  42. }
  43. void motorBbackward() {
  44. digitalWrite (pinB1, HIGH);
  45. digitalWrite (pinB2, LOW);
  46. }
  47. void motorAstop() {
  48. digitalWrite (pinA1, HIGH);
  49. digitalWrite (pinA2, HIGH);
  50. }
  51. void motorBstop() {
  52. digitalWrite (pinB1, HIGH);
  53. digitalWrite (pinB2, HIGH);
  54. }
  55. void motorAcoast() {
  56. digitalWrite (pinA1, LOW);
  57. digitalWrite (pinA2, LOW);
  58. }
  59. void motorBcoast() {
  60. digitalWrite (pinB1, LOW);
  61. digitalWrite (pinB2, LOW);
  62. }
  63. void motorAon() {
  64. digitalWrite (enableA, HIGH);
  65. }
  66. void motorBon() {
  67. digitalWrite (enableB, HIGH);
  68. }
  69. void motorAoff() {
  70. digitalWrite (enableA, LOW);
  71. }
  72. void motorBoff() {
  73. digitalWrite (enableB, LOW);
  74. }

  75. // Setup movement functions
  76. void forward (int duration) {
  77. motorAforward();
  78. motorBforward();
  79. delay (duration);
  80. }
  81. void backward (int duration) {
  82. motorAbackward();
  83. motorBbackward();
  84. delay (duration);
  85. }
  86. void right (int duration) {
  87. motorAbackward();
  88. motorBforward();
  89. delay (duration);
  90. }
  91. void left (int duration) {
  92. motorAforward();
  93. motorBbackward();
  94. delay (duration);
  95. }
  96. void coast (int duration) {
  97. motorAcoast();
  98. motorBcoast();
  99. delay (duration);
  100. }
  101. void breakRobot (int duration) {
  102. motorAstop();
  103. motorBstop();
  104. delay (duration);
  105. }
  106. void disableMotors() {
  107. motorAoff();
  108. motorBoff();
  109. }
  110. void enableMotors() {
  111. motorAon();
  112. motorBon();
  113. }

  114. // Setup Ultrasonic Sensor distance measuring
  115. int distance() {
  116. int duration, distance;
  117. digitalWrite(trigPin, HIGH);
  118. delayMicroseconds(1000);
  119. digitalWrite(trigPin, LOW);
  120. duration = pulseIn(echoPin, HIGH);
  121. distance = (duration/2) / 29.1;
  122. return distance;
  123. }

  124. // Setup the main car function
  125. void car() {
  126. int distance_0;
  127. distance_0 = distance();
  128. // Keep moving forward in a straight line while distance of objects in front > 15cm away
  129. while(distance_0 > 15)
  130. {
  131. motorAon();
  132. motorBon();
  133. forward(1);
  134. distance_0 = distance();
  135. }
  136. breakRobot(0);

  137. }

  138. // Go back and turn slightly right to move car in new direction
  139. // This function only runs if an obstacle within 15cm is detected
  140. void avoid()
  141. {
  142. backward(500);
  143. right(360);
  144. }

Finishing up

Once you have the code you will need to verify it and then upload it to your Arduino Uno board. Make sure you have either the Arduino disconnected from the Sensor Shield or all batteries disconnected when uploading the code to prevent any errors while uploading. Make sure to also arrange the different components carefully in the chassis to prevent any short circuits.
And that’s it! There is so much more that can be done with this project. Try extending your project by adding the following features:
  • Line following
  • Low-light activated headlights (using light sensors and LEDs)
  • Brake lights
  • Speed measuring
  • Bluetooth/mobile app control
  • More ultrasonic sensors to avoid reversing into objects or falling off edges
The video below shows a demo of the 4WD autonomous car using an Arduino and the ultrasonic sensor attache to a servo motor. Six AA batteries power the motors while the Arduino board is powered using a separate 9V battery.
Video Player
00:00
00:21

More photos