top of page

All Blogs

Arduino Robot Car Control Using HC-05 Bluetooth Module

Writer's picture: Aryan AryaAryan Arya


We will start with the Bluetooth communication, and for that purpose we need two HC-05 Bluetooth modules which need to be configured as master and slave devices.



We can easily do that by using AT commands, and I set the joystick to be a master and the Arduino robot car to be a slave. Here’s the complete circuit schematic for this example:

📷

You can get the components needed for this example from the links below:

*Please note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Source Code

We will use the same code from the previous tutorial, where we control the Arduino robot car directly using the joystick, and we will make some modifications to it.

HC-05 Master code:

  1. /*

  2. Arduino Robot Car Wireless Control using the HC-05 Bluetooth

  3. == MASTER DEVICE - Joystick ==

  4. by Dejan Nedelkovski, www.HowToMechatronics.com

  5. */

  6. int xAxis, yAxis;

  7. void setup() {

  8. Serial.begin(38400); // Default communication rate of the Bluetooth module

  9. }

  10. void loop() {

  11. xAxis = analogRead(A0); // Read Joysticks X-axis

  12. yAxis = analogRead(A1); // Read Joysticks Y-axis

  13. // Send the values via the serial port to the slave HC-05 Bluetooth device

  14. Serial.write(xAxis/4); // Dividing by 4 for converting from 0 - 1023 to 0 - 256, (1 byte) range

  15. Serial.write(yAxis/4);

  16. delay(20);

  17. }

The code at the master device, or the joystick is quite simple. We just need to read the X and Y values of the joystick, which actually regulate the speed of the motors, and send them via the serial port to the slave HC-05 Bluetooth device. We can note here that the analog values of joystick from 0 to 1023 are converted into a values from 0 to 255 by diving them by 4.

We do this because that range, from 0 to 255, can be sent, over the Bluetooth device, as 1 byte which is easier to be accepted on the other side, or at the Arduino robot car.

So here, if the serial has received the 2 bytes, the X and Y values, using the Serial.read() function we will read both of them.

  1. // Code from the Arduino Robot Car

  2. // Read the incoming data from the Joystick, or the master Bluetooth device

  3. while (Serial.available() >= 2) {

  4. x = Serial.read();

  5. delay(10);

  6. y = Serial.read();

  7. }

Now we just have to convert the values back to the range from 0 to 1023, suitable for the motor control code below, which we already explained how it works in the previous video.

  1. // Code from the Arduino Robot Car

  2. // Convert back the 0 - 255 range to 0 - 1023, suitable for motor control code below

  3. xAxis = x*4;

  4. yAxis = y*4;

Just a quick note that when uploading the code, we need to disconnect the RX and TX pins of the Arduino board.

