2014/09/27

Android control color RGB LED using HC-05 Bluetooth with Arduino (Part I)


Android control color RGB LED using HC-05 Bluetooth with Arduino (Part I)


Part II of this tutorial is here!



This time, I am going to show you how to use Android phone to connect to HC-05 Bluetooth module and Arduino.

Hardware needed:

  • Arduino mini pro * 1
  • HC-05 Bluetooth module * 1
  • RGB LED * 2
  • Jump wires * 10

HC-05 Bluetooth Module wiring as follow:

  • TX connect to Arduino Pin 11(RX)
  • RX connect to Arduino Pin 12(TX)
  • Remember to connect TX on the HC-05 to RX on the Arduino and RX on the HC-05 to TX on the Arduino, so Arduino can receive data correctly.
  • VCC connect to 5V power
  • GND connect to Ground

RGB LED wiring as follow:

Warning: You must connect Arduino mini pro, so your PWM Pin can control LED’s color! For example, Arduino mini pro only has 6 PWM pins and they are 3, 5, 6, 9, 10, and 11, other pins do not have PWM.

Refer to Wikipedia for PWM
  • R connect to Arduino Pin 3
  • Common Anode connect to 5V        
    Note: if you bought common anode LED, then this pin must connect to VCC 5V. If you bought common cathode LED, then this pin must connect to GND
  • G connect to Arduino Pin 5
  • B connect to Arduino Pin 6

Arduino wiring as follow:

  • GND connect to Ground
  • VCC connect to 5V
Look at following chart for the wiring and hardware setup:


If you connect all the parts together, it will look like this:

The completed Arduino code is listed here:


#include <Softwareserial.h>
#include <Wire.h>//Include libraries: SoftwareSerial & Wire
SoftwareSerial BT(11,12); //Define PIN11 & PIN12 as RX and TX pins

//RGB LED Pins
int PIN_RED = 3;
int PIN_GREEN = 5;
int PIN_BLUE = 6;
//RED LED at Pin 13
int RED_LED = 13;
String RGB = ""; //store RGB code from BT
String RGB_Previous = "255.255.255)"; //preserve previous RGB color for LED switch on/off, default White
String ON = "ON"; //Check if ON command is received
String OFF = "OFF"; //Check if OFF command is received
boolean RGB_Completed = false;

void setup() {
  Serial.begin(9600); //Arduino serial port baud rate:9600
  BT.begin(9600);//My HC-05 module default baud rate is 9600
  RGB.reserve(30);

  pinMode(RED_LED, OUTPUT); 
  //Set pin13 as output for LED, 
  // this LED is on Arduino mini pro, not the RGB LED
}

void loop() {
  // put your main code here, to run repeatedly: 
  
  //Read each character from Serial Port(Bluetooth)
  while(BT.available()){
    char ReadChar = (char)BT.read();

    // Right parentheses ) indicates complet of the string
    if(ReadChar == ')'){
      RGB_Completed = true;
    }else{
       RGB += ReadChar;
    }
  }
  
  //When a command code is received completely with ')' ending character
  if(RGB_Completed){
   //Print out debug info at Serial output window
      Serial.print("RGB:");
      Serial.print(RGB);
      Serial.print("     PreRGB:");
      Serial.println(RGB_Previous);
      
      if(RGB==ON){
          digitalWrite(13,HIGH);
          RGB = RGB_Previous; //We only receive 'ON', so get previous RGB color back to turn LED on
          Light_RGB_LED();          

      }else if(RGB==OFF){
          digitalWrite(13,LOW);
          RGB = "0.0.0)"; //Send OFF string to turn light off
          Light_RGB_LED();
      }else{
          //Turn the color according the color code from Bluetooth Serial Port
          Light_RGB_LED();   
          RGB_Previous = RGB;     
      }
      //Reset RGB String  

      RGB = "";
      RGB_Completed = false;
      
    
  } //end if of check if RGB completed
  
} // end of loop

void Light_RGB_LED(){

  int SP1 = RGB.indexOf('.');
  int SP2 = RGB.indexOf('.', SP1+1);
  int SP3 = RGB.indexOf('.', SP2+1);
  String R = RGB.substring(0, SP1);
  String G = RGB.substring(SP1+1, SP2);
  String B = RGB.substring(SP2+1, SP3);

  //Print out debug info at Serial output window
  Serial.print("R=");
  Serial.println( constrain(R.toInt(),0,255));
  Serial.print("G=");
  Serial.println(constrain(G.toInt(),0,255));
  Serial.print("B=");
  Serial.println( constrain(B.toInt(),0,255));
  //Light up the LED with color code

//**2014-09-21
//Because these RGB LED are common anode (Common positive)
//So we need to take 255 to minus R,G,B value to get correct RGB color code
  analogWrite(PIN_RED,  (255-R.toInt()));
  analogWrite(PIN_GREEN, (255-G.toInt()));
  analogWrite(PIN_BLUE,  (255-B.toInt()));

}




