自製Arduino藍牙遙控器 ( Arduino Project: Build a Customized Bluetooth Controller )

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.

一、前言 (Preface)
在製作 Arduino 藍牙遙控器之前,我們先來了解藍牙的使用方式,以手機連接HC-06藍牙模組流程約有底下幾個步驟:
Before building a customized bluetooth controller, there are several steps we need to know about using bluetooth communication. For example, the following steps are for connecting HC-06 with a smart phone:
  1. 開啟手機藍牙。 (Turn on bluetooth on a smart phone)
  2. 尋找 HC-06 藍牙模組。(Scan for the nearby bluetooth devices)
  3. 與 HC-06 藍牙模組配對。(Paire HC-06 with the smart phone)
  4. 開啟手機藍牙App。(Open a BT application on the smart phone)
  5. 使用手機藍牙App與 HC-06 藍牙模組連線。(Use the BT application to connect with HC-06)
此外在上述流程中,手機藍牙是當主控端 (Master),HC-06藍牙模組當從端 (Slave)。而我們需要將以上的流程先建立好在主控端藍牙模組 HC-05內,讓可以當主控端的 HC-05 藍牙模組在上電後就可以自動連線到 HC-06 從端藍牙模組。本文會以 Arduino Nano 藍牙小夜燈 當 Slave 端 (HC-06)與 Arduino UNO 版的雙軸按鍵搖桿當 Master 端(HC-05)。
The role of smart phone is bluetooth master. The role of HC-06 is bluetooth slave. HC-05(master) have to store the bluetooth master configuration in itself. After saving the bluetooth master configuration in HC-05(master), it will connect to HC-06(slave) automatically.
For more details, go to "Connecting 2 Arduinos by Bluetooth using a HC-05 and a HC-06: Pair, Bind, and Link".

此外主控端與從端的速度(baudrate)要一樣,密碼(password)也要一樣。此文所用的 baudrate為 9600,password為 1234。
The baudrate and password must be the same in BT master/slave. In this tutorial, the baudrate is 9600 and the password is 1234.

二、硬體材料(Materials)
  1. Arduino Nano 板子 x 1
  2. Arduino UNO 板子 x 1
  3. Arduino Sensor Shield V5.0 感測器擴展板 x 1
  4. NFU Dream Maker 所設計的 PCB from NUF Dream Maker x 1
  5. RGB LED x 3
  6. 電阻 Resistor 220 ohm x 3
  7. HC-05 藍牙模組 x 1
  8. HC-06 藍牙模組 x 1
  9. 雙軸按鍵搖桿 Joystick  x 1
  10. 母隊母杜邦線 Female to Female Jumper x 8
設定藍牙模組的電路與AT指令請參考 HC-05與HC-06藍牙模組補充說明(二):連接電路
與 HC-05與HC-06藍牙模組補充說明(三):使用Arduino設定AT命令 兩篇文章。
三、藍牙模組的設定(Bluetooth Module Configuration)
HC-06的設定值 (For HC-06)
所需的指令如下(The AT Commands List):
  • AT
  • AT+PIN1234
  • AT+NAMEyour_name
  • AT+BAUD4
成功的結果會類似下圖(The below picture is the successful result)
紅色 1 OK 是 AT 檢查裝置指令回傳結果。(RED 1 OK is the return value of the AT command.)
藍色 2 OKsetPIN 是 AT+PIN1234 設定密碼指令回傳結果。(Blue 2 OKsetPIN is the return value of the AT+PIN1234 command.)
綠色 3 OKsetname 是 AT+NAMEPingLun 設定藍芽名稱指令回傳結果。(Green 3 OKsetname is the return value of the AT+NAMEPingLun command.)
紫色 4 OK9600 是 AT+BAUD4 設定傳輸速度指令回傳結果。(Purple 4 OK9600 is the return value of the AT+BAUD4 command.)

接著可用 Bluetooth Scanner 來找出 HC-06 模組的位址(Address)。
Use Bluetooth Scanner to figure out the MAC address of HC-06.

注:上圖中的位址需轉為 98D3,31,FB5396  的格式,才能給 AT 指令用,此部分可的說明請參考 藍牙模組補充說明(四):無線連結兩個Arduino控制板一文中的藍芽裝置位址一節。
PS: the MAC address must be the 98D3,31,FB5396 for AT command.

HC-05的設定值(For HC-05)
要用AT指令讓HC-05主控端連上HC-06從端時,需將HC-05切換到指令模式,這需要在上電前壓住按鈕(如下圖用夾子夾住),注意 HC-05在 指令模式下的速度為 38400
Hold the reset button on the HC-05 before power on. This will enable HC-05 in command mode. A plastic peg could hold the reset button for you.

所需的指令如下:(The AT Command List)
  • AT+RMAAD 清除已配對的藍牙模組 (Clear the paired BT devices)
  • AT+ROLE=1 設定為 Master (Set the role to Master)
  • AT+BIND=98D3,31,FB5396 (Binding with the MAC address: 98D3,31,FB5396)
有回應OK才算成功。下圖的 ERROR 就是沒成功的例子,此時再下指令直到出現 OK。
Execute every command until it succeeds, got a OK response. The ERROR:(0) response say: "the command need to execute again".

藍牙模組互聯連線成功影片(注意燈號閃爍的速度):
The successful connection video(notice the onboard LED blinking speed)

尚未連線前:HC-06 與 HC-05 的指示燈會快速閃爍。
Before connection: the HC-06 and HC-05 onboard LED blink fast.
連線成功後:HC-06 的指示燈會恆亮;HC-05 的指示燈每兩秒快速閃爍兩次。
After connection: the HC-06 onboard will turn on, not blinking; the HC-05 will blink twice every two seconds.

If you have any problem to pair HC-05 with HC-06, please refer to this tutorial: "How to Configure HC-05 Bluetooth Module As Master and Slave Via AT Command".

四、HC-06 從端藍牙小夜燈程式 (The Code for HC-06 and Nano)
此部分的程式和 Arduino Nano 藍牙小夜燈相同。The code for Arduino Nano with HC-06 is the same with Control a Lamp from Android Phone.

五、HC-05 主控端藍牙搖桿程式(The Code for HC-05 and UNO)
搖桿的控制方式如下圖,箭頭的方向代表增加該顏色的亮度,例如紅線為X軸方向,向右會增加紅色燈的亮度,向左會減少紅色燈的亮度。黑色線為控制三種顏色的亮度。
This diagram shows the mechanism of LED lightness. For example, the red line means the RED lightness will increase from left to right(X Coordinate); the blue line mean the BLUE lightness will increase from top to bottom(Y Coordinate).


將HC-06藍牙小夜燈 與 HC-05藍牙搖桿兩程式分別燒錄好後,就可以用搖桿來控制小夜燈了。
After uploading the Nano(HC-06) and UNO(HC-05), the bluetooth lamp could be controlled remotely by a bluetooth joystick.


參考資料:
[1] HC-05與HC-06藍牙模組補充說明(一)
[2] HC-05與HC-06藍牙模組補充說明(二):連接電路
[3] HC-05與HC-06藍牙模組補充說明(三):使用Arduino設定AT命令
[4] 藍牙模組補充說明(四):無線連結兩個Arduino控制板
[5] Connecting 2 Arduinos by Bluetooth using a HC-05 and a HC-06: Pair, Bind, and Link

沒有留言: