Arduino Remote Control CAR.


 Arduino Remote Control Car

Remote controlled toys have always captivated me. Thus, I decided to make a RC car of my own....😎.  It is pretty interesting to make, but a  look at the basics first......

Item list - These can be easily picked from Amazon or other online stores 

  a. L298N Motor driver   -   Link 

  b. Joy stick                              -   Link

 c. Arduino uno                      -   Link                       

d. 9v battery                             -  Link   

e. Motor and wheels         -  Link  

f. Jumper wires                   -    Link 

g. Mini breadboard          -  Link

Principle.



The two main functions on which our joy stick controlled car is based are the  :-                   a. Forward & rearward controls.                                                                                                                        b. Right & left directional controls.                                                                                       

 These two functions would determine the complete 360 degree movement of the vehicle.     The first ☝( forward and rearward) function is achieved by changing the direction of the motors. The clockwise rotation of the motors would move the car forward and vice versa. To be able to execute this directional change in the motor while moving we must change the direction of current flowing through the motor spontaneously while in motion itself. This can be done using the simple principle of a H-bridge.


An H - bridge comprises of 4 switches arranged in an ‘H’ like pattern with the motor in the centre.  In a H - bridge when two opposite switches are activated(closed) and the other pair of the opposite switches deactivated(open) then the motor receives current flow from one direction where as when the connections are reversed then the motor starts to receive current from the opposing direction and its rotation direction reverses.

The second✌(right and left) function of movement of the car, is achieved by regulating the speed of each of the motors. For the car to go right - the right motor must be slowed down and the speed of the left motor must be increased. Similarly, for the car to go left - the left motor must be slowed and the right motor must go faster.

Hence to achieve  both the 1st & 2nd function. We will be using the l298n motor driver which has in it an H-bridge circuit and regulates the motor speed. there  

Now to control the car with the joy stick, the component will have to be interfaced with our microcontroller(Arduino). 

Also the motor driver to which the motor are connected must be interfaced with the Arduino. Thus when we would move the joystick, the microcontroller(Arduino) would read the input and then command the motor driver to function accordingly. 

The microcontroller would also be required to be programmed for the output command it will give to the motor driver. For ever action on the joystick the microcontroller needs to be told the kind of output it must deliver. 

 Schematic :                                                                          


     

Code. Here is the code....

  1. #define enA 9
  2. #define in1 4
  3. #define in2 5
  4. #define enB 10
  5. #define in3 6
  6. #define in4 7
  7. int motorSpeedA = 0;
  8. int motorSpeedB = 0;
  9. void setup() {
  10. pinMode(enA, OUTPUT);
  11. pinMode(enB, OUTPUT);
  12. pinMode(in1, OUTPUT);
  13. pinMode(in2, OUTPUT);
  14. pinMode(in3, OUTPUT);
  15. pinMode(in4, OUTPUT);
  16. }
  17. void loop() {
  18. int xAxis = analogRead(A0); // Read Joysticks X-axis
  19. int yAxis = analogRead(A1); // Read Joysticks Y-axis
  20. // Y-axis used for forward and backward control
  21. if (yAxis < 470) {
  22. // Set Motor A backward
  23. digitalWrite(in1, HIGH);
  24. digitalWrite(in2, LOW);
  25. // Set Motor B backward
  26. digitalWrite(in3, HIGH);
  27. digitalWrite(in4, LOW);
  28. // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
  29. motorSpeedA = map(yAxis, 470, 0, 0, 255);
  30. motorSpeedB = map(yAxis, 470, 0, 0, 255);
  31. }
  32. else if (yAxis > 550) {
  33. // Set Motor A forward
  34. digitalWrite(in1, LOW);
  35. digitalWrite(in2, HIGH);
  36. // Set Motor B forward
  37. digitalWrite(in3, LOW);
  38. digitalWrite(in4, HIGH);
  39. // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
  40. motorSpeedA = map(yAxis, 550, 1023, 0, 255);
  41. motorSpeedB = map(yAxis, 550, 1023, 0, 255);
  42. }
  43. // If joystick stays in middle the motors are not moving
  44. else {
  45. motorSpeedA = 0;
  46. motorSpeedB = 0;
  47. }
  48. // X-axis used for left and right control
  49. if (xAxis < 470) {
  50. // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
  51. int xMapped = map(xAxis, 470, 0, 0, 255);
  52. // Move to left - decrease left motor speed, increase right motor speed
  53. motorSpeedA = motorSpeedA - xMapped;
  54. motorSpeedB = motorSpeedB + xMapped;
  55. // Confine the range from 0 to 255
  56. if (motorSpeedA < 0) {
  57. motorSpeedA = 0;
  58. }
  59. if (motorSpeedB > 255) {
  60. motorSpeedB = 255;
  61. }
  62. }
  63. if (xAxis > 550) {
  64. // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
  65. int xMapped = map(xAxis, 550, 1023, 0, 255);
  66. // Move right - decrease right motor speed, increase left motor speed
  67. motorSpeedA = motorSpeedA + xMapped;
  68. motorSpeedB = motorSpeedB - xMapped;
  69. // Confine the range from 0 to 255
  70. if (motorSpeedA > 255) {
  71. motorSpeedA = 255;
  72. }
  73. if (motorSpeedB < 0) {
  74. motorSpeedB = 0;
  75. }
  76. }
  77. // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
  78. if (motorSpeedA < 70) {
  79. motorSpeedA = 0;
  80. }
  81. if (motorSpeedB < 70) {
  82. motorSpeedB = 0;
  83. }
  84. analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  85. analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
  86. }


















                                 




Comments

Popular posts from this blog

Measuring The Speed Of Light

Hand Battery

PASSWORD PROTECTED DOOR LOCK SECURITY SYSTEM

STORY ENGINEERING - A KIND GESTURE

ARDUINO OBSTACLE AVOIDING CAR - A TUTORIAL GUIDE

Smartphone Controlled RC Car

MORSE CODE - HARNESSING ELECTRICITY TO COMMUNICATE