You need to go to Google Play Store and download "BT LED Controller"; you need to use it with this tutorial. You may download APP here

This is what Android BT LED Controller App can do:

1.     Connect to Bluetooth
2.     Wireless control LED on/off
3.     Wireless control LED to change the color

The screen of Android APP is below and you can use it easily:

  • Click the ”BT-List” button (The red dot next to it means BT not connected)



  • Next select the Bluetooth you want to connect from the list, after it connected successfully, the APP will jump back to the main screen

  • On the main screen says BT connected (The green dot indicates BT connected)
  • Click LED on / LED off button, you can turn LED on/off
  • Click any color block on the App, the RGB color code will be sent to HC-05 and HC-05 will send the RGB color code to Arduino. RGB code is composed 3 numbers of 1~255 with “)” as the ending character, such as “255.20.10)”. Arduino receives a character at a time from the serial port and it combines all characters together. When a “)” is received, the RGB code is completed.
  • Arduino then decompose RGB code string into 3 sets of numbers for R, G, B, and then send the code to RGB LED to display the color indicated.
  • As shown below (RGB code 255.51.255 will make RGB LED to show magenta)
  • Click Disconnect to cut the connection between Android and Arduino.



You also learn how to use this APP by watching the video below:

Android 藍芽連線控制彩色 RGB LED (Part I)

本次教學將如何用 Android 程式以藍芽方式連線到HC-05藍芽模組及Arduino.

硬體所需材料:
  1. Arduino mini pro * 1
  2. HC-05 Bluetooth module * 1
  3. RGB LED * 2
  4. Jump wires * 10

HC-05 Module 接法:

  • TX接到 Arduino Pin 11 (RX)
  • RX接到 Arduino Pin 12 (TX)
    就是發射端與接收端要反接到Arduino, 這樣 Arduino 才收的到訊號
  • VCC 接 5V 電源
  • GND 接地

RGB LED 接法:

注意: 一定要接 Arduino mini pro  有 PWM 的腳位才能控制LED的顏色!
以 Arduino mini pro PWM 腳位只有 : 3, 5, 6, 9, 10, and 11,其它沒有 PWM.
  • R 接 Arduino Pin 3
  • Common Anode 接 5V
    (我買的是共陽的 LED, 所以這支腳要接 5V,若你買的是共陰的 Cathode, 則要接 GND)
  • G  接 Arduino Pin 5
  • B 接 Arduino Pin 6

Arduino 接法:

  • GND 接地
  • VCC 接 5V
 Arduino, HC-05 module, & RGB LED 電路圖如下:


我把這些材料通通接好如下:



Arduino 的程式碼請參考上方!


請由 Google Play Store 下載 BT_LED_Controller V1.0 與這個程式搭配這個教學使用.
由此下載 APP
Android BT_LED_Controller V1.0 有下列功能:

  1. 藍芽連線
  2. 無線遙控 LED 開/關
  3. 無線遙控 LED 改變它的顏色

