HC-05 COMMUNICATION

Hi, i'm making a bluetooth controlled car with two arduinos and two hc05 modules. One each for the controller and for the car itself.
I am reading a joystick module on the controller and sending the x,y to the car.
My issue is that i cannot receive any data to the car. If i check the serial panel of the controller it is sending data, but the car's serial panel is empty. The hc05 are set as master and slave and they are both paired when they're on.
Everything is hoocked up correctly fisically, but there's something wrong.
Can you help me?

slave.ino (968 Bytes)

controller.ino (215 Bytes)

1.) Don't use "Serial" to transfer data - it should only be used for debugging
2.) You are transferring data unreliably - look at this tutorial and consider using this serial transfer library for reliable data transfer
3.) Make sure the HC-05 modules are correctly paired
4.) Make sure the wiring is correct - beginners usually get the TX -> RX wiring wrong (fyi)

1. Use software UART Port (SUART) to connect your HC05 as per following diagram (Fig-1) with the controller.
HC5-uno.png
Figure-1:

2. Upload the following sketch in your controller UNO -- your one slightly modified.

#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3); //SRX = DPin-2 of UNO <-- TX of HC05 directly
                            //STX = DPin-3 of UNO --> RX of HCo5 via divider
int x=0;
int y=0;
int xa=A0;
int ya=A1;

void setup() 
{
 Serial.begin(38400); 
}
void loop() 
{
  x = analogRead(xa);
  y = analogRead(ya); 
  Serial.write(map(x, 0, 1023, 0, 255)); 
  Serial.print(',');  //data item separator
  Serial.write(map(y, 0, 1023, 0, 255));
  Serial.print('\n'); //end-of-msg mark
  delay(20);
}

3. Upload the following sketch in your car UNO -- your sketch is slightly modified.

#include<Servo.h>
Servo myservo;
#define enA 9
#define in1 10
#define in2 11
int servo = 8;
int ang = 0;
int xAxis, yAxis;
unsigned int  x = 0;
unsigned int  y = 0;
int motorSpeed = 0;

byte myData[10];

void setup()
{
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(servo, OUTPUT);
  Serial.begin(9600);//(38400);
  myservo.attach(8);
}
void loop()
{
  byte n = Serial.available();
  if (n != 0)
  {
    byte n = Serial.readBytesUntil('\n', myData, 10);
    x = myData[0];
    x = map(x, 0, 255, 0, 1023);
    y = myData[2];
    y = map(y, 0, 255, 0, 1023);

    //x = 510 / 4;
    //y = 510 / 4;

    //while (Serial.available() >= 2)
    //{
    //x = Serial.read();
    //delay(10);
    //y = Serial.read();
    //}
    //delay(10);

    xAxis = x;// * 4;
    yAxis = y ;//* 4;

    if (yAxis < 470)
    {
      digitalWrite(in1, HIGH);
      digitalWrite(in2, LOW);
    }
    else if (yAxis > 550)
    {
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
    }
    else
    {
      motorSpeed = 0;
    }

    if (xAxis < 470 && xAxis > 550)
    {
      ang = map(xAxis, 0, 1023, 10, 100);
      myservo.write(ang);
    }
    else
    {
      myservo.write(60);
    }
  }
}

HC5-uno.png

GolamMostafa:
1. Use software UART Port (SUART) to connect your HC05 as per following diagram (Fig-1) with the controller.
HC5-uno.png
Figure-1:

2. Upload the following sketch in your controller UNO -- your one slightly modified.

#include<SoftwareSerial.h>

SoftwareSerial SUART(2, 3); //SRX = DPin-2 of UNO <-- TX of HC05 directly
                            //STX = DPin-3 of UNO --> RX of HCo5 via divider
int x=0;
int y=0;
int xa=A0;
int ya=A1;

void setup()
{
Serial.begin(38400);
}
void loop()
{
  x = analogRead(xa);
  y = analogRead(ya);
  Serial.write(map(x, 0, 1023, 0, 255));
  Serial.print(',');  //data item separator
  Serial.write(map(y, 0, 1023, 0, 255));
  Serial.print('\n'); //end-of-msg mark
  delay(20);
}




**3.** Upload the following sketch in your car UNO -- your sketch is slightly modified.


#include<Servo.h>
Servo myservo;
#define enA 9
#define in1 10
#define in2 11
int servo = 8;
int ang = 0;
int xAxis, yAxis;
unsigned int  x = 0;
unsigned int  y = 0;
int motorSpeed = 0;

byte myData[10];

void setup()
{
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(servo, OUTPUT);
  Serial.begin(9600);//(38400);
  myservo.attach(8);
}
void loop()
{
  byte n = Serial.available();
  if (n != 0)
  {
    byte n = Serial.readBytesUntil('\n', myData, 10);
    x = myData[0];
    x = map(x, 0, 255, 0, 1023);
    y = myData[2];
    y = map(y, 0, 255, 0, 1023);

//x = 510 / 4;
    //y = 510 / 4;

//while (Serial.available() >= 2)
    //{
    //x = Serial.read();
    //delay(10);
    //y = Serial.read();
    //}
    //delay(10);

xAxis = x;// * 4;
    yAxis = y ;//* 4;

if (yAxis < 470)
    {
      digitalWrite(in1, HIGH);
      digitalWrite(in2, LOW);
    }
    else if (yAxis > 550)
    {
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
    }
    else
    {
      motorSpeed = 0;
    }

if (xAxis < 470 && xAxis > 550)
    {
      ang = map(xAxis, 0, 1023, 10, 100);
      myservo.write(ang);
    }
    else
    {
      myservo.write(60);
    }
  }
}

1)Should i get on the serial panel of the slave the same stuff that's on the controller panel (after having addes Serial.print to the sketch)?
2)on the slave sketch i should use 38400 baud.
3)how can you make the slave read anything if you don't say to him where is the pin to read from?

  1. It is good if you make the following connection of HC05 with your receiver.
    HC5-uno.png

  2. Use the same Bd as of sender.

  3. Include the following lines in the sketch:

#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3);  //DPin-2 = SRX of UNO; DPin-3 = STX of UNO
SUART.begin(9600);

HC5-uno.png

i still not getting anything into the slave serial panel. There should be the same stuff that's on the controller one( on the controller serial panel i have always ?,? but i believe it is correct).
If i add SUART.begin it doesn't change anything. i tried to add that also to the controller, but it also stops sending.
I connect things like the image, and the sketches are the same.
Somehow they don't transfer infos. I mean should i get values on the serial of the controller without having to print them, isn't it?

Thanks for helping me :slight_smile:

GolamMostafa:

  1. It is good if you make the following connection of HC05 with your receiver.
    HC5-uno.png

  2. Use the same Bd as of sender.

  3. Include the following lines in the sketch:

#include<SoftwareSerial.h>

SoftwareSerial SUART(2, 3);  //DPin-2 = SRX of UNO; DPin-3 = STX of UNO
SUART.begin(9600);

You tell us about your setup:

A: Controller side:
1. Arduino UNO fitted with HC05-1 at SUART (2, 3). yes/no?

2. Arduino works well with your Android Smart Phone. yes/no?

B: Car side:
1. Arduino NANO fitted with HC05-2 at SUART(2. 3). yes/no?

2. NANO works well with Android Smart Phone. yes/no?

C: Is HC05-1 is paired with HC05-2? yes/no?

D: Post codes of your Controller (UNO) side and Car (NANO) side.

They are both UNOs.

A) yes
yes

B) it's not a nano.
the hc05 is connected to suart 2,3
yes(connection is ok);
Yes

slave_rev_2.ino (1.15 KB)

controller_rev_2.ino (518 Bytes)

1. Your controller programmer is missing the following line:

SUART.begin(9600);

The corrected program is:

#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3); //SRX = DPin-2 of UNO <-- TX of HC05 directly
//STX = DPin-3 of UNO --> RX of HCo5 via divider
int x = 0;
int y = 0;
int xa = A0;
int ya = A1;

void setup()
{
  Serial.begin(38400);
  SUART.begin(9600);     //it was missing before
}
void loop()
{
  x = analogRead(xa);
  y = analogRead(ya);
  Serial.write(map(x, 0, 1023, 0, 255));
  Serial.print(',');  //data item separator
  Serial.write(map(y, 0, 1023, 0, 255));
  Serial.print('\n'); //end-of-msg mark
  delay(20);
}

2. Your slave program is also missing the following line:

SUART.begin(9600);

The corrected program is:

#include<Servo.h>
#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3);  //DPin-2 = SRX of UNO; DPin-3 = STX of UNO
Servo myservo;
int enA = 9;
int in1 = 10;
int in2 = 11;
int servo = 8;
int ang = 0;
int xAxis, yAxis;
unsigned int  x = 0;
unsigned int  y = 0;
int motorSpeed = 0;

byte myData[10];

void setup()
{
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(servo, OUTPUT);

  Serial.begin(38400);
  SUART.begin(9600);

  myservo.attach(8);
}
void loop()
{
  byte n = Serial.available();
  if (n != 0)
  {
    byte n = Serial.readBytesUntil('\n', myData, 10);
    x = myData[0];
    x = map(x, 0, 255, 0, 1023);
    y = myData[2];
    y = map(y, 0, 255, 0, 1023);

    xAxis = x;
    yAxis = y ;

    if (yAxis < 470)
    {
      digitalWrite(in1, HIGH);
      digitalWrite(in2, LOW);
    }
    else if (yAxis > 550)
    {
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
    }
    else
    {
      motorSpeed = 0;
    }

    if (xAxis < 470 && xAxis > 550)
    {
      ang = map(xAxis, 0, 1023, 10, 100);
      myservo.write(ang);
    }
    else
    {
      myservo.write(60);
    }
  }
}

Hope that the corrected programs will help you. Report the results, please.