若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。
本文主要是參考 [雙A計劃] Part1:App Inventor 經由藍牙控制 Arduino LED 亮滅 來測試的,所用的藍芽模組為HC-05,首先要讓它進入指令模式(AT模式),才能更改傳輸速率(Baudrate)。進入指令模式需要在給電之前,將EN腳位接在高電位(3.3V或5V),再來需要先燒錄與HC-05溝通的Arduino韌體,此部分可參考 HC-05與HC-06藍牙模組補充說明(三):使用Arduino設定AT命令。
本文主要是參考 [雙A計劃] Part1:App Inventor 經由藍牙控制 Arduino LED 亮滅 來測試的,所用的藍芽模組為HC-05,首先要讓它進入指令模式(AT模式),才能更改傳輸速率(Baudrate)。進入指令模式需要在給電之前,將EN腳位接在高電位(3.3V或5V),再來需要先燒錄與HC-05溝通的Arduino韌體,此部分可參考 HC-05與HC-06藍牙模組補充說明(三):使用Arduino設定AT命令。
步驟一:將藍芽模組接上 Arduino UNO板子
整個系統的電路圖如下:
硬體材料:
1. Arduino UNO R3 板子 x 1
2. 公對母杜邦線 x 4
3. 公對公杜邦線 x 2
4. 藍芽模組HC-05 x 1
5. LED燈 x 1
6. 220歐姆電阻 x 1
7. 麵包板 x 1
請依照下面接線方式先將HC-05接至 Arduino 上,
HC-05 Key(EN) --> Arduino 3.3V
HC-05 VCC --> Arduino 5V
HC-05 GND --> Arduino GND
HC-05 TXD --> Arduino Digital Pin 10
HC-05 RXD --> Arduino Digital Pin 11
注意:在上電給HC-05之前,需要壓住HC-05模組上的按鍵,如下圖所示:
步驟二:上傳與HC-05通訊的程式到Arduino UNO板子
請利用Arduino IDE 將下面程式燒錄到UNO板子
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 修改自 http://swf.com.tw/?p=712 | |
#include <SoftwareSerial.h> // 引用程式庫 | |
// 定義連接藍牙模組的序列埠 | |
SoftwareSerial BT(10, 11); // 接收腳, 傳送腳 | |
char val; // 儲存接收資料的變數 | |
void setup() { | |
Serial.begin(9600); // 與電腦序列埠連線 | |
Serial.println("BT is ready!"); | |
// 設定藍牙模組的連線速率 | |
BT.begin(38400); | |
} | |
void loop() { | |
// 若收到「序列埠監控視窗」的資料,則送到藍牙模組 | |
if (Serial.available()) { | |
val = Serial.read(); | |
BT.print(val); | |
} | |
// 若收到藍牙模組的資料,則送到「序列埠監控視窗」 | |
if (BT.available()) { | |
val = BT.read(); | |
Serial.print(val); | |
} | |
} |
開啟Arduino IDE 中的序列埠監控視窗:
會看到下圖,請注意下圖紅色框框內要選擇 NL & CR,這是因為HC-05需要的設定,不然模組不會有反應:
輸入查詢HC-05傳輸速率的指令:AT+UART?
若傳輸速率不為9600時,請輸入設定的指令:AT+UART:9600,0,0
設定成功的話,HC-05 應該會回應「OK」,如下圖:
至此,藍芽模組的設定已完成。
步驟三:上傳控制LED燈的程式到Arduino UNO板子
請將下面程式上傳到Arduino UNO:
請將下面程式上傳到Arduino UNO:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 修改自 http://blog.cavedu.com/programming-language/appinventor/%E9%9B%99a%E8%A8%88%E5%8A%83-part1%EF%BC%9Aapp-inventor-%E7%B6%93%E7%94%B1%E8%97%8D%E7%89%99%E6%8E%A7%E5%88%B6-arduino-led-%E4%BA%AE%E6%BB%85/ | |
#include <SoftwareSerial.h> | |
#include <Wire.h> | |
SoftwareSerial LED_BT(10,11); //定義 PIN10 及 PIN11 分別為 RX 及 TX 腳位 | |
int ledPin = 3; | |
void setup() { | |
Serial.begin(9600); //Arduino起始鮑率:9600 | |
LED_BT.begin(9600); //藍牙鮑率:9600 注意!每個藍牙晶片的鮑率都不太一樣,請務必確認 | |
pinMode(ledPin, OUTPUT); //設定 ledPin 為輸出,LED就接在這 | |
} | |
void loop() { | |
char cmd[20]; | |
int insize; | |
while(1){ | |
if( (insize = (LED_BT.available())) > 0) { //讀取藍牙訊息 | |
Serial.print("size = "); | |
Serial.println(insize); | |
for (int i=0; i<insize; i++){ | |
Serial.print(cmd[i]=LED_BT.read()); | |
Serial.print(" "); | |
} | |
} | |
if( insize > 0 ) { | |
int brightness = cmd[0]; | |
Serial.print("Org B:"); | |
Serial.println(brightness); | |
if( brightness < 0 ) // 因為Android是二補數系統,所以當數值小於零時,須加上256 | |
brightness = 256 + brightness; | |
Serial.print("B:"); | |
Serial.println(brightness); | |
analogWrite( ledPin, brightness ); | |
} //If | |
} //while | |
}//loop |
請參考此文章【雙A計劃】Part1:App Inventor 經由藍牙控制 Arduino LED 亮滅製作 Android App。
示範影片:
沒有留言:
張貼留言