Arduino: hc05 blue tooth

From OnnoWiki
Jump to navigation Jump to search

Sumber: https://create.arduino.cc/projecthub/electropeak/getting-started-with-hc-05-bluetooth-module-arduino-e0ca81



Components and supplies

  • ElectroPeak HC-05 Bluetooth Serial Wireless Module
  • Arduino UNO R3

Apps and online services

  • Ide web
  • Arduino IDE

About this project

You can read this and other amazing tutorials onElectroPeak's official website

In this tutorial, you will learn how to communicate and send data with Bluetooth using HC05 Bluetooth Module and Arduino board. At the end of this article, you will learn:

  • General information about Bluetooth protocol
  • How to send data using Bluetooth
  • How to send AT-Command to HC05

A Brief Introduction to Bluetooth Communication and Protocol

There are several ways for wireless communication such as NRF, ZigBee, Wi-Fi, and Bluetooth.

Bluetooth protocol; an affordable communication method in PAN network, with a maximum data rate of 1Mb/S, working in a nominal range of 100 meters using 2.4 G frequency is a common way of wireless communicating.

HC05 module is a Bluetooth module using serial communication, mostly used in electronics projects.

HC05 Bluetooth module important specifications:

Working voltage: 3.6V – 5VInternal antenna: YesAutomatic connection to the last device: Yes Sending Data to Arduino via Bluetooth

HC05 module has an internal 3.3v regulator and that is why you can connect it to 5v voltage. But we strongly recommend 3.3V voltage, since the logic of HC05 serial communication pins is 3.3V. Supplying 5V to the module can cause damage to the module.

In order to prevent the module from damages and make it work properly, you should use a resistance division circuit (5v to 3.3v ) between arduino TX pin and module RX pin.

When master and slave are connected, blue and red LEDs on the board blink every 2seconds. If they aren’t connected, only blue one blinks every 2 seconds.

Circuit

Hc05-bluetooth-module-cuircuit--600x375 AAxGiNhlFP.jpg


Hc05-bluetooth-module-circuit-2-600x317 Lj33u89gqS.jpg


Code

In order to communicate with HC05 using Bluetooth, you need a Bluetooth terminal application on your phone. You can use this one.Now for start transferring data, upload this code on your Arduino and connect HC05 using the app you have just installed. Communication name is HC05, the password is 1234 or 0000 and the transfer baud rate is 9600 by default.

/*   
HC05 - Bluetooth AT-Command mode  
modified on 10 Feb 2019 
by Saeed Hosseini 
https://electropeak.com/learn/ 
*/ 
#include <SoftwareSerial.h> 
SoftwareSerial MyBlue(2, 3); // RX | TX 
int flag = 0; 
int LED = 8; 
void setup() 
{   
 Serial.begin(9600); 
 MyBlue.begin(9600); 
 pinMode(LED, OUTPUT); 
 Serial.println("Ready to connect\nDefualt password is 1234 or 000"); 
} 
void loop() 
{ 
 if (MyBlue.available()) 
   flag = MyBlue.read(); 
 if (flag == 1) 
 { 
   digitalWrite(LED, HIGH); 
   Serial.println("LED On"); 
 } 
 else if (flag == 0) 
 { 
   digitalWrite(LED, HIGH); 
   Serial.println("LED Off"); 
 } 
}  

Let’s take a deeper look at the code and see what each line means:

#include "SoftwareSerial.h"

library you need for software serial communication. You can download it here.

SoftwareSerial MyBlue(2, 3);  

Software definition for serial pins; RX2 & TX3

MyBlue.begin(9600);  

Configuring software serial baud rate at 9600  

void loop() 
{ 
 if (MyBlue.available()) 
   flag = MyBlue.read(); 
 if (flag == 1) 
 { 
   digitalWrite(LED, HIGH); 
   Serial.println("LED On"); 
 } 
 else if (flag == 0) 
 { 
   digitalWrite(LED, HIGH); 
   Serial.println("LED Off"); 
 } 
} 

Reading serial data and Turning LEDs On/Off accordingly. Sending AT-Commands to HC05 Bluetooth Module and Changing Its Settings

By pressing and holding the button the module switches into AT-command mode. Otherwise, it works in the communication mode.Some modules have a push button in their packages and there is no need to add one anymore.The default baud rate to enter At-command mode is 38400. Now upload this code on your board and set commands using Serial Monitor.

/* 
HC05 - Bluetooth AT-Command mode 
modified on 10 Feb 2019 
by Saeed Hosseini 
https://electropeak.com/learn/guides
*/ 
#include "SoftwareSerial.h"
SoftwareSerial MyBlue(2, 3); // RX | TX 
void setup() 
{ 
 Serial.begin(9600); 
 MyBlue.begin(38400);  //Baud Rate for AT-command Mode.  
 Serial.println("***AT commands mode***"); 
} 
void loop() 
{ 
 //from bluetooth to Terminal. 
 if (MyBlue.available()) 
   Serial.write(MyBlue.read()); 
 //from termial to bluetooth 
 if (Serial.available()) 
   MyBlue.write(Serial.read());
}

You will receive the RESPONSE by sending a COMMAND to the module. Here are some of the most important AT commands:

Untitled HtnrnxyUuo.png.jpeg

code 1 Arduino

/*   
HC05 - Bluetooth AT-Command mode  
modified on 10 Feb 2019 
by Saeed Hosseini 
https://electropeak.com/learn/ 
*/ 
#include <SoftwareSerial.h> 
SoftwareSerial MyBlue(2, 3); // RX | TX 
int flag = 0; 
int LED = 8; 
void setup() 
{   
  Serial.begin(9600); 
  MyBlue.begin(9600); 
  pinMode(LED, OUTPUT); 
  Serial.println("Ready to connect\nDefualt password is 1234 or 000"); 
} 
void loop() 
{ 
  if (MyBlue.available()) 
    flag = MyBlue.read(); 
  if (flag == 1) 
  { 
    digitalWrite(LED, HIGH); 
    Serial.println("LED On"); 
  } 
  else if (flag == 0) 
  { 
    digitalWrite(LED, HIGH); 
    Serial.println("LED Off"); 
  } 
}   


code 2 Arduino

void loop() 
{ 
  if (MyBlue.available()) 
    flag = MyBlue.read(); 
  if (flag == 1) 
  { 
    digitalWrite(LED, HIGH); 
    Serial.println("LED On"); 
  } 
  else if (flag == 0) 
  { 
    digitalWrite(LED, HIGH); 
    Serial.println("LED Off"); 
  } 
} 


code 3 Arduino

/* 
HC05 - Bluetooth AT-Command mode 
modified on 10 Feb 2019 
by Saeed Hosseini 
https://electropeak.com/learn/guides
*/ 
#include "SoftwareSerial.h"
SoftwareSerial MyBlue(2, 3); // RX | TX 
void setup() 
{ 
  Serial.begin(9600); 
  MyBlue.begin(38400);  //Baud Rate for AT-command Mode.  
  Serial.println("***AT commands mode***"); 
} 
void loop() 
{ 
  //from bluetooth to Terminal. 
  if (MyBlue.available()) 
    Serial.write(MyBlue.read()); 
  //from termial to bluetooth 
  if (Serial.available()) 
    MyBlue.write(Serial.read());
}



Referensi

Pranala Menarik