Arduino: Bluetooth Tutorial

Image
As a first post it makes sense to cover one of the more useful Arduino applications: Communication via bluetooth.

There are several methods of transferring data over from one Arduino board to another such as RF modules, Bluetooth modules, Zigbee etc. Selecting a wireless communication module will depend on the range between devices, the frequency and amount of data transferred. The HC-05 bluetooth module is a good solution for short range communication.

Here are two documents that cover the specs and a list of commands used by the module:

 Link One 
 Link Two 

443453428_815



Start by connecting the RX and TX pins on the bluetooth module to the RX and TX pins on the Arduino.

Arduino RX-> BT TX
Arduino TX-> BT RX

Next Connect the 3.3V pin to the 3.3V pin of the Arduino, the ground pin to ground and the Reset pin on the BT module to pin 12 of the Arduino. Finally, connect the PIO11 pin to digital pin 8 on the Arduino. The same configuration should be used for Arduino #2.

Now that we have 2 Arduino boards, each setup with its own bluetooth module programming can begin.

The code below transmits the letters a and b, one after the other, at one second intervals.

Transmitter Code
#include <SoftwareSerial.h> //Software Serial Port
#define RxD 10 //to TxD on Module
#define TxD 11 //to RxD on Module
#define Reset 12
#define PIO11 8
#define Led 13

SoftwareSerial blueToothSerial(RxD,TxD);

void setup()
{
Serial.begin(9600);
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
pinMode(Led, OUTPUT);
pinMode(PIO11, OUTPUT);
digitalWrite(PIO11, HIGH);
pinMode(Reset, OUTPUT);
digitalWrite(Reset, LOW);
setupBlueToothConnection();

digitalWrite(Led,HIGH);
}

void loop()
{
blueToothSerial.print('a');
delay(1000);
blueToothSerial.print('b');
delay(1000);
}

void setupBlueToothConnection()
{
enterATMode();
sendATCommand();
sendATCommand("NAME=hc");
sendATCommand("UART=57600,0,0");
sendATCommand("ROLE=1"); //Label as Master
sendATCommand("PSWD=1234");
sendATCommand("PAIR=0012,12,211183");//address of bluetooth module
sendATCommand("BIND=0012,12,211183");
sendATCommand("CMODE=1");
enterComMode();
}

void resetBT()
{
digitalWrite(Reset, LOW);
delay(2000);
digitalWrite(Reset, HIGH);
}

void enterComMode()
{
blueToothSerial.flush();
delay(500);
digitalWrite(PIO11, LOW);
resetBT();
delay(500);
blueToothSerial.begin(57600);
}

void enterATMode()
{
blueToothSerial.flush();
delay(500);
digitalWrite(PIO11, HIGH);
resetBT();
delay(500);
blueToothSerial.begin(38400);

}

void sendATCommand(char *command)
{
blueToothSerial.print("AT");
if(strlen(command) > 1){
blueToothSerial.print("+");
blueToothSerial.print(command);
delay(100);
}
blueToothSerial.print("\r\n");
}

void sendATCommand()
{
blueToothSerial.print("AT\r\n");
delay(100);
}

Receiver Code

#include <SoftwareSerial.h>//Software Serial Port
#define RxD 10
#define TxD 11
#define Reset 12
#define PIO11 8
#define Led 13

SoftwareSerial blueToothSerial(RxD,TxD);

void setup()
{
Serial.begin(9600);
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
pinMode(Led, OUTPUT);
pinMode(PIO11, OUTPUT);
digitalWrite(PIO11, HIGH);
pinMode(Reset, OUTPUT);
digitalWrite(Reset, LOW);
setupBlueToothConnection();
// digitalWrite(Led,HIGH); //Indicate Connection is Established
}

void loop()
{
char recvChar;
if(blueToothSerial.available()){//check if there's any data sent from bluetooth master
recvChar = blueToothSerial.read();
Serial.println(recvChar);

}
}

void setupBlueToothConnection()
{
enterATMode();
sendATCommand();
sendATCommand("NAME=2nd");
sendATCommand("UART=57600,0,0");
sendATCommand("ROLE=0"); // Label as Slave
sendATCommand("PSWD=1234");
sendATCommand("CMODE=1");
enterComMode();
}

void resetBT()
{
digitalWrite(Reset, LOW);
delay(2000);
digitalWrite(Reset, HIGH);
}

void enterComMode()
{
blueToothSerial.flush();
delay(500);
digitalWrite(PIO11, LOW);
resetBT();
delay(500);
blueToothSerial.begin(57600);
}

void enterATMode()
{
blueToothSerial.flush();
delay(500);
digitalWrite(PIO11, HIGH);
resetBT();
delay(500);
blueToothSerial.begin(38400);

}

void sendATCommand(char *command)
{
blueToothSerial.print("AT");
if(strlen(command) > 1){
blueToothSerial.print("+");
blueToothSerial.print(command);
delay(100);
}
blueToothSerial.print("\r\n");
}

void sendATCommand()
{
blueToothSerial.print("AT\r\n");
delay(100);
}

The Arduino will transmit one character at a time therefore, for long messages it might be useful to embed key characters into the message being sent to distinguish between data/words/phrases/sentences; ex. If you transmit accelerometer data for x,y and z axis with values 12, 15, 0, this message will be received as "1" "2" "1" "5" "0", one character at a time. To distinguish between each angle value the message sent can be in the form "12,15,0f". Using this setup the comma (,) character can be used to distinguish between each angle and the "f" character can identify the end of the message. These values can be easily stored into an array and displayed after all angle values have been received.

*Note: give adequate time between sent messages to avoid missed characters on the receiver side.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s