Arduino Interfacing with Bluetooth HC-05

Arduino Interfacing with Bluetooth HC-05

Arduino Interfacing with Bluetooth HC-05

Arduino AND Bluetooth HC-05 Connecting Easily

Hello Everybody, How to connect Arduino with BlueTooth.

The Bluetooth module I will use today is HC-05 which is so familiar and cheap,

Most tutorials on The website Connect the Bluetooth with default Rx and Tx on the Arduino Board, I faced a lot of problems and Bluetooth didn’t work well.

But Arduino  support something Called Software Serial, which allows You to change any Arduino board pin to a serial pin

http://arduino.cc/en/Reference/SoftwareSerial

so After reading this article you will be able to:

1) Connect Arduino Board with PC By Bluetooth, to send and receive data.
2)Connect Arduino Board with Any android device.

so you can send your information, Like Sensors reading, from Arduino to PC Or android device, and you can build your Home automation system by BlueTooth, and controlling your robot wirelessly

 

 Material and Connection

you need to do this experiment :

1) Arduino Board ” I used Arduino Uno “.
2) Bluetooth module HC-05.
3)Solderless jumper.
4)Bread Board.
5)Battery 9V “Optional”.

The connection between  Arduino and BlueTooth is like the schematic above

Arduino Interfacing with Bluetooth HC-05 FK9L6ZAHH2VNS6Y

 

Connect Arduino With PC

We now want to send or receive data between Arduino and computer, first, we need to make a Communication link to Definition Arduino Board to the computer.

We will need a software called Tera Term to show the data received or what we want to send through it.

You can download Tera Term or any terminal emulator software, you can download Tera term  from this link :

http://hp.vector.co.jp/authors/VA002416/ttermv14.zip

To link your Arduino and BlueTooth, do the following :

1) Go to the  BlueTooth icon, right-click and select Add a Device

2) Search for the new device, Our BlueTooth module will appear as HC-05 and add it

3) The pairing code will be 1234.

4)after making a pairing, we can now program the Arduino and upload a sketch to send or receive data from Computer.

Arduino Interfacing with Bluetooth HC-05 FHD98MYHH2VNS6I

Arduino Interfacing with Bluetooth HC-05 FKP02S9HH2VNS6K

Arduino Code

As I mentioned before, I will use the software serial library to make pin D10 & D11 As Tx & Rx instead of using the default Rx and tx ” D0 &D1 On most Arduino Board “.

this program below allows us to control LED connected to D13 To blink on/off, by press # 1 from PC Keyboard the LED blink on, and if we press 0 LED blink off!

To send the Control commands from Computer to Arduino, Go to the tera term, Run it, and choose Serial, and select the BlueTooth Serial from the list as shown on the picture.

The code below :

// This program shown how to control arduino from PC Via Bluetooth
// Connect …
// arduino>>bluetooth
// D11   >>>  Rx
// D10   >>>  Tx
//Written By Mohannad Rawashdeh
//for http://www.genotronex.com/

// you will need Arduino 1.0.1 or higher to run this sketch

#include <SoftwareSerial.h>// import the serial library

SoftwareSerial Genotronex(10, 11); // RX, TX
int ledpin=13; // led on D13 will show blink on / off
int BluetoothData; // the data given from Computer

void setup() {
// put your setup code here, to run once:
Genotronex.begin(9600);
Genotronex.println(“Bluetooth On please press 1 or 0 blink LED ..”);
pinMode(ledpin,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
if (Genotronex.available()){
BluetoothData=Genotronex.read();
if(BluetoothData==’1′){   // if number 1 pressed ….
digitalWrite(ledpin,1);
Genotronex.println(“LED  On D13 ON ! “);
}
if (BluetoothData==’0′){// if number 0 pressed ….
digitalWrite(ledpin,0);
Genotronex.println(“LED  On D13 Off ! “);
}
}
delay(100);// prepare for next data …
}

After uploading This sketch go to tera term and press 0 or 1 and see the results

 

Connect Arduino to Android Device

Connect Arduino to Android Device

after we finished connecting Arduino with PC By Bluetooth, let’s move to how we can connect Arduino To android device.

at first, you need a terminal emulator on your android device to send or receive data to Arduino.

You can download this app from Google play.

https://play.google.com/store/apps/details?id=arduino.bluetooth.terminal&feature=search_result#?t=W251bGwsMSwxLDEsImFyZHVpbm8uYmx1ZXRvb3RoLnRlcm1pbmFsIl0.

after that, you can use the same Arduino Sketch and control LED Blinking on or Off from an android device.

just type and t send #1 to make LED Blink on, or 0 to blink off.

 

Receiving Data From Arduino

The last Arduino Sketch that I wrote, used to send commands from PC Or android device to android, Now in this program, I will use Arduino to Calculate the time since the start of the program in a second, and send it Via BlueTooth to any pairing device.

the code below

// This program shown how to control arduino from PC Via Bluetooth
// Connect …
// arduino>>bluetooth
// D11   >>>  Rx
// D10   >>>  Tx
//Written By Mohannad Rawashdeh
//for http://www.genotronex.com/

// you will need Arduino 1.0.1 or higher to run this sketch

#include <SoftwareSerial.h>// import the serial library

SoftwareSerial Genotronex(10, 11); // RX, TX
int ledpin=13; // led on D13 will show blink on / off
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)
int ledState = LOW;             // ledState used to set the LED
long Counter=0; // counter will increase every 1 second
void setup() {
// put your setup code here, to run once:
Genotronex.begin(9600);
Genotronex.println(“Bluetooth On please wait….”);
pinMode(ledpin,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
unsigned long currentMillis = millis();

if(currentMillis – previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
Counter+=1;

Genotronex.println(Counter);

// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;

// set the LED with the ledState of the variable:
digitalWrite(ledpin, ledState);
}
}

 

 

Resources

For more articles like this visit: www.eduengteam.com