Android App 畫面如下,使用方法很簡單:
  • 點選上方 "BT-LIST" 鈕 (右方的小紅點表示藍芽尚未連線)






























  • 接下來點選你要搭配的藍芽 HC-05 模組,連線完成後,程式會跳回到主畫面
























      • 主畫面上顯示 BT Connected (小綠點表示已藍芽連線成功)
      • 按下 LED Turn on / LED Turn Off 可以打開/關閉 LED




      • 點選 App 上的任一顏色方塊,App 會把那個方塊的 RGB code 送到 HC-05,再由 HC-05傳給 Arduino,  RGB code 由3組 1~255的數字組成, 再加上右括號尚作是結束符號, 如 255.20.10), 當 Arduino 由 serial port 一個字元一個字元接收, 一直收到 ) 時,才當作是一個完整的 RGB code。
      • Arduino 把 RGB code 字串解析後,再送給 RGB LED 顯示指定的顏色
        如下圖 (RGB code: 255.51.255 會傳到到 Arduino 顯示出紫紅色)
      • 點選 Disconnect 則關閉 Android 和 Arduino 的連線
      • 完成測試

      請參考底下的影片顯示操作的方法:


      2014/07/19

      法國半自助之行 - 第十二天 回 戴高樂機場 旁飯店 準備回家

      11-Jul Novetel  享用豐盛早餐, Check out/付 6.6 Euro Tax "步行到隔壁機場,用護照到自動登錄機印出登機證。
      機場人員協助辦登錄,入關,嚴格的入關檢查,複領被搜身(因為門牙校正器?)" 買禮品/贈品/Aven 買香水/登機/ 呈呈&Aven是 VIP

      法國半自助之行 - 第十一天 史特拉斯堡 遊

      10-Jul Train to Sasbosborg Clement姐姐兒子家放行李 "逛教堂(Norte-Dame) 登上330 stairs層階梯教堂頂樓,遠看德國 Black Forest, 歐盟的國會
      " 到(AU Brasseur)吃薄皮披薩及自製啤酒 坐船(Batorama)遊 史特拉斯堡 逛小法國區 Yll river 旁咖啡店喝飲料休息 "回Clement 小姪子(Eieteen)家拿行李,
      步行到Sasbosborg車站坐TGV" 與姐姐/姐夫依依不捨道別 "火車到DCG Aeroport, 找DCGVAL(TUBE)
      from Terminal 2 to Terminal 3, 步行到門外的 Novtel 飯店" 休息

      法國半自助之行 - 第十天 科瑪 遊

      09-Jul 早上吃飯店早餐 逛傳統巿場/Aven迷路/買鹹面包/ 小威尼斯 建議2個垃圾桶搬移(剛好員工出來搬走) 超巿吃 pizza 中餐 參觀 Clement 家,吃冰淇淋,面包/蛋榚 小朋友回飯店/3人shoppting & Clement 走去餐廳吃/ 洋䓤泒/德國豬腳/超臭起士/冰淇淋 回飯店(IBIS hotel) 看足球 Argetntina vs Holand (PK 4:2)

      法國半自助之行 - 第九天 蒙巴利葉遊

      08-Jul 早上 shopping Aven&sisters shopping/ Ronnie & I 逛巿區 中午吃Clement煮的義大利面/ 搭 TGV 火車5小時(沙拉/三明治) 9:30PM Clement 爸爸/姐夫前來

      法國半自助之行 - 第八天 蒙巴利葉遊

      07-Jul Gordes (戈代爾) 古代小鎮 Abbaye de Sénanque 修道院(看薰衣草) 開車 3hr 回家 還車,忘了背包返回拿取

      法國半自助之行 - 第七天 蒙巴利葉遊

      06-Jul 早上打藍球 (2 vs 2)/Super market buy gifts "中午吃自製 法國櫛瓜泒,蘆荀泒,藍梅泒
      (Clement style French Pie)" "下午 5:00 開車到地中海邊玩水
      (Le Grand Travers, La Grande-Motte, France),看Kite Surfing" "吃全世界第4名 Pizza,買四個不夠,又去買兩個
      2013 world No. 4 Pizza" Watch White House Down

      法國半自助之行 - 第六天 蒙巴利葉遊

      05-Jul 法國開車到 亞爾 (ARLES) 亞維農(Avignon)看城堡/表演節/超巿買東西/迷路 "嘉德水道橋(人類第一個大型工程/自來水工程)
      游泳/打水漂/" Dinner at home 晚餐/肉粽/大滷麵/西瓜

      法國半自助之行 - 第五天 蒙巴利葉遊

      04-Jul 早上打藍球 米其林二星級餐廳 Garden of Sensation Montpellier city 小火車遊街 Beatrice Lin 大滷麵 觀看法國對德國足球賽 0:1 看 Watch Gravity 戶外看大熊星座

      法國半自助之行 - 第四天 蒙巴利葉遊

      03-Jul TRAM to Yea-Lih research center Montpellier city center 吃阿拉伯沙威馬+薄荷甜荼 逛街,辦 SIM 卡 (SFR for SIM card) 回家 買烤肉用品 自家烤肉 (Home made barcue)

      2014/07/03

      法國半自助之行 - 第三天 巴黎

      02-Jul Metro 7 (Villejuif  station) 小孩參觀博物館(Jardin Des Planets),我們逛街 清真寺 (Muslims Temple) 競技場 (羅馬時代) 萬神殿 Metro back to Green house Some take bus w/ laguaage, 3 take Metro 3 Metro to get Lyon Train Station 搭 TGV 到蒙巴利葉