Exercise 26 - Bluetooth module (HC-06)

1.      INTRODUCTION

Bluetooth is catalogued into personal area networks (APA), it uses short wavelength in the ISM band from 2.4. Originally, Bluetooth was designed as a wireless alternative to RS-232 communication.

A Bluetooth device can actuate as master or slave. When the device is master, it can connect to a maximum of seven slave devices. However, when it actuates as a slave, it only can be connect with one master.

At any time, data can be transferred between the master and other device. If there is more than one slave, the master chooses a concrete slave to communicate with it.

In this exercise, we are going to use HC-06 module it cannot work as master. It has a brother, HC-05 module, the hardware between modules it is the same, but HC-05 has a more complete firmware, which permit configure it with more AT command than in HC-06.



2.      HOW TO CONNECT?

Materials:

·         Arduino UNO board

·         Protoboard

·         Some wire Jumpers

·         HC-06 bluetooth module

·         2x LEDs

·         2x 220 ohms resitors

 

To connect the HC-06 module you must connect Vcc to 5V, GND to GND on Arduino, module TX pin to Arduino Rx pin and module Rx pin to Arduino TX pin.

On EXERCISE_A, you only have to connect module. On the other hand, in exercises B and C, you must connect two LEDs and RGB LED respectively.

Finally, in EXERCISE_B AND C it is necessary to download two Android applications.

When device is attached to Module the power led will be permanently turn on.


EXERCISE_A (AT_Commands)

EXERCISE_B (Control_two_LEDs)

EXERCISE_C (Colour_control_RGB_LED)



3.      PROGRAMMING

In all exercises, it is necessary to include SoftwareSerial.h. You also use directly TX and Rx pins on Arduino (pins 0 y 1) but when you upload the program, you must disconnect these wires from Arduino. If you don't do this, the program is uploaded via Bluetooth to the air.

EXERCISE_A (AT_Commands)

This program reads and writes on Serial monitor and BT serial. If the module LED is blinking, it is ready for AT communication. On HC-06 AT commands are:

·         AT => returns ok

·         AT+VERSION => returns software version

·         AT+NAMEXXX => Set module name

e.g. AT+NAMEEDUTRONIX

 

·         AT+BAUDX => Set the communication velocity between module and Serial monitor.

1 configure        1200bps

2 configure        2400bps

3 configure        4800bps

4 configure        9600bps (Default)

5 configure        19200bps

6 configure        38400bps

7 configure        57600bps

8 configure        115200bps

e.g.  AT+BAUD7 configure communication at 57600 bauds

You can write AT commands on serial monitor. If module does not respond, you can to try change velocity communication. Make you sure that you connected Tx and Rx well.

Code:

#include <SoftwareSerial.h>   // Include  SoftwareSerial  library

SoftwareSerial BT(10, 11);   /*Define pins Rx and Tx on Arduiono, which are connected to Bluetooth.*/

 

void setup()

{

  BT.begin(9600);       //Initialize serial port which has been created.

  Serial.begin(9600);   //Initialize serial port.

}

 

void loop()

{

  if (BT.available())   //If data arrives by BT, it is sent by Serial monitor.

  {

    Serial.write(BT.read()); //Data read from Bluetooth module

  }

 

  if (Serial.available()) //If data arrives by Serial monitor, it is sent by BT.

  {

    BT.write(Serial.read()); //Data read from from Serial monitor.

  }

}

 

EXERCISE_B (Control_two_LEDs)

To use this program it is necessary to download a Bluetooth terminal on Android.

e.g. Bluetooth terminal

First, you must associate module with a Smartphone in phone settings and then, you can enter into application and connect with the phone.

Code:

#include <SoftwareSerial.h>   // Include  SoftwareSerial  library

SoftwareSerial BT(10,11);    /*Define pins Rx and Tx on Arduiono, which are connected to Bluetooth.*/

 

Declare LEDs variables and declare value variable, where received data will be stored.

 

int led1=6;

int led2=5;

char value=0;

 

void setup()

{

  BT.begin(9600);       //Initialize serial port which has been created.

  Serial.begin(9600);   //Initialize serial port.

  pinMode(led1,OUTPUT);

  pinMode(led2,OUTPUT);

}

 

void loop()

{

  if(BT.available())   //If data arrives by BT, it is sent by Serial monitor.

  {

    value=BT.read();    

  }

 

On this condition, the code evaluates Bluetooth send data.

 

  if(value=='a'){

    digitalWrite(led1,HIGH);

    BT.println("LED1 turn on");

  }

  else if(value=='A'){

    digitalWrite(led1,LOW);

    BT.println("LED1 turn off");

  }

  else if(value=='b'){

    digitalWrite(led2,HIGH);

    BT.println("LED2 turn on");

  }

  else if(value=='B'){

    digitalWrite(led2,LOW);

    BT.println("LED2 turn off");

  }

  value=0;

}

 

 

EXERCISE_C (Colour_control_RGB_LED)

To use this exercise you also have to download an application, this time you can download it from edutronix.eu. This application can be connected like Bluetooth terminal of previous exercise.

The Android app, send to Arduino 4 string digits, three for value and one for led numeration. On the Arduino code this digits are converted into integer and separate in value, and led.

Code:

#include <SoftwareSerial.h>   // Include  SoftwareSerial  library  

SoftwareSerial BT(10,11);    /*Define pins Rx and Tx on Arduiono, which are connected to Bluetooth.*/

 

First, declare variables and set led as outputs in setup

 

String strGeral;

 

int red = 3;

int blue = 5;

int green = 6;

 

int received;

int value;

int led;

 

void setup () {

  Serial.begin(9600);//Initialize serial port which has been created.

  BT.begin(9600);//Initialize serial port.

  pinMode(red, OUTPUT);

  pinMode(blue, OUTPUT);

  pinMode(green, OUTPUT);

}

 

 

On loop, strGeral increments itself one by one from data read on BT e.g. If Bluetooth sends 1258 from android app the code interprets 1+2+5+8 and save it in strGeral.

Loop only execute the code if BT is available (first condition).

void loop () {

  if (BT.available())  {

    strGeral = String("");

    while (BT.available()) {

      strGeral = strGeral + char(BT.read());

      delay(1);

    }

 

    //    divideDados();

 

    received = strGeral.toInt();

 

received store strGeral value converted to integer. On the other hand, received value is separated in two parts: value that stores a value, which is going to write on RGB LED, and led (number of color).

 

    Serial.println(value);

 

    led = received % 10;

    value = received / 10;

    value=map(value,0,255,255,0);

 

On the last, first condition only admits values between 0 and 255 (to avoid bad data transfers). Secondly, led is evaluate and compare to write value in the correct color.

 

    if (value >= 0 && value <= 255) {

      if (led == 1) {

        analogWrite(red, value);

      }

      else if (led == 2) {

        analogWrite(green, value);

      }

      else if (led == 3) {

        analogWrite(blue, value);

      }

    }

 

  }

}


Last modified: Tuesday, 21 January 2020, 10:31 AM