The default baud rate of HC-06 is 9600. Therefore, software serial can be used in Arduino without a baud rate change.

hc-06.pdf
0.74MB

 

Arduino board and HC-06 wiring
Arduino board and HC-06 wiring

- Wire connection for Arduino and HC-06
HC-06   Arduino
VCC ->  3.3V/5V
GND ->  GND
TXD ->  Pin 2 (Set as RX pin in Arduino code)
RXD ->  Pin 3 (set as TX pin in Arduino code)

 

- How to check baud rate of the module

1. Module and Arduino must be well wired in hardware.

2. Upload the basic Sketch which is coded baud rate "9600" of Serial.

3. Set option to "Both NL & CR" for Serial Monitor of Arduino IDE.

4. Set baud rate to "9600" at Serial Monitor of Arduino IDE.

6. Input "AT" at text window and if get "OK" as a response, the baud rate of the module is 9600.

7. If not, try to change baud rate to 115200 or another value in the sketch and upload.

8. Set baud rate to "115200" or another value at Serial Monitor of Arduino IDE.

9. Input "AT" at text window and if get "OK" as a response, the baud rate of the module is 115200. if not repeat no.7 ~ 9.

SERIAL_BASIC_HC06.zip
0.00MB

#include <SoftwareSerial.h> 
#define BT_rxPin 3 // Bluetooth RX -> 3(arduino TX)
#define BT_txPin 2 // Bluetooth TX -> 2(arduino RX)
SoftwareSerial SerialBT(BT_txPin, BT_rxPin); // (arduino RX, arduino TX)

void setup() {
  Serial.begin(9600);  // Serial Monitor and baud rate 
  SerialBT.begin(9600);   // ESP01 serial and baud rate 
}

void loop() {
  if (SerialBT.available()) {
    Serial.write(SerialBT.read()); //Output the contents of the ESP01 to the serial monitor
  }
  if (Serial. available()) {
    SerialBT.write(Serial.read()); //Write the contents of the serial monitor to the ESP01
  }
}

- How to change baud rate of HC-06

the Bluetooth serial baud rate list of HC-06
4---------9600 (Default)
5---------19200
6---------38400
7---------57600
8---------115200

If current baud rate is 9600 and want to change the baud rate to 115200, input "AT+BAUD8" at text window and get OK115200 as a response, then the change has been made.

 

Precautions when using the HC-06 with Serial communication

While receiving data from HC-06 through software serial communication in Arduino, If Arduino transmits data to HC-06 some of the data received through Bluetooth from the app may be missing. (Probably the first byte) This could be an error in the software serial library. Therefore, It must be coded to transmit data to HC-06 after receiving data from HC-06.

 

Key code for remote control

- Set Software serial to communicate with HC-06.

#include <SoftwareSerial.h> 
#define BT_rxPin 3 // Bluetooth RX -> 3(arduino TX)
#define BT_txPin 2 // Bluetooth TX -> 2(arduino RX)
SoftwareSerial SerialBT(BT_txPin, BT_rxPin); // (arduino RX, arduino TX)

 

- Define the name for HC-06 and pin number which is used to pair.

#define btName F("HC06") // set name of bluetooth module HC-06. 
#define pass F("1234")   // pin number for paring

 

- Function that executes AT command for HC06 setup

String sendData(String order, String val, bool sendPass) {
  String command = F("AT+");
  command += order;
  command += val;
  if (!sendPass) command += F("\r\n");
  SerialBT.print(command); // send the command to the HC-06
  long int time = millis();
  String response = "";
  while( (time+1000) > millis()) { 
    while(SerialBT.available()) { // if The esp has data
      char c = SerialBT.read();   // read the next character.
      response+=c;
    }
  }
  return response;
}

 

- setup() function

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  Serial.begin(9600);   // Software Serial for Serial monitor of Arduino IDE
  SerialBT.begin(9600); // Software Serial for HC-06 
  Serial.println(sendData(F("NAME"),btName,false)); // SET NAME "HC06"
  //Serial.println(sendData(F("PIN"),pass,true));   // SET PIN NUMBER IF NEED. 
  Serial.println(F("Bluetooth 2.0"));
}

 

- loop() function

bool gotData = false;

void loop() {
  if (SerialBT.available()) { // when incomming data from HC06
    uint8_t temp = SerialBT.read();
    Serial.println(temp); // Output data of the HC06 to the serial monitor.
    gotData = true; // the flag for response
  } else {
    if (gotData) {   // if response
      String setVal = "";
      setVal += char(0xF4);      // protocol header of text for ADUCON with bluetooth
      setVal += "Hello World ";
      setVal += random(100,200); // the message for sending
      setVal += char(0xF1);      // protocol footer of text for ADUCON with bluetooth
      Serial.println(setVal);    // Output the message to the serial monitor.
      SerialBT.print(setVal);    // send data to HC06 as response 
      gotData = false;           // initialize the flag
    }
  }
}

 

ARDUINO_HC06_BASIC.zip

ARDUINO_HC06_BASIC.zip
0.00MB

 

- Output of Serial Monitor after upload scketch "ARDUINO_HC06_BASIC.ino"

 

*** To test the code above, you need an app that transmits data via Bluetooth.

[Arduino/ADUCON] - Arduino Bluetooth remote control with ADUCON and HC-06

[Arduino/ADUPAD] - Arduino Bluetooth remote control with ADUPAD and HC-06

 

[Arduino/ADUCON] - ADUCON - Arduino wireless remote control application

[Arduino/ADUPAD] - ADUPAD - Arduino wireless remote control PAD application

 

https://play.google.com/store/apps/details?id=com.tistory.postpop.hmi 

 

ADUCON - Google Play 앱

무선(WiFi/Bluetooth/BLE)을 통한 Arduino 원격 제어 앱

play.google.com

 

https://play.google.com/store/apps/details?id=io.kodular.skyship72.pad01 

 

ADUPAD - Google Play 앱

무선(WiFi/Bluetooth/BLE)을 통한 Arduino 원격 제어 PAD

play.google.com

 

반응형

+ Recent posts