Bluetooth HC-05 or HC-06 Tutorial

Post Reply
djgardn2
Posts: 61
Joined: Sat Apr 19, 2014 6:34 am

Bluetooth HC-05 or HC-06 Tutorial

Post by djgardn2 » Sun Jun 21, 2015 5:07 am

Hello,

This is a simple starter tutorial to work with a Bluetooth module (HC-05 or HC-06) using the PIC32BLUE Android application (you can use PIC32BLUE+ and/or PIC32BTN apps instead too depending on which example code you are using that is attached below). The Bluetooth module is what I used in my previous project found here viewtopic.php?f=15&t=3269

Materials used:
1) One Microcontroller (PIC32MX250F128B)
2) One Bluetooth module
3) Four jumper wires (male to female)
4) Power source (10,000 mAh power bank battery pack 5volts)
5) Android phone/tablet with PIC32BLUE installed (device must have Bluetooth capability)

Important note, the Bluetooth module power source and inputs should be at 3.3 volts only. VCC on the Bluetooth module should be connected to 3.3 volts and GND should be connected to ground seen in the photo at the end of this post.

The power bank is optional and can be switched out with power source and the jumper wire connection style may vary depending on the connectors available on your microcontroller board to make the necessary connections.

Connections between the microcontroller pins to the stepper motor PCB are as follows:

Bluetooth Module <----to----> PIC32 Microcontroller Pin

RXD <--------------wire------------> TX

TXD <--------------wire------------> RX

Note: RX means receive and TX means transmit, so the Bluetooth module will transmit to the microcontrollers receive pin and vice versa.

Note2: You may want to change the code attached in this post to use a different serial port, so you will want to replace the word “Serial” in the code to either Serial0 or Serial1, etc according to the pins you would like to connect to.


A quick and easy way of replacing all of the ‘Serial’ words with a few quick steps is the following:

When in MPIDE with the code sketch opened, hold down the “ctrl” (control) key on your keyboard then press the “F” key, a pop-up will show, you can then type the word Serial inside the “Find:” section and type the word Serial0 (or whichever one you wanted to use) into the “Replace with:” section, once both sections have what you wanted to change click on the “Replace All” button and all the changes have been made throughout the code example.

The connections will vary according to the microcontroller board you are using so be sure to check your datasheet or board’s manual for pinouts to ensure you are connecting to the correct pins. You can also navigate to where you have the MPIDE folder installed on your computer then follow the following file path below to see all of the available board folders then select the folder for your current board and reference the Board_Defs.h file, scrolling down in that file until you see the “Serial Port Declarations”. Note: as soon as the softwareSerial library is added into MPIDE I will be sure to update this post with an example using softwareSerial for an easier connection/wiring example with consistent pin number connections between board selections.

Windows file path example below once inside the MPIDE folder on your computer:
***\hardware\pic32\variants\...

For using the DP32 board settings I used the following connections and changed the provided code to use Serial0 :

Bluetooth Module <----to----> PIC32 Microcontroller Pin

RXD <--------------wire------------> 14

TXD <--------------wire------------> 6



Five different code examples attached below. Be sure to read through them and choose which one fits your needs the best. They are listed as the easiest first to the hardest last.


HC05_or_HC06_Simple_Echo_with_Delay

Code: Select all

/*Change delay time longer if needed inside loop function*/
/*Shorter delay splits up message using HC-05 or HC-06 on Android*/
String inputString ="";
char incoming = 0;
void setup ()
{
  delay(15);
  Serial.begin(9600);
}
void loop ()
{
  if(Serial.available()>0)
  {
    while(Serial.available()>0)
    {
      incoming = Serial.read();
      inputString += String(incoming);
      delay(2);
    }
    Serial.println("Received: " + inputString);
  }
  inputString ="";
}
HC05_or_HC06_Simple_Echo_without_Delay

Code: Select all

// Microcontroller code example
String inputString ="";
char incoming = 0;
void setup ()
{
  delay(15);
  Serial.begin(9600);
  delay(50);
}
void loop ()
{
  if(Serial.available()>0)
  {
      incoming = Serial.read();
      inputString += String(incoming);
      if (incoming == '.')
      {
         Serial.println("Received: " + inputString);
         inputString ="";
      }
  }
}
HC05_or_HC06_with_Delay_Toggle_LED_Simple

Code: Select all

/*Change delay time longer if needed inside loop function*/
/*Shorter delay splits up message using HC-05 or HC-06 on Android*/
String inputString ="";
char incoming = 0;
void setup ()
{
  /*Setup pin you want toggle*/
  /*Example uses only 12*/
  pinMode(12,OUTPUT);
  digitalWrite(12,LOW);
  delay(15);
  Serial.begin(9600);
}
void loop ()
{
  /*String to send must be exact*/
  /*No extra spaces, case sensitive*/
  /* Example string : Toggle */
  if(Serial.available()>0)
  {
    while(Serial.available()>0)
    {
      incoming = Serial.read();
      inputString += String(incoming);
      delay(2);
    }
    if(inputString == "Toggle")
    {
      digitalWrite(12, !digitalRead(12));
    }
    Serial.println("Received: " + inputString);
  }
  inputString ="";
}
HC05_or_HC06_Delay_Toggle_LEDs

Code: Select all

/*Change delay time longer if needed inside loop function*/
/*Shorter delay splits up message using HC-05 or HC-06 on Android*/
String inputString ="";
String togglePin ="";
char incoming = 0;
void setup ()
{
  delay(15);
  /*Setup any pins you want toggle*/
  /*Example uses only 12 and 13*/
  pinMode(12,OUTPUT);
  digitalWrite(12,LOW);
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);
  Serial.begin(9600);
}
void loop ()
{
  /*String to send must be exact*/
  /*No extra spaces, case sensitive*/
  /* Example string : Toggle12 */
  /* Example string : Toggle13 */
  /* ToggleX where X is any output pin in setup function*/
  if(Serial.available()>0)
  {
    while(Serial.available()>0)
    {
      incoming = Serial.read();
      inputString += String(incoming);
      delay(2);
      if(inputString == "Toggle")
      {
        toggleFunction();
      }
    }
    /*Echo what was received back*/
    Serial.println("Received: " + inputString);
    inputString ="";
    togglePin="";
  }
}

void toggleFunction()
{
  while(Serial.available()>0)
  {
    incoming = Serial.read();
    togglePin += String(incoming);
    delay(2);
  } 
  int pin_To_Toggle = atoi(togglePin.c_str());
  digitalWrite(pin_To_Toggle, !digitalRead(pin_To_Toggle));
  inputString+=togglePin;
}
HC05_or_HC06_without_Delay_Toggle_LEDs

Code: Select all

// Microcontroller code example
String inputString ="";
String togglePin ="";
int toggleARMED =0; 
char incoming = 0;
void setup ()
{
  delay(15);
  /*Setup any pins you want toggle*/
  /*Example uses only 12 and 13*/
  pinMode(12,OUTPUT);
  digitalWrite(12,LOW);
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);
  Serial.begin(9600);
}
void loop ()
{
  /*String to send must be exact*/
  /*No extra spaces, case sensitive*/
  /* Example string : Toggle.12. */
  /* Example string : Toggle.13. */
  /* Toggle.X. where X is any output pin in setup function */
  /* You can toggle multiple pins in one string as seen below */
  /* Toggle.X1.Toggle.X2. where X1 & X2 is any output pins in setup function */
  /* Example string : Toggle.12.Toggle.13. */
  if(Serial.available()>0)
  {
    incoming = Serial.read();
    if(incoming != '.')
    {
      inputString += String(incoming);
    }
    else if(incoming == '.')
    {
      if(inputString == "Toggle")
      {
        toggleARMED =1;
      }
      else if(toggleARMED==1)
      {  
        toggleFunction();
        toggleARMED=0;
        Serial.println(inputString + " toggled");
      }
      inputString ="";
      togglePin="";
    }
  }
}

void toggleFunction()
{
  int pin_To_Toggle = atoi(inputString.c_str());
  digitalWrite(pin_To_Toggle, !digitalRead(pin_To_Toggle));
  inputString+=togglePin;
}

The code example files can also be downloaded off my website too if preferred.

HC_Bluetooth_Module.jpg
HC_Bluetooth_Module.jpg (63.66 KiB) Viewed 1476 times
Dare2Tech.com Electronic tutorials and more coming soon.

Post Reply