Bluetooth HC-06

Hello everybody. I am new to this, and i have a few questions. Im trying to make a connexion betwen an Arduin and an Android phone via bluetooth. I have a HC-06 bluetooth device. In many tutorials they say to connect the TX to RX and RX to TX (from Arduino to HC-06) in order to make it work. But for testing purpous and try the AT commans, they confifure the arduino to work with other ports. I tryed to work with ports AX and TX (0 and 1) and with 10 and 11 like in a tutorial, and it only works witht the port 10 and 11. I wonder why, whats the difference. Someone can give me a complete explanaition about the difference if their use and the context? I wish to learn more about how all this work.

Please post the program you have been using for your tests.

...R

AmineF: Hello everybody. I am new to this, and i have a few questions. Im trying to make a connexion betwen an Arduin and an Android phone via bluetooth

Taking that literally and to do that and nothing else, you just need to have Bluetooth connected, and the LED flashing about 2Hz. Then scan with Android

I have a HC-06 bluetooth device. In many tutorials they say to connect the TX to RX and RX to TX (from Arduino to HC-06) in order to make it work.

Correct

I tryed to work with ports AX and TX (0 and 1) and with 10 and 11 like in a tutorial, and it only works witht the port 10 and 11. I wonder why, whats the difference.

The former uses Serial, the latter software serial. You might find the following background notes useful.

http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino

AmineF:
I tryed to work with ports AX and TX (0 and 1) and with 10 and 11 like in a tutorial, and it only works witht the port 10 and 11. I wonder why, whats the difference. Someone can give me a complete explanaition about the difference if their use and the context? I wish to learn more about how all this work.

1. Inside the ATmega328P Microcontroller of the UNO Board, there is an electronics-based ‘Serial Data Communication Port’ known as UART Port. Please, take a little bit time and patience to scan the following diagram which has described the connection of the UART Port with the Computer.

Figure-1:

2. In Fig-1, we observe that the UART Port is connected with Computer. The TX and RX line of the port is also available at the DPin (digital pin) connector as 1 (TX) and 0(RX).

3. Because you may not be aware about the internal connection of the UART Port, you have thought that the TX/RX lines of this port are free and accordingly you have connected your Bluetooth Module with DPin-1 and DPin-0.

4. Now you see that there are two devices connected with the same UART Port; this is no good. Therefore, you have to move your BT Module to another UART Port?

5. But we have no more UART Port inside the MCU; so, how can we have another UART Port?

6. The clever Software Engineers have created for us UART Ports using ‘Software Instructions’; these ports are known as Software UART Ports (SUART). In Fig-1, we have shown a SUART Port with SRX pin at DPin-10 and STX Pin at DPin-11.

7. When we include the following codes in our sketch, a SUART is automatically created:

#include<SoftwareSerial.h>
SoftwareSerial BTserial(10, 11);    //software name of SUART is BTserial with SRX at DPin-10 and RTX pin at DPIN-11
BTserial.begin(9600);

8. Now you can connect your BT Module with the newly created SUART Port and operate it safely. There is absolutely no conflict with any other devices.

9. All the functions/methods of the UART Port are equally applicable for the SUART Port except the following which is exclusively reserved for hardware UART Port:

void serialEvent{}

10. The following simple codes could be executed to check the functional link among your Android Smartphone, BT Module and Serial Monitor.

Sending A from Smartphone will turn ON the L (built-in LED of UNO); sending B will turn OFF L.

#include <SoftwareSerial.h>
SoftwareSerial BTserial(10, 11); // SRX , STX
#define ledPin 13

void setup()
{
  Serial.begin(9600);
  BTserial.begin(9600);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
}

void loop()
{
  if (BTserial.available() > 0)
  {
    char c = BTserial.read();
    if (c == 'A')
    {
      digitalWrite(ledPin, HIGH);
    }
    if (c == 'B')
    {
      digitalWrite(ledPin, LOW);
    }
  }
}

Here is the code:

#include <SoftwareSerial.h>

SoftwareSerial hc06(10,11);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Command:");
  hc06.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
if (hc06.available()){
                  Serial.write(hc06.read());
                }

                if (Serial.available()){
                  hc06.write(Serial.read());
                }  

}

I go step by step. I want first to undersand corectly how arduino work and comunicate with other devices.

Thanks for the answers and the documentation. Very helpfull.

I have a HC06 that I have not used for ages. I thought I might be able to test your program but I can't get it to pair with anything with my own test program that used to work. I may try again later.

...R

Your program in Reply #4 seems to work fine for me with my Android phone.

...R

The program works fine. I simply wanted to understand why it works in a way and not another.

AmineF: The program works fine. I simply wanted to understand why it works in a way and not another.

What have you wanted to mean by saying -- 'not another'? You wanted to mean why the BT is not working with hardware UART Port (0, 1 = RX, TX)?

AmineF: I simply wanted to understand why it works in a way and not another.

You will have to expand on that.

...R