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

沒有留言:

張貼留言