I am trying to make a Bluetooth controlled 4WD robot. i was following a tutorial i found online (https://howtomechatronics.com/tutorials/arduino/arduino-robot-car-wireless-control-using-hc-05-bluetooth-nrf24l01-and-hc-12-transceiver-modules/). After successfully connecting my 2 HC 05 Bluetooth module i used the script from the tutorial and modified it slightly so it would work for my set up. The code for the robot:

//Code for Slave
#define enA 9
#define in1 8
#define in2 7
#define enB 10
#define in3 6
#define in4 5
int xAxis, yAxis;
unsigned int  x = 0;
unsigned int  y = 0;
int motorSpeedA = 0;
int motorSpeedB = 0;
void setup() {
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  Serial.begin(9600); // Default communication rate of the Bluetooth module
void loop() {
  // Default value - no movement when the Joystick stays in the center
  x = 510 / 4;
  y = 510 / 4;
  // Read the incoming data from the Joystick, or the master Bluetooth device
  while (Serial.available() >= 2) {
    x = Serial.read();
    Serial.print("X: ");
    y = Serial.read();
    Serial.print("Y: ");
  // Convert back the 0 - 255 range to 0 - 1023, suitable for motor control code below
  xAxis = x*4;
  yAxis = y*4;
  // Y-axis used for forward and backward control
  if (yAxis < 470) {
    // 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
    motorSpeedA = map(yAxis, 470, 0, 0, 255);
    motorSpeedB = map(yAxis, 470, 0, 0, 255);
  else if (yAxis > 550) {
    // 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
    motorSpeedA = map(yAxis, 550, 1023, 0, 255);
    motorSpeedB = map(yAxis, 550, 1023, 0, 255);
  // If joystick stays in middle the motors are not moving
  else {
    motorSpeedA = 0;
    motorSpeedB = 0;
  // X-axis used for left and right control
  if (xAxis < 470) {
    // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
    int xMapped = map(xAxis, 470, 0, 0, 255);
    // Move to left - decrease left motor speed, increase right motor speed
    motorSpeedA = motorSpeedA + xMapped;
    motorSpeedB = motorSpeedB - xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA > 255) {
      motorSpeedA = 255;
    if (motorSpeedB < 0) {
      motorSpeedB = 0;
  if (xAxis > 550) {
    // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
    int xMapped = map(xAxis, 550, 1023, 0, 255);
    // Move right - decrease right motor speed, increase left motor speed
    motorSpeedA = motorSpeedA - xMapped;
    motorSpeedB = motorSpeedB + xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA < 0) {
      motorSpeedA = 0;
    if (motorSpeedB > 255) {
      motorSpeedB = 255;
  // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
  if (motorSpeedA < 70) {
    motorSpeedA = 0;
  if (motorSpeedB < 70) {
    motorSpeedB = 0;
  analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
void fowardMov(){
  digitalWrite(in1, LOW);
  digitalWrite(in3, LOW);

void backMov(){
  digitalWrite(in2, LOW);
  digitalWrite(in4, LOW);

The code for the Controller:

#define Xaxis A0
#define Yaxis A1

void setup() {
  // put your setup code here, to run once:

void loop() {
  // put your main code here, to run repeatedly:

After uploading the code i tried to move it but it wasn't working so i put print statements to see the value of X and Y. initially it didn't work so i swapped the Bluetooth modules i had and reconnected them. i also changed this line.

 while (Serial.available() >= 2)

To this

 if (Serial.available() > 0)

i started to receive values from the master to the slave but i noticed the values were the same for x and y so i searched for another tutorial on the internet and found this (https://create.arduino.cc/projecthub/andriy-baranov/arduino-4wd-rc-car-639953?ref=similar&ref_id=18369&offset=3) reading his code i wondered how did the slave work. According to the official Arduino documentation Serial.read() does not take in any arguments but i still tried it and it didn't work. I decided to retry the code from the first tutorial i checked this time it looked like it was working so i tried moving the joystick and the serial monitor printed correct values for both at first then it swaps their values then freezes. How can i stop it from freezing and swapping the values? Also why doesn't the while loop run forever isn't the amount of data in the serial buffer always higher than 2? Thank you.diagram of slave

diagram of master


Your setup is WAY too complex! Simplify your setup to the point where it only deals with just the piece that is giving you problems; in your case serial port comms between master and slave. Solve JUST THAT problem, and then move on

  • I have simplified my setup by removing everything except the master and slave Bluetooth devices . i made a simple sketch to send data from the master to the slave. it still does the same thing. it freezes then after a while it prints out all the data sent by the master device. i noticed when i increase the delay for the master to 1 second it works a lot better but won't a delay of 1 second cause a noticeable time lag when controlling the robot? Thank you – Ayanfe Adekanye Oct 20 '20 at 15:47
  • Glad to hear you simplified things, and are starting to get some encouraging results. If increasing the Master's delay to 1sec works a lot better, then you may be on to something. Try increasing the delay even further (2sec, 10 sec) and see if that allows the master/slave response to look more like 'real time'. If so, you may find that the master/slave serial rate is too low, and the 1 second delay is required to get all the bytes through the channel – user3765883 Oct 20 '20 at 18:59
  • In any case, take your time, go very slowly, write everything down, and ONLY CHANGE ONE THING AT A TIME. At some point you will look at your results and your notes and say "Of course, why didn't I think of that earlier" ;-). – user3765883 Oct 20 '20 at 19:00
  • I did what you said and noticed something. i started the masters delay from 2 secs and started gradually decreasing it by half. At 125 millisecond it was still receiving serial data normally but with a little noticeable lag sometimes. the lag doesn't last up to a second though. I'm going to modify the code for the robot slightly so it doesn't need to send as much data. in the first tutorial the baud rate he uses is 38400 but i use 9600 do you think that could be the solution to my problem? thank you – Ayanfe Adekanye Oct 20 '20 at 20:42
  • The old 9600 baud rate is ancient. Try 115200 or 230400 – user3765883 Oct 20 '20 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.