Bluetooth Module
Bluetooth Module
char ch;
void setup() {
Serial.begin(9600);
pinMode(13,OUTPUT);
}
void loop() {
if(Serial.available())
{
ch=Serial.read();
if(ch=='h')
{
digitalWrite(13,HIGH);
}
if(ch=='l')
{
digitalWrite(13,LOW);
}
}
}
In the above code if h (without quotes) is entered in the Serial port the LED
(which is interconnected to 13th pin of Arduino) will glow. If l (without quotes)
is entered the LED will turn off. Now upload the code to Arduino and pair with
HC-05 and open the COM port to which HC-05 is connected. Now enter the
commands h or l to control the Arduino. Thus, successfully you controlled the
Arduino using HC-05. You can make the changes to code as per your
requirement.
Note: Here the HC-05 TX and RX are connected to RX (0) and TX (0) of
Arduino Uno. If you use other Arduino there are more than one serial ports. So
while writing the code write correct serial port number to which RX and TX of
HC-05 are connected.
For example if you use Arduino MEGA there are 4 serial ports. Consider if TX
and RX of HC-05 are connected to RX (1) and TX (1) of MEGA then replace
Serial with Serial1 in code and upload it.
It is quite simple to pair a Bluetooth module from your Android phone. But
bit tricky to pair it with another HC-05 module. In this post Ill describe
the method of pairing 2 Bluetooth modules. One of the module is assigned
ROLE as MASTER & the other left as SLAVE.
By default all HC-05 modules are SLAVES. Using AT commands the
module can be configured as we like.
To configure the SLAVE we make use of an Arduino UNO board. Not
much of configuration needed for slave. We can leave it to defaults. But to
know the ADDRESS of the slave youve to follow this procedure.
ARDUINO HC05
Rx (pin0) > Rx
TX (pin1) > TX
Now provide the USB cable power to Arduino. The HC-05 module enters
the Command mode with Baud Rate 38400.
Open the Serial Monitor of Arduino.
Ensure to select BOTH NL & CR & Baud Rate as 38400 at the bottom
of the serial monitor. This is very important as the Bluetooth module HC-
05 expects both Carriage Return and Line Feed after every AT command.
AT+NAME=HC05_SLAVE
While using this address in AT commands you should replace the colon
with a comma, like
14, 2,110007
An Introduction to Bluetooth Modules 11 | P a g e
Now remove the KEY connection from the HC05 module & disconnect
the power.
Again provide the power to see STATUS LED on the module blinking fast
indicating that it is looking for a PAIR.
Now we have the slave Bluetooth module ready. Now we have to setup the
master Bluetooth module.
AT+NAME=HC05_MASTER
Now, type the following AT Command to pair with the slave module.
AT+LINK=14, 2,110007
If it returns OK it means the module is set to pair with the slave module
when both module are switched on at same time.
We generally program the Arduino board USB cable. But using HC-05 Bluetooth
module we can program the Arduino board without USB cable that is by
/* Serial Loop */
#include <SoftwareSerial.h>
#define rxPin 8
#define txPin 9
void setup() {
Serial.begin(9600);
Serial.println("AT");
mySerial.begin(38400);
mySerial.println("AT");
}
void loop() {
while (mySerial.available()) {
myChar = mySerial.read();
Serial.print(myChar);
}
while (Serial.available()) {
myChar = Serial.read();
Serial.print(myChar); //echo
mySerial.print(myChar);
}
}
Open the serial console and make sure the baud rate is set to 9600 and
line endings is set to be "Both NL & CR"
Enter the following AT commands into the serial console.
AT
AT+ORGL
AT+ROLE=0
AT+POLAR=1,0
AT+UART=115200,0,0
AT+INIT
References
https://alselectro.wordpress.com/2014/10/18/bluetooth-module-hc-
05how-to-pair-2-modulesat-command-walkthrough/
https://alselectro.wordpress.com/2014/10/21/bluetooth-hc05-how-to-pair-
two-modules/
http://www.bluetooth.com/Pages/How-It-Works.aspx
http://makezine.com/projects/diy-arduino-bluetooth-programming-shield/
An Introduction to Bluetooth Modules 23 | P a g e