Complete HC-05 Slave code:

  1. /*

  2. Arduino Robot Car Wireless Control using the HC-05 Bluetooth

  3. == SLAVE DEVICE - Arduino robot car ==

  4. by Dejan Nedelkovski, www.HowToMechatronics.com

  5. */

  6. #define enA 9

  7. #define in1 4

  8. #define in2 5

  9. #define enB 10

  10. #define in3 6

  11. #define in4 7

  12. int xAxis, yAxis;

  13. unsigned int x = 0;

  14. unsigned int y = 0;

  15. int motorSpeedA = 0;

  16. int motorSpeedB = 0;

  17. void setup() {

  18. pinMode(enA, OUTPUT);

  19. pinMode(enB, OUTPUT);

  20. pinMode(in1, OUTPUT);

  21. pinMode(in2, OUTPUT);

  22. pinMode(in3, OUTPUT);

  23. pinMode(in4, OUTPUT);

  24. Serial.begin(38400); // Default communication rate of the Bluetooth module

  25. }

  26. void loop() {

  27. // Default value - no movement when the Joystick stays in the center

  28. x = 510 / 4;

  29. y = 510 / 4;

  30. // Read the incoming data from the Joystick, or the master Bluetooth device

  31. while (Serial.available() >= 2) {

  32. x = Serial.read();

  33. delay(10);

  34. y = Serial.read();

  35. }

  36. delay(10);

  37. // Convert back the 0 - 255 range to 0 - 1023, suitable for motor control code below

  38. xAxis = x*4;

  39. yAxis = y*4;

  40. // Y-axis used for forward and backward control

  41. if (yAxis < 470) {

  42. // Set Motor A backward

  43. digitalWrite(in1, HIGH);

  44. digitalWrite(in2, LOW);

  45. // Set Motor B backward

  46. digitalWrite(in3, HIGH);

  47. digitalWrite(in4, LOW);

  48. // 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

  49. motorSpeedA = map(yAxis, 470, 0, 0, 255);

  50. motorSpeedB = map(yAxis, 470, 0, 0, 255);

  51. }

  52. else if (yAxis > 550) {

  53. // Set Motor A forward

  54. digitalWrite(in1, LOW);

  55. digitalWrite(in2, HIGH);

  56. // Set Motor B forward

  57. digitalWrite(in3, LOW);

  58. digitalWrite(in4, HIGH);

  59. // 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

  60. motorSpeedA = map(yAxis, 550, 1023, 0, 255);

  61. motorSpeedB = map(yAxis, 550, 1023, 0, 255);

  62. }

  63. // If joystick stays in middle the motors are not moving

  64. else {

  65. motorSpeedA = 0;

  66. motorSpeedB = 0;

  67. }

  68. // X-axis used for left and right control

  69. if (xAxis < 470) {

  70. // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value

  71. int xMapped = map(xAxis, 470, 0, 0, 255);

  72. // Move to left - decrease left motor speed, increase right motor speed

  73. motorSpeedA = motorSpeedA - xMapped;

  74. motorSpeedB = motorSpeedB + xMapped;

  75. // Confine the range from 0 to 255

  76. if (motorSpeedA < 0) {

  77. motorSpeedA = 0;

  78. }

  79. if (motorSpeedB > 255) {

  80. motorSpeedB = 255;

  81. }

  82. }

  83. if (xAxis > 550) {

  84. // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value

  85. int xMapped = map(xAxis, 550, 1023, 0, 255);

  86. // Move right - decrease right motor speed, increase left motor speed

  87. motorSpeedA = motorSpeedA + xMapped;

  88. motorSpeedB = motorSpeedB - xMapped;

  89. // Confine the range from 0 to 255

  90. if (motorSpeedA > 255) {

  91. motorSpeedA = 255;

  92. }

  93. if (motorSpeedB < 0) {

  94. motorSpeedB = 0;

  95. }

  96. }

  97. // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)

  98. if (motorSpeedA < 70) {

  99. motorSpeedA = 0;

  100. }

  101. if (motorSpeedB < 70) {

  102. motorSpeedB = 0;

  103. }

  104. analogWrite(enA, motorSpeedA); // Send PWM signal to motor A

  105. analogWrite(enB, motorSpeedB); // Send PWM signal to motor B

  106. }

Arduino Robot Car Control Using a Smartphone and a Custom-build Android App

Next, let’s see how we can control our Arduino robot car using a custom-build Android app. The circuit schematic of the robot car is exactly the same as the previous example, with the HC-05 Bluetooth mode set as a slave device.

📷

On the other hand, using the MIT App Inventor online application we will build our own Android app, and here’s how it looks.

So basically the app simulates a joystick, which appearance is made out of two images, or image sprites.

📷

If we take a look at the blocks of this app, we can see that when the joystick sprite is dragged, the image of the joystick ball is moved to the current location of our finger, and at the same time we send the X and Y values over the Bluetooth to the Arduino car.📷

These values are accepted by the Arduino in the same way as in the previous example, using the Serial.read function.

  1. // Read the incoming data from the Smartphone Android App

  2. while (Serial.available() >= 2) {

  3. x = Serial.read();

  4. delay(10);

  5. y = Serial.read();

  6. }

What we need to do additionally here is to convert the received X and Y values from the smartphone into the 0 to 1023 range, suitable for the motor control code below. These values depend of the canvas size, and the X and Y values I was getting from my app were from 60 to 220, which using the map() function I easily converted them.

  1. // Makes sure we receive corrent values

  2. if (x > 60 & x < 220) {

  3. xAxis = map(x, 220, 60, 1023, 0); // Convert the smartphone X and Y values to 0 - 1023 range, suitable motor for the motor control code below

  4. }

  5. if (y > 60 & y < 220) {

  6. yAxis = map(y, 220, 60, 0, 1023);

  7. }

At the application blocks, we can also see that when the image sprite is touched up the joystick ball moves back to the center of the canvas and appropriate values are sent to the car in order stop moving. You can find and download this app on the website article, as well as, the two images of the joystick so you can build your own or modify this app.

Your can download the Android App below, as well as the two images for the joystick:

📷Arduino_Robot_Car_Joystick_App.apk 1.62 MBDownload📷Arduino_Robot_Car_Joystick_App_aia_file 171.20 KBDownload📷Joystick App Images 44.36 KBDownload

Complete Arduino code:

  1. /*

  2. Arduino Robot Car Wireless Control using the HC-05 Bluetooth and custom-build Android app

  3. == SLAVE DEVICE - Arduino robot car ==

  4. by Dejan Nedelkovski, www.HowToMechatronics.com

  5. */

  6. #define enA 9

  7. #define in1 4

  8. #define in2 5

  9. #define enB 10

  10. #define in3 6

  11. #define in4 7

  12. int xAxis, yAxis;

  13. int x = 0;

  14. int y = 0;

  15. int motorSpeedA = 0;

  16. int motorSpeedB = 0;

  17. void setup() {

  18. pinMode(enA, OUTPUT);

  19. pinMode(enB, OUTPUT);

  20. pinMode(in1, OUTPUT);

  21. pinMode(in2, OUTPUT);

  22. pinMode(in3, OUTPUT);

  23. pinMode(in4, OUTPUT);

  24. Serial.begin(38400); // Default communication rate of the Bluetooth module

  25. }

  26. void loop() {

  27. // Default value - no movement when the Joystick stays in the center

  28. xAxis = 510;

  29. yAxis = 510;

  30. // Read the incoming data from the Smartphone Android App

  31. while (Serial.available() >= 2) {

  32. x = Serial.read();

  33. delay(10);

  34. y = Serial.read();

  35. }

  36. delay(10);

  37. // Makes sure we receive corrent values

  38. if (x > 60 & x < 220) {

  39. xAxis = map(x, 220, 60, 1023, 0); // Convert the smartphone X and Y values to 0 - 1023 range, suitable motor for the motor control code below

  40. }

  41. if (y > 60 & y < 220) {

  42. yAxis = map(y, 220, 60, 0, 1023);

  43. }

  44. // Y-axis used for forward and backward control

  45. if (yAxis < 470) {

  46. // Set Motor A backward

  47. digitalWrite(in1, HIGH);

  48. digitalWrite(in2, LOW);

  49. // Set Motor B backward

  50. digitalWrite(in3, HIGH);

  51. digitalWrite(in4, LOW);

  52. // 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

  53. motorSpeedA = map(yAxis, 470, 0, 0, 255);

  54. motorSpeedB = map(yAxis, 470, 0, 0, 255);

  55. }

  56. else if (yAxis > 550) {

  57. // Set Motor A forward

  58. digitalWrite(in1, LOW);

  59. digitalWrite(in2, HIGH);

  60. // Set Motor B forward

  61. digitalWrite(in3, LOW);

  62. digitalWrite(in4, HIGH);

  63. // 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

  64. motorSpeedA = map(yAxis, 550, 1023, 0, 255);

  65. motorSpeedB = map(yAxis, 550, 1023, 0, 255);

  66. }

  67. // If joystick stays in middle the motors are not moving

  68. else {

  69. motorSpeedA = 0;

  70. motorSpeedB = 0;

  71. }

  72. // X-axis used for left and right control

  73. if (xAxis < 470) {

  74. // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value

  75. int xMapped = map(xAxis, 470, 0, 0, 255);

  76. // Move to left - decrease left motor speed, increase right motor speed

  77. motorSpeedA = motorSpeedA - xMapped;

  78. motorSpeedB = motorSpeedB + xMapped;

  79. // Confine the range from 0 to 255

  80. if (motorSpeedA < 0) {

  81. motorSpeedA = 0;

  82. }

  83. if (motorSpeedB > 255) {

  84. motorSpeedB = 255;

  85. }

  86. }

  87. if (xAxis > 550) {

  88. // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value

  89. int xMapped = map(xAxis, 550, 1023, 0, 255);

  90. // Move right - decrease right motor speed, increase left motor speed

  91. motorSpeedA = motorSpeedA + xMapped;

  92. motorSpeedB = motorSpeedB - xMapped;

  93. // Confine the range from 0 to 255

  94. if (motorSpeedA > 255) {

  95. motorSpeedA = 255;

  96. }

  97. if (motorSpeedB < 0) {

  98. motorSpeedB = 0;

  99. }

  100. }

  101. // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)

  102. if (motorSpeedA < 70) {

  103. motorSpeedA = 0;

  104. }

  105. if (motorSpeedB < 70) {

  106. motorSpeedB = 0;

  107. }

  108. analogWrite(enA, motorSpeedA); // Send PWM signal to motor A

  109. analogWrite(enB, motorSpeedB); // Send PWM signal to motor B

  110. }

Arduino Robot Car Wireless Control Using NRF24L01 Transceiver Module

Now we can move on to the next method, wireless control of the Arduino robot car using the NRF24L01 transceiver modules.

📷

Here’s the circuit schematic. We can note that these modules use the SPI communication, so compared to the previous example, I had the move the Enable A and Enable B pins of the L298N driver to the pins number 2 and 3 of the Arduino board.📷You can get the NRF24L01 module on the following Amazon link.

Source Code

For this example we need to install the RF24 library. In a similar way to the previous example, after defining some pins and setting up the module as a transmitter, we read the X and Y values of the joystick and send them to the other NRF24L01 module at the Arduino robot car.

First we can note that analog readings are Strings, which using the string.toCharArray() function are put into a character array. Then using the radio.write() function we send that character array data to the other module.

Transmitter code:

  1. /*

  2. Arduino Robot Car Wireless Control using the NRF24L01 Transceiver module

  3. == Transmitter - Joystick ==

  4. by Dejan Nedelkovski, www.HowToMechatronics.com

  5. Library: TMRh20/RF24, https://github.com/tmrh20/RF24/

  6. */

  7. #include <SPI.h>

  8. #include <nRF24L01.h>

  9. #include <RF24.h>

  10. RF24 radio(8, 9); // CE, CSN

  11. const byte address[6] = "00001";

  12. char xyData[32] = "";

  13. String xAxis, yAxis;

  14. void setup() {

  15. Serial.begin(9600);

  16. radio.begin();

  17. radio.openWritingPipe(address);

  18. radio.setPALevel(RF24_PA_MIN);

  19. radio.stopListening();

  20. }

  21. void loop() {

  22. xAxis = analogRead(A0); // Read Joysticks X-axis

  23. yAxis = analogRead(A1); // Read Joysticks Y-axis

  24. // X value

  25. xAxis.toCharArray(xyData, 5); // Put the String (X Value) into a character array

  26. radio.write(&xyData, sizeof(xyData)); // Send the array data (X value) to the other NRF24L01 modile

  27. // Y value

  28. yAxis.toCharArray(xyData, 5);

  29. radio.write(&xyData, sizeof(xyData));

  30. delay(20);

  31. }

On the other side. at the Arduino robot car, after defining the module as receiver, we accept data using the radio.read() function. Then using the atoi() function we convert the received data, or the X and Y values from the joystick, into integer values, which are suitable for the motor control code below.

  1. // Code from the Arduino Robot Car - NRF24L01 example

  2. if (radio.available()) { // If the NRF240L01 module received data

  3. radio.read(&receivedData, sizeof(receivedData)); // Read the data and put it into character array

  4. xAxis = atoi(&receivedData[0]); // Convert the data from the character array (received X value) into integer

  5. delay(10);

  6. radio.read(&receivedData, sizeof(receivedData));

  7. yAxis = atoi(&receivedData[0]);

  8. delay(10);

  9. }

It’s that simple, but of course as I already said, if you need more details how to connect and setup the modules, you can always check my particular tutorial for it.

Receiver code:

  1. /*

  2. Arduino Robot Car Wireless Control using the NRF24L01 Transceiver module

  3. == Receiver - Arduino robot car ==

  4. by Dejan Nedelkovski, www.HowToMechatronics.com

  5. Library: TMRh20/RF24, https://github.com/tmrh20/RF24/

  6. */

  7. #include <SPI.h>

  8. #include <nRF24L01.h>

  9. #include <RF24.h>

  10. #define enA 2 // Note: Pin 9 in previous video ( pin 10 is used for the SPI communication of the NRF24L01)

  11. #define in1 4

  12. #define in2 5

  13. #define enB 3 // Note: Pin 10 in previous video

  14. #define in3 6

  15. #define in4 7

  16. RF24 radio(8, 9); // CE, CSN

  17. const byte address[6] = "00001";

  18. char receivedData[32] = "";

  19. int xAxis, yAxis;

  20. int motorSpeedA = 0;

  21. int motorSpeedB = 0;

  22. void setup() {

  23. pinMode(enA, OUTPUT);

  24. pinMode(enB, OUTPUT);

  25. pinMode(in1, OUTPUT);

  26. pinMode(in2, OUTPUT);

  27. pinMode(in3, OUTPUT);

  28. pinMode(in4, OUTPUT);

  29. Serial.begin(9600);

  30. radio.begin();

  31. radio.openReadingPipe(0, address);

  32. radio.setPALevel(RF24_PA_MIN);

  33. radio.startListening();

  34. }

  35. void loop() {

  36. if (radio.available()) { // If the NRF240L01 module received data

  37. radio.read(&receivedData, sizeof(receivedData)); // Read the data and put it into character array

  38. xAxis = atoi(&receivedData[0]); // Convert the data from the character array (received X value) into integer

  39. delay(10);

  40. radio.read(&receivedData, sizeof(receivedData));

  41. yAxis = atoi(&receivedData[0]);

  42. delay(10);

  43. }

  44. // Y-axis used for forward and backward control

  45. if (yAxis < 470) {

  46. // Set Motor A backward

  47. digitalWrite(in1, HIGH);

  48. digitalWrite(in2, LOW);

  49. // Set Motor B backward

  50. digitalWrite(in3, HIGH);

  51. digitalWrite(in4, LOW);

  52. // 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

  53. motorSpeedA = map(yAxis, 470, 0, 0, 255);

  54. motorSpeedB = map(yAxis, 470, 0, 0, 255);

  55. }

  56. else if (yAxis > 550) {

  57. // Set Motor A forward

  58. digitalWrite(in1, LOW);

  59. digitalWrite(in2, HIGH);

  60. // Set Motor B forward

  61. digitalWrite(in3, LOW);

  62. digitalWrite(in4, HIGH);

  63. // 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

  64. motorSpeedA = map(yAxis, 550, 1023, 0, 255);

  65. motorSpeedB = map(yAxis, 550, 1023, 0, 255);

  66. }

  67. // If joystick stays in middle the motors are not moving

  68. else {

  69. motorSpeedA = 0;

  70. motorSpeedB = 0;

  71. }

  72. // X-axis used for left and right control

  73. if (xAxis < 470) {

  74. // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value

  75. int xMapped = map(xAxis, 470, 0, 0, 255);

  76. // Move to left - decrease left motor speed, increase right motor speed

  77. motorSpeedA = motorSpeedA - xMapped;

  78. motorSpeedB = motorSpeedB + xMapped;

  79. // Confine the range from 0 to 255

  80. if (motorSpeedA < 0) {

  81. motorSpeedA = 0;

  82. }

  83. if (motorSpeedB > 255) {

  84. motorSpeedB = 255;

  85. }

  86. }

  87. if (xAxis > 550) {

  88. // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value

  89. int xMapped = map(xAxis, 550, 1023, 0, 255);

  90. // Move right - decrease right motor speed, increase left motor speed

  91. motorSpeedA = motorSpeedA + xMapped;

  92. motorSpeedB = motorSpeedB - xMapped;

  93. // Confine the range from 0 to 255

  94. if (motorSpeedA > 255) {

  95. motorSpeedA = 255;

  96. }

  97. if (motorSpeedB < 0) {

  98. motorSpeedB = 0;

  99. }

  100. }

  101. // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)

  102. if (motorSpeedA < 70) {

  103. motorSpeedA = 0;

  104. }

  105. if (motorSpeedB < 70) {

  106. motorSpeedB = 0;

  107. }

  108. analogWrite(enA, motorSpeedA); // Send PWM signal to motor A

  109. analogWrite(enB, motorSpeedB); // Send PWM signal to motor B

  110. }

Arduino Robot Car Wireless Control Using HC-12 Long Range Transceiver

For the last method of wireless control of the Arduino robot car, we will use the HC-12 long range transceiver modules. These modules can communication to each other with distances up to 1.8 km.

The circuit schematic for this example is almost the same as the one for the HC-05 Bluetooth modules, as they are using the same method for communication with the Arduino, via the serial port.

📷

You can get the HC-12 Transceiver module on the following Amazon link.

Source Code

The Joystick code is exactly the same as the one for the Bluetooth communication. We just read the analog values of the joystick and send them to the other module using the Serial.write() function.

Transmitter code:

  1. /*

  2. Arduino Robot Car Wireless Control using the HC-12 long range wireless module

  3. == Transmitter - Joystick ==

  4. by Dejan Nedelkovski, www.HowToMechatronics.com

  5. */

  6. int xAxis, yAxis;

  7. void setup() {

  8. Serial.begin(9600); // Default communication rate of the Bluetooth module

  9. }

  10. void loop() {

  11. xAxis = analogRead(A0); // Read Joysticks X-axis

  12. yAxis = analogRead(A1); // Read Joysticks Y-axis

  13. // Send the values via the serial port to the slave HC-05 Bluetooth device

  14. Serial.write(xAxis/4); // Dividing by 4 for converting from 0 - 1023 to 0 - 256, (1 byte) range

  15. Serial.write(yAxis/4);

  16. delay(20);

  17. }

On the other side, with the while() loop we wait until the data arrive, then read it using Serial.read() function and convert it back into the 0 to 1023 range, suitable for the motor control code below.

Receiver code:

  1. /*

  2. Arduino Robot Car Wireless Control using the HC-12 long range wireless module

  3. == Receiver - Arduino robot car ==

  4. by Dejan Nedelkovski, www.HowToMechatronics.com

  5. */

  6. #define enA 9

  7. #define in1 4

  8. #define in2 5

  9. #define enB 10

  10. #define in3 6

  11. #define in4 7

  12. int xAxis, yAxis;

  13. int x = 0;

  14. int y = 0;

  15. int motorSpeedA = 0;

  16. int motorSpeedB = 0;

  17. void setup() {

  18. pinMode(enA, OUTPUT);

  19. pinMode(enB, OUTPUT);

  20. pinMode(in1, OUTPUT);

  21. pinMode(in2, OUTPUT);

  22. pinMode(in3, OUTPUT);

  23. pinMode(in4, OUTPUT);

  24. Serial.begin(9600); // Default communication rate of the Bluetooth module

  25. }

  26. void loop() {

  27. // Default value - no movement when the Joystick stays in the center

  28. xAxis = 510;

  29. yAxis = 510;

  30. // Read the incoming data from the

  31. while (Serial.available() == 0) {}

  32. x = Serial.read();

  33. delay(10);

  34. y = Serial.read();

  35. delay(10);

  36. // Convert back the 0 - 255 range to 0 - 1023, suitable for motor control code below

  37. xAxis = x * 4;

  38. yAxis = y * 4;

  39. // Y-axis used for forward and backward control

  40. if (yAxis < 470) {

  41. // Set Motor A backward

  42. digitalWrite(in1, HIGH);

  43. digitalWrite(in2, LOW);

  44. // Set Motor B backward

  45. digitalWrite(in3, HIGH);

  46. digitalWrite(in4, LOW);

  47. // 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

  48. motorSpeedA = map(yAxis, 470, 0, 0, 255);

  49. motorSpeedB = map(yAxis, 470, 0, 0, 255);

  50. }

  51. else if (yAxis > 550) {

  52. // Set Motor A forward

  53. digitalWrite(in1, LOW);

  54. digitalWrite(in2, HIGH);

  55. // Set Motor B forward

  56. digitalWrite(in3, LOW);

  57. digitalWrite(in4, HIGH);

  58. // 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

  59. motorSpeedA = map(yAxis, 550, 1023, 0, 255);

  60. motorSpeedB = map(yAxis, 550, 1023, 0, 255);

  61. }

  62. // If joystick stays in middle the motors are not moving

  63. else {

  64. motorSpeedA = 0;

  65. motorSpeedB = 0;

  66. }

  67. // X-axis used for left and right control

  68. if (xAxis < 470) {

  69. // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value

  70. int xMapped = map(xAxis, 470, 0, 0, 255);

  71. // Move to left - decrease left motor speed, increase right motor speed

  72. motorSpeedA = motorSpeedA - xMapped;

  73. motorSpeedB = motorSpeedB + xMapped;

  74. // Confine the range from 0 to 255

  75. if (motorSpeedA < 0) {

  76. motorSpeedA = 0;

  77. }

  78. if (motorSpeedB > 255) {

  79. motorSpeedB = 255;

  80. }

  81. }

  82. if (xAxis > 550) {

  83. // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value

  84. int xMapped = map(xAxis, 550, 1023, 0, 255);

  85. // Move right - decrease right motor speed, increase left motor speed

  86. motorSpeedA = motorSpeedA + xMapped;

  87. motorSpeedB = motorSpeedB - xMapped;

  88. // Confine the range from 0 to 255

  89. if (motorSpeedA > 255) {

  90. motorSpeedA = 255;

  91. }

  92. if (motorSpeedB < 0) {

  93. motorSpeedB = 0;

  94. }

  95. }

  96. // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)

  97. if (motorSpeedA < 70) {

  98. motorSpeedA = 0;

  99. }

  100. if (motorSpeedB < 70) {

  101. motorSpeedB = 0;

  102. }

  103. analogWrite(enA, motorSpeedA); // Send PWM signal to motor A

  104. analogWrite(enB, motorSpeedB); // Send PWM signal to motor B

  105. }

So that’s pretty much everything for this tutorial. Feel free to ask any question in the comments section below.

 
 
 

Opmerkingen


1000+ Online here world wide

bottom of page