顯示具有 uart 標籤的文章。 顯示所有文章
顯示具有 uart 標籤的文章。 顯示所有文章

2014年2月25日 星期二

使用Arduino Leonardo連結HC-05 BT 模組 - 設定與測試

Arduino Leonardo有兩個serial port:
  1. Serial: 與PC連結的USB模擬
  2. Serial1: Leonardo pin 0/pin 1
可以透過Leonardo做中介,使用PC直接設定HC-05,也可使用PC測試HC-05在一般模式時是否可以正常運作


步驟:
  1. 將HC-05 TX/RX 接到Leonardo的RX(0)/TX(1)
  2. 將HC-05的SET拉HIGH(設定模式) or LOW(一般模式)
  3. Leonardo上電運行範例程式
  4. HC-05上電

模式:
  1. 設定模式
    • 直接使用AT command設定
    • 注意: 當設定HC-05時,PC終端機程式必須設成enter轉換為\r\n,這樣HC-05才會吃command
  2. 一般模式
    • 可透過 BT SPP profile與遠端裝置溝通(PC or android or....)
    • android market 上有好用的免費程式可以用
      • https://play.google.com/store/apps/details?id=mobi.dzs.android.BLE_SPP_PRO



範例程式:
===========================================
void setup() {
  //init PC serial
  Serial.begin(38400); // to PC

  //init BT module serical
  Serial1.begin(38400); // to BT module <==確認藍芽模組baud rate設定正確
}

char inByte=0;
void loop() {
  while(Serial1.available() > 0){
    inByte = Serial1.read();
    Serial.write(inByte);
  }

  while(Serial.available() > 0){
    inByte = Serial.read();
    Serial1.write(inByte);
  }
}



ps.
網路上看到UNO RESET時,會直接將USB UART與PIN 0/1接通,直接當USB轉TTL版子使用,不知道Leonardo是否可以這樣用
http://coopermaa2nd.blogspot.tw/2012/07/arduino-as-usb-to-ttl-adapter.html

HC-05 BT module


HC-05本身IO 3.3v,
通常買來的模組會加一塊子板轉成5V,並加入LED。

一般arduino/8051/pic IO運作在5V,所以買加上子板模組方便許多

主要pin

  1. TX: 接到控制板的RX
  2. RX: 接到控制板的TX
  3. VCC: 接到5V
  4. GND: 接到GND
  5. SET: 設定模式

HC-05有兩種模式:
  1. 控制模式: SET拉high(5V)後將模組上電
  2. 一般模式: SET拉low(0V)後將模組上電

控制模式中可以下AT command來設定模組,一般模式下如果BT與對方SPP接上,則可以當作無線的UART使用

注意!!!
使用AT command設定時,假設是接到PC設定,須在terminal內設定enter對應到\r\n,這樣HC-05才可以吃AT command


HC-05 所有支援的baud rate,通常預設是9600 or 38400,如果不行就只好全部試試看...

  • 4800, 9600, 19200, 38400, 57600, 115200, 23400, 460800, 921600, 1382400


出場預設狀態:
  ①.设备类:0
  ②.查询码:0x009e8b33
  ③.模块工作角色:SlaveMode
  ④.连接模式:指定专用蓝牙设备连接模式
  ⑤.串口参数:波特率—38400bits/s;停止位:1位;校验位:无
  ⑥.配对码:“1234”
  ⑦.设备名称:“H-C-2010-06-01”

常用AT command:

  1. AT
    • 測試模組是否存在,若存在就會回OK
  2. AT+ORGL
    • 回復出場預設值
  3. AT+NAME
    1. 取得目前名稱(藍芽搜尋到的裝置名稱): AT+NAME?
    2. 設定名稱: AT+NAME="XXXXXX"
  4. AT+ROLE
    1. 取得目前工作模式: AT+ROLE?
    2. 設定目前工作模式: AT+ROLE=X
    3. 工作模式
      1. 0: SLAVE
      2. 1: MASTER
      3. 2: SLAVE-LOOP(同SLAVE,但會回傳所有收到的字,應該是測試用)
  5. AT+PSWD
    1. 查看目前密碼: AT+PSWD?
    2. 設定密碼: AT+PSWD="XXXX"
  6. AT+UART
    1. 設定UART: AT+UART=XXX,Y,Z
    2. XXX:
      1. baud rate: 4800, 9600, 19200, 38400, 57600, 115200, 23400, 460800, 921600, 1382400
    3. Y:
      1. stop bit
        1. 0: 1bit
        2. 1: 2bit
    4. Z:
      1. parity bit
        1. 0: none
        2. 1: odd
        3. 2: even


相關文件

  • 模組介紹
    • http://www.wavesen.com/mysys/db_picture/news3/2013911141224101.pdf
    • http://www.exp-tech.de/service/datasheet/HC-Serial-Bluetooth-Products.pdf
  • at command
    • http://www.wavesen.com/mysys/db_picture/news3/2013911155224101.pdf
  • 網路相關文章
    • https://mbed.org/users/edodm85/notebook/HC-05-bluetooth/
    • http://www.yfrobot.com/forum.php?mod=viewthread&tid=4
    • http://taktak.co.uk/2013/bluetooth-module-hc-05-bt_board-1-5-configuration/
    • http://www.instructables.com/id/Arduino-AND-Bluetooth-HC-05-Connecting-easily/