Arduino Forum

Using Arduino => General Electronics => Topic started by: SicongLiu on Aug 03, 2015, 12:51 pm

Title: Bluetooth communication problem
Post by: SicongLiu on Aug 03, 2015, 12:51 pm
Hi everyone, I am trying to use Bluetooth to control the Arduino DUE via my PC, but the test results are so weird.

The bluetooth I used is JY-MCU (HC-06) slaver, and connections are all correct, TXD - RX, RXD- TX, VCC- 5V OR 3.5V, GND-GND. The type of data I sent from PC is ASCII. I bought three Bluetooth(number:1,2,3).

Test result:
Only when Bluetooth 2 is connected to TX2 and RX2, it can receive most data. I send 1 with  a 1000ms loop for 100 times, there are about 14 wrong results shown in the monitor. If I try to used other Bluetooth and connections, they just cannot work! For example, when I connect Bluetooth 1 to TX2 and RX2, it receives the wrong data mostly.

I cannot figure out why this problem happened. I also tried some solutions, like pull up the resister of TX pin, but the it did not work. Can someone help me with this problem? Thank you in advance.

My BT code:
String comdata = "";
double flag1=0;
int temp;
void setup() {
  Serial.begin(9600);
  Serial2.begin(9600);
  pinMode(16,INPUT_PULLUP);
 
}

void loop() {
  if(Serial2.available()>0)
  {
 
    temp = Serial2.read();
    Serial.println(temp);
    comdata += char(temp);
    //comdata +=temp;
    delay(2);
    flag1 =1;
   }
   
 
  if(flag1 ==1)
  {
    Serial.print("Comdata is ");
    Serial.println(comdata);
    flag1 = 0;
    comdata = String("");

  }
}
Title: Re: Bluetooth communication problem
Post by: jrdoner on Aug 04, 2015, 05:42 am
I am not familiar with your Bluetooth parts, but in the past, if I've had partially incorrect data on a serial link, it usually was due to a timing problem.  I.e., the clocks of the sending and receiving devices were a little off from each other.
Title: Re: Bluetooth communication problem
Post by: AJITnayak on Aug 04, 2015, 10:27 am
use software serial library , you can use two serial as you mentioned.Thats reason you might getting data from one module not from other

Code: [Select]



#include <SoftwareSerial.h>
// software serial #1: TX = digital pin 10, RX = digital pin 11
SoftwareSerial portOne(10,11);

// software serial #2: TX = digital pin 8, RX = digital pin 9
// on the Mega, use other pins instead, since 8 and 9 don't work on the Mega
SoftwareSerial portTwo(8,9);

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // Start each software serial port
  portOne.begin(9600);
  portTwo.begin(9600);
}

void loop()
{
  // By default, the last intialized port is listening.
  // when you want to listen on a port, explicitly select it:
  portOne.listen();
  Serial.println("Data from port one:");
  // while there is data coming in, read it
  // and send to the hardware serial port:
  while (portOne.available() > 0) {
    char inByte = portOne.read();
    Serial.write(inByte);
  }

  // blank line to separate data from the two ports:
  Serial.println();

  // Now listen on the second port
  portTwo.listen();
  // while there is data coming in, read it
  // and send to the hardware serial port:
  Serial.println("Data from port two:");
  while (portTwo.available() > 0) {
    char inByte = portTwo.read();
    Serial.write(inByte);
  }

  // blank line to separate data from the two ports:
  Serial.println();
}
Title: Re: Bluetooth communication problem
Post by: guix on Aug 04, 2015, 07:14 pm
I don't see why he should use sofware serial since the Due has 4 hardware serials..

Quote
like pull up the resister of TX pin
TX pins are outputs. Try on the RX pins instead.


I had a similar problem, and this solved it for me (on Arduino Mega):
Code: [Select]

pinMode( 19, INPUT_PULLUP ); // fix Serial1
pinMode( 17, INPUT_PULLUP ); // fix Serial2
pinMode( 15, INPUT_PULLUP ); // fix Serial3


What is the version of your JY-MCU boards ?
Title: Re: Bluetooth communication problem
Post by: SicongLiu on Aug 06, 2015, 12:46 pm
I don't see why he should use sofware serial since the Due has 4 hardware serials..

TX pins are outputs. Try on the RX pins instead.


I had a similar problem, and this solved it for me (on Arduino Mega):
Code: [Select]

pinMode( 19, INPUT_PULLUP ); // fix Serial1
pinMode( 17, INPUT_PULLUP ); // fix Serial2
pinMode( 15, INPUT_PULLUP ); // fix Serial3


What is the version of your JY-MCU boards ?
Sorry for replying so late. The version of  JY-MCU is 1.06, HC-06.
Title: Re: Bluetooth communication problem
Post by: SicongLiu on Aug 07, 2015, 05:02 pm
I don't see why he should use sofware serial since the Due has 4 hardware serials..

TX pins are outputs. Try on the RX pins instead.


I had a similar problem, and this solved it for me (on Arduino Mega):
Code: [Select]

pinMode( 19, INPUT_PULLUP ); // fix Serial1
pinMode( 17, INPUT_PULLUP ); // fix Serial2
pinMode( 15, INPUT_PULLUP ); // fix Serial3


What is the version of your JY-MCU boards ?
Thank you so much! Your solution works! My Bluetooth now can receive data from PC.