Sei sulla pagina 1di 6

HC-05 Bluetooth Module

The HC-05 Bluetooth module is the most economical and easiest way to go wireless. (Via Bluetooth)

This module makes it easy for you to wirelessly extend your serial interface, so you can control any program
running on your Laptop with serial port interface.

The 4 pins are:

VCC (Power 3.3 6V);


GND;
TXD;
RXD;

Default pairing code: 1234

Default baudrate: 9600

Usually this Bluetooth module is used with the Arduino, but it can be used with others
microcontrollers devices. Here its how its wired with an Arduino:

Two common mistakes:

You need to remove the RX and TX cables when youre uploading the sketch to your
Arduino.
Sometimes people connect the TX from the Bluetooth module to the TX of the
Arduino thats wrong and it wont work. Make sure you connect it properly, the TX
into RX and the RX into the TX.

Note:
If the HC-05 Bluetooth Module asks for a password, Its 1234.
Upload this sketch below!

Make sure you remove the wires from RX and TX otherwise the code wont upload properly!

In the wiring diagram I am using a voltage divider because I believe it will be simpler
for most people to acquire the resistors than the IC. You only need to drop the
Arduino's TX voltage to 3.3V to match the RX of the HC-05. The Arduino's RX pin can
handle the HC-05 incoming TX signal which is 3.3V.

I used 2K and 1K ohms resistors in my circuit diagram to drop 5V to 3.3V but you can
use different resistor values.
Wiring code with default Rx , Tx

char val; // variable to receive data from the serial port


int ledpin = 13; // LED connected to pin 13 (on-board LED)

void setup() {
pinMode(ledpin, OUTPUT); // pin 48 (on-board LED) as OUTPUT
Serial.begin(9600); // start serial communication at 9600bps

void loop() {
if( Serial.available() ) // if data is available to read
{
val = Serial.read(); // read it and store it in 'val'
}
if( val == 'H' ) // if 'H' was received
{
digitalWrite(ledpin, HIGH); // turn ON the LED
} else {
digitalWrite(ledpin, LOW); // otherwise turn it OFF
}
delay(100); // wait 100ms for next reading

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 " .

#include <SoftwareSerial.h>

SoftwareSerial bluetooth(10, 11); // RX, TX


int ledGRN = 4;
int ledBLU = 5;
int ledRED = 6;
int ledWHI = 7;
int BluetoothData;

void setup()
{
bluetooth.begin(9600);
pinMode(ledGRN,OUTPUT);
pinMode(ledBLU,OUTPUT);
pinMode(ledRED,OUTPUT);
pinMode(ledWHI,OUTPUT);
}

void loop()
{
if (bluetooth.available())
{
BluetoothData=bluetooth.read();

if(BluetoothData==1)
{
digitalWrite(ledGRN,1);
bluetooth.println(GREEN ON);
}

if(BluetoothData==2)
{
digitalWrite(ledBLU,1);
bluetooth.println(BLUE ON);
}

if(BluetoothData==3)
{
digitalWrite(ledRED,1);
bluetooth.println(RED ON);
}

if(BluetoothData==4)
{
digitalWrite(ledWHI,1);
bluetooth.println(WHITE ON);
}

if (BluetoothData==0)
{
digitalWrite(ledGRN,0);
digitalWrite(ledBLU,0);
digitalWrite(ledRED,0);
digitalWrite(ledWHI,0);
bluetooth.println(LEDS OFF);
}

}
delay(100);
}
Arduino AND Bluetooth HC-05
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 .

To make a link between your Arduino and bluetooth , do the following :

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


2) Search for new device , Our bluetooth module will appear as HC-05 , and add it
3) The pairing code will be 1234 .
4)after make a pairing , we can now program the arduino and upload a sketch to
send or receive data from Computer.

this program below allow 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 !

The code below :

// This program shown how to control arduino from PC Via Bluetooth


// Connect ...
// arduino>>bluetooth
// D11 >>> Rx
// D10 >>> Tx

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

SoftwareSerial blueTooth(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:
blueTooth.begin(9600);
blueTooth.println("Bluetooth On please press 1 or 0 blink LED ..");
pinMode(ledpin,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
if (blueTooth.available()){
BluetoothData=blueTooth.read();
if(BluetoothData=='1'){ // if number 1 pressed ....
digitalWrite(ledpin,1);
blueTooth.println("LED On D13 ON ! ");
}
if (BluetoothData=='0'){// if number 0 pressed ....
digitalWrite(ledpin,0);
blueTooth.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

Potrebbero piacerti anche