1. HC-05 藍芽模組連線
●軟硬體環境準備
※硬體器材
HC-05 藍芽模組(廢話 XD)
電腦一台-用以編輯、編譯程式
Arduino UNO-用以控制藍芽模組連線
Android 手機一支-用以執行連線 App
USB 線材-用以連接 Arduino UNO 與電腦
杜邦線或其他線材-用以連接藍芽模組與 Arduino UNO
麵包板-用於連接周邊元件
※開發軟體工具
Arduino 開發環境-https://www.arduino.cc/en/Main/Software
Android Studio 和 SDK Tools-
https://developer.android.com/studio/index.html
Eclipse IDE for Java EE Developers-
http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/marsr
●藍芽模組主/從(Master/Slave)模式
藍芽連線控制上,分為主控端(Master)設備與從端(Slave)設備,主控端向從端建立連線,
一台主控端(Master)設備可最多控制 7 台從端(Slave)設備。若將 HC-05 作為從端(Slave)設備,
模組通電時需將 KEY 接腳連接至 Ground(即邏輯「0」),便可正常傳輸;若將 HC-05 作為主
控端(Master)設備,電路連接方式與說明請參考以下「●以 AT Command 控制藍芽模組」內容。
●藍芽模組的 LED 燈號
(參考自 http://jackedu.blogspot.tw/2015/01/hc-05.html)
連續的快閃:藍芽等待配對中。
連續的快閃 2 下後停 1 下:藍芽已配對成功,運作中。
連續慢速閃爍(約兩秒一次):藍芽已進入 AT 模式,準備設定。
2. ●以 AT Command 控制藍芽模組
Step1:電路連接
HC-05 RXD → Arduino pin 11
HC-05 TXD → Arduino pin 10
HC-05 GND → Arduino GND
HC-05 VCC50 → Arduino 5V
HC-05 KEY → Arduino pin 9(亦可直接連接至 Vcc)
電路連接說明:
藍芽模組透過 UART 介面傳輸資料,模組上有一組接腳-TxD 與 RxD,應連接至
Arduino UNO 的 RxD 與 TxD,但因 Arduino UNO 硬體定義之 UART 介面已連接至 PC 電
腦端,因此程式需建立一模擬 UART 通訊埠(詳見 Step2,也就是程式中的 BTSerial),軟體
模擬通訊埠可自行定義,在此將 HC-05 模組之 RxD 連接至 Arduino UNO 之 11 腳,TxD 連
接至 Arduino UNO 之 10 腳,因此 Arduino UNO 執行 UART 模擬時,11 腳為 TxD,10 腳
為 RxD。
HC-05 的 KEY 接腳作為藍芽模組工作模式選擇,若通電時連接至 Vcc(即邏輯「1」),
則藍芽模組進入 AT Command 模式,可輸入 AT Command 控制藍芽模組,亦可作為主控端
(Master);若通電時連接至 Ground(即邏輯「0」),則藍芽模組作為 Slave 端執行正常傳輸;
示意如下圖。
Step2 :上 傳至 Arduino 程式參考 如下 : (from http://www.techbitar.com/modify-the-hc-05-
bluetooth-module-defaults-using-at-commands.html)
/*
AUTHOR: Hazim Bitar (techbitar)
DATE: Aug 29, 2013
LICENSE: Public domain (use at your own risk)
CONTACT: techbitar at gmail dot com (techbitar.com)
3. */
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
void setup()
{
pinMode(9, OUTPUT);
// this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
Serial.begin(9600);
Serial.println("Enter AT commands:");
BTSerial.begin(38400); // HC-05 default speed in AT command more
}
void loop()
{
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (BTSerial.available())
Serial.write(BTSerial.read());
// Keep reading from Arduino Serial Monitor and send to HC-05
if (Serial.available())
BTSerial.write(Serial.read());
}
修正上述程式,使終端機顯示已輸入指令:
/*
AUTHOR: Hazim Bitar (techbitar)
Editor: Jimmy Hu
DATE: May 22, 2016
*/
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
void setup()
{
pinMode(9, OUTPUT);
// this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
Serial.begin(9600);
Serial.print("Enter AT commands:");
BTSerial.begin(38400); // HC-05 default speed in AT command more
}
void loop()
4. {
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (BTSerial.available())
{
char BTSerial_read;
// 宣告 BTSerial_read 字元變數,用於記錄 BTSerial.read()回傳字元
BTSerial_read = BTSerial.read();
// 將 BTSerial.read()回傳字元填入 BTSerial_read
Serial.write(BTSerial_read);
if(BTSerial_read == 'n')
{
Serial.print("Enter AT commands:");
}
}
// Keep reading from Arduino Serial Monitor and send to HC-05
if (Serial.available())
{
char Serial_read;
// 宣告 Serial_read 字元變數,用於記錄 Serial.read()回傳字元
Serial_read = Serial.read();
// 將 Serial.read()回傳字元填入 Serial_read
Serial.print(Serial_read);
BTSerial.write(Serial_read);
}
}
修正上述程式,將接腳定義於程式開頭。
/*
AUTHOR: Hazim Bitar (techbitar)
DATE: Aug 29, 2013
LICENSE: Public domain (use at your own risk)
CONTACT: techbitar at gmail dot com (techbitar.com)
http://www.techbitar.com/modify-the-hc-05-bluetooth-module-defaults-using-at-commands.html
Editor: Jimmy Hu
DATE: May 22, 2016
*/
//***函式庫引用區***
#include <SoftwareSerial.h> // 引用<SoftwareSerial.h>函式庫
//***定義變數區***
#define Bluetooth_KEY 9
// 定義藍芽傳輸模組 KEY 接腳連接至 arduino 第 9 接腳
5. #define Bluetooth_RxD 11
// 定義藍芽傳輸模組 RxD 接腳連接至 arduino 第 11 接腳
#define Bluetooth_TxD 10
// 定義藍芽傳輸模組 TxD 接腳連接至 arduino 第 10 接腳
SoftwareSerial BTSerial(Bluetooth_TxD, Bluetooth_RxD);
// 建立軟體定義串列埠 BTSerial,用以控制藍芽模組
void setup() // setup 程式
{ // 進入 setup 程式
pinMode(Bluetooth_KEY, OUTPUT);
// 設定 arduino 連接藍芽傳輸模組 KEY 之接腳為輸出
digitalWrite(Bluetooth_KEY, HIGH);
// 設定藍芽傳輸模組 KEY 接腳為 HIGH(進入 AT command 模式)
Serial.begin(38400);
// 開啟 Serial Port 透過 USB(uart)方式與電腦通信,鮑率為 38400bps (Bits Per Second)
Serial.print("Enter AT commands:"); // 透過 USB(uart)傳輸字串"Enter AT commands:"
BTSerial.begin(38400);
// 設定控制 HC-05 藍芽模組之串列埠 BTSerial 鮑率為 38400bps,
// 此亦為 HC-05 藍芽模組預設鮑率
} // 結束 setup 程式
void loop() // loop 程式
{ // 進入 loop 程式
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (BTSerial.available()) // 若連接至藍芽模組之 Serialport 接收到字元
{ // 進入 if 敘述
char BTSerial_read;
// 宣告 BTSerial_read 字元變數,用於記錄 BTSerial.read()回傳字元
BTSerial_read = BTSerial.read(); // 將 BTSerial.read()回傳字元填入 BTSerial_read
Serial.write(BTSerial_read); // 將 BTSerial_read 回傳至電腦
if(BTSerial_read == 'n') // 若 BTSerial_read 為換行字元
{ // 進入 if 敘述
delay(10); // 延遲 10ms
if(BTSerial.available() == 0) // 若資料傳輸結束
{ // 進入 if 敘述
Serial.print("Enter AT commands:");
// 透過 USB(uart)傳輸字串"Enter AT commands:"
} // 結束 if 敘述
} // 結束 if 敘述
} // 結束 if 敘述
// Keep reading from Arduino Serial Monitor and send to HC-05
if (Serial.available())
6. // 若與電腦連線之 Serial Port 接收到字元
{ // 進入 if 敘述
char Serial_read;
// 宣告 Serial_read 字元變數,用於記錄 Serial.read()回傳字元
Serial_read = Serial.read(); // 將 Serial.read()回傳字元填入 Serial_read
Serial.print(Serial_read); // 將 Serial_read 字元回傳至電腦
BTSerial.write(Serial_read); // 將 Serial_read 字元傳送至藍芽模組
} // 結束 if 敘述
} // 結束 loop 程式
Step3:開啟 Arduino IDE 的終端機輸入指令
點選「工具」→「序列埠監控視窗」(或鍵盤之 Ctrl+Shift+M)開啟終端機(如下圖)。
以 Step2 之 Arduino 程式為例,串列傳輸鮑率為 9600baud,故終端機亦選擇相同之鮑率;
另依據參考文件說明,AT command is case- sensitive, should end up with terminator (“enter”
or “rn”),意即「結束字元」需與指令一併傳送,故選擇「Both NL & CR」選項(如下圖)。
7. 看到終端機顯示「Enter AT commands:」即可輸入指令;AT command 說明如下。
※測試(Test)指令-AT
指令格式:
指令 回應 回應參數
AT OK 無
結果如下圖。
※重置(Reset)指令-AT+RESET
指令格式:
指令 回應 回應參數
AT+RESET OK 無
結果如下圖。
8. ※取得軟體版本(soft version)-AT+VERSION?
指令格式:
指令 回應 回應參數
AT+VERSION?
+VERSION: <Param>
OK
Param: Version number
結果如下圖。
※重置藍芽狀態-AT+ORGL
指令格式:
指令 回應 回應參數
AT+ORGL OK 無
藍芽模組重置設定如下:
①.Device type: 0
②.Inquire code: 0x009e8b33
③.Module work mode: Slave Mode
④.Connection mode: Connect to the Bluetooth device specified
9. ⑤.Serial parameter: Baud rate: 38400 bits/s; Stop bit: 1 bit; Parity bit: None.
⑥.Passkey: “1234”
⑦.Device name: “H-C-2010-06-01”
更多指令說明請參考 HC-03/05 Embedded Bluetooth Serial Communication Module AT
command set(http://www.techbitar.com/uploads/2/0/3/1/20316977/hc-05_at_commands.pdf)文件,
或 參 考 Serial Port Bluetooth Module (Master/Slave) : HC-
05(http://wiki.iteadstudio.com/Serial_Port_Bluetooth_Module_(Master/Slave)
_:_HC-05)網頁說明。
10. ●HC-05 藍芽模組從端(Slave)正常傳輸模式
Step1:電路連接
HC-05 RXD → Arduino pin 11
HC-05 TXD → Arduino pin 10
HC-05 GND → Arduino GND
HC-05 VCC50 → Arduino 5V
HC-05 KEY → Arduino 9(亦可直接連接至 GND)
Step2:上傳至 Arduino 程式參考如下:
/*
AUTHOR: Jimmy Hu
DATE: May 23, 2016
*/
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
void setup()
{
pinMode(9, OUTPUT);
// this pin will pull the HC-05 pin 34 (key pin) LOW
digitalWrite(9, LOW);
Serial.begin(9600);
//Serial.print("Receive String:");
BTSerial.begin(115200);
}
void loop()
{
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (BTSerial.available())
{
char BTSerial_read;
// 宣告 BTSerial_read 字元變數,用於記錄 BTSerial.read()回傳字元
BTSerial_read = BTSerial.read();
// 將 BTSerial.read()回傳字元填入 BTSerial_read
Serial.write(BTSerial_read);
}
// Keep reading from Arduino Serial Monitor and send to HC-05
if (Serial.available())
{
15. ●參考資料
HC-05 藍芽端
HC-05 模組圖
http://wiki.iteadstudio.com/images/3/38/HC-05.jpg
藍芽無線傳輸(Bluetooth)
http://www.hightech.tw/index.php/2012-06-06-14-12-38/27-wireless-communication/368-
bluetooth
Modify The HC-05 Bluetooth Module Defaults Using AT Commands(英文網頁)
http://www.techbitar.com/modify-the-hc-05-bluetooth-module-defaults-using-at-commands.html
HC-05 藍芽模組設定(中文網頁)
http://jackedu.blogspot.tw/2015/01/hc-05.html
Bluetooth AT Commands(PDF 檔)
http://www.techbitar.com/uploads/2/0/3/1/20316977/hc-05_at_commands.pdf
HC-05 Bluetooth Module(PDF 檔)
http://cc.ee.ntu.edu.tw/~ultrasound/belab/course_files/02_bio_signal/Exp3_HC05.pdf
Arduino : HC-05 藍芽模組的設定(中文網頁)
http://gsyan888.blogspot.tw/2014/03/arduino-hc-05.html
HC-05 與 HC-06 藍牙模組補充說明(一)(中文網頁)
http://swf.com.tw/?p=693
HC-05 AT 模式設定(中文網頁)
http://mowei-tw.blogspot.tw/2014/04/hc-05-at.html
Bluetooth HC05- How to pair two modules(英文網頁)
https://alselectro.wordpress.com/2014/10/21/bluetooth-hc05-how-to-pair-two-modules/
HC-05 bluetooth RSSI not working with Arduino-stackoverflow.com(英文網頁)
http://stackoverflow.com/questions/23221336/hc-05-bluetooth-rssi-not-working-with-arduino
16. ★Problems connecting reliably using HC-05 (as bluetooth master)
http://arduino.stackexchange.com/questions/16954/problems-connecting-reliably-using-hc-05-as-
bluetooth-master
Android 手機端
Android 藍牙開發(中文網頁)
http://fecbob.pixnet.net/blog/post/35503513-
android%E8%97%8D%E7%89%99%E9%96%8B%E7%99%BC
Android--Bluetooth-Connection-Code-Sample(github.com)(英文網頁)
https://github.com/theappguruz/Android--Bluetooth-Connection-Code-Sample
Android - Bluetooth Connection Demo(英文網頁)
http://www.theappguruz.com/blog/android-bluetooth-connection-demo
Android APP 藍芽範例說明 – BluetoothChat(中文網頁)
http://han-ya.blogspot.tw/2015/10/android-app-bluetoothchat.html
其他參考資料
Arduino 入門教學(14) – 以 Amarino 連接 Android 與 Arduino (作者:Cooper Maa)(網
頁)
http://programmermagazine.github.io/201402/htm/article1.html
Getting started with amarino(線上簡報)
http://www.slideshare.net/coopermaa/getting-started-with-amarino
[ARDUINO]_用 ANDROID 手機經 BLUETOOTH 遙控 ARDUINO 小車(2012/8/9
UPDATE)(中文網頁)
http://www.ntex.tw/wordpress/463.html
Android Studio: Add jar as library?(英文網頁)
http://stackoverflow.com/questions/16608135/android-studio-add-jar-as-library
Have no libs directory in Android Studio(英文網頁)
http://stackoverflow.com/questions/26956827/have-no-libs-directory-in-android-studio
DATA TRANSFER BETWEEN ANDROID AND ARDUINO VIA BLUETOOTH(英文網頁)
http://solderer.tv/data-transfer-between-android-and-arduino-via-bluetooth/
★Android example to communicate with Bluetooth device, HC-06 Bluetooth Module(英文網
頁)
http://android-er.blogspot.tw/2015/07/android-example-to-communicate-with.html
17. ★Android communicate with Arduino + HC-06 Bluetooth Module, part II(英文網頁)
http://android-er.blogspot.tw/2015/10/android-communicate-with-arduino-hc-06.html
Connect Arduino Due with HC-06 (Bluetooth Module)(英文網頁)
http://arduino-er.blogspot.tw/2015/07/connect-arduino-due-with-hc-06.html
Raspberry Pi 3 Bluetooth Setup
https://www.raspberrypi.org/forums/viewtopic.php?t=138145&f=28