본문 바로가기

Arduino

Arduino + Bluetooth HC-06 (아두이노와 블루투스연동) - 1


 HC-06 블루투스 모듈입니다.
Datasheet


Arduino 와 Bluetooth 연동을 한번 해보았습니다.
(아직 뭔가 만들만한 아이디어가 떠오르지 않아 그냥 기본적인 이것저것만 해보고 있네요..)

제가 구입한 BT 모듈은 HC-06 이라는 녀석으로 모 카페에서 하는 공동구매로 구매 했습니다.
모듈에다가 쓰기편하게 핀 납땜이 다 되어 있는 녀석이고 가격도 12,000원으로 착하네요~





총 6핀이 나와있지만 네핀만 연결하면 구동하는데 문제는 없습니다.
RXD, TXD를 아두이노의 디지털핀 두개에 연결하고
GND,VCC:3.3V 에 연결하면 됩니다.

아두이노의 시리얼로 컨트롤 할 수가 있는데 
아두이노의 0, 1번핀의 TX,RX는 아두이노 usb와 다이렉트로 연결되어 있어서
usb에 뭔가 연결되어 있다면 동시에 시리얼 포트로 사용할 수 없습니다.
때문에 다른 디지털핀으로 시리얼 통신을 해야하는데 이때 필요한게 Software Serial Library 입니다.

BT모듈과 연결한후 아래의 스케치를 올리면 터미널창으로 BT와 통신이 가능합니다.

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(2, 3); //Connect HC-06 TX,RX

void setup() 
{
  Serial.begin(9600);
  Serial.println("Hello!");

  // set the data rate for the BT port
  BTSerial.begin(9600);
}

void loop()
{
  if (BTSerial.available())
    Serial.write(BTSerial.read());
  if (Serial.available())
    BTSerial.write(Serial.read());
}



시리얼로 연결되면 몇가지 BT 설정을 할 수 있습니다.
AT, AT+BAUD, AT+NAME, AT+PIN
요 네가지가 명령어의 전부 인것 같네요.
http://iteadstudio.com/produce/more-powerful-serial-port-bluetooth-module-masterslave/
요기 들어가보시면 다른 명령어들을 볼 수 있습니다.
이건 HC-05 에 대한 설명인듯

네가지 설정을 하면 순서대로 요런 응답을 보내줍니다.


AT command:
1. Communications Test :
Sent : AT
receive : OK
 
2. Change baud rate :
Sent : AT+BAUD1
receive : OK1200
 
Sent : AT+BAUD2
receive : OK2400
1---------1200
2---------2400
3---------4800
4---------9600
5---------19200
6---------38400
7---------57600
8---------115200
Baud rate setting can be save even power down.
 
3. Change Bluetooth device name:
Sent : AT+NAMEdevicename
receive : OKname
(devicename is the name you want the device to be , and it will be searched with this name)
Name setting can be save even power down.
 
4. Change Pincode:
Sent : AT+PINxxxx
receive : OKsetpin
(xxxx is the pin code you set)
Pin code can be save even power down.



그래도 명색이 무선통신이니 안드로이드 폰하고 연동을 해보았습니다.
안드로이드용 BT spp지원 어플은 여러가지가 있는것 같은데 저는 아래링크에 있는 Bluetooth BEE 를 썼습니다.
http://www.elecfreaks.com/1444.html


LIquidCrystal 연결해서 BT로 메세지를 받으면 표시되도록 스케치 작성

#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
SoftwareSerial BTSerial(6, 7);
void setup() 
{
  Serial.begin(9600);
  Serial.println("Hello!");
 
  lcd.begin(16,2);
  // set the data rate for the SoftwareSerial port
  BTSerial.begin(9600);
}
void loop()
{
  if (BTSerial.available()) {
    delay(100);
    lcd.clear();
   
    while (BTSerial.available() > 0) {
      char inChar = (char)BTSerial.read();
      Serial.write(inChar);
      lcd.write(inChar);
    }
  }
  if (Serial.available())
    BTSerial.write(Serial.read());
}






Bluetooth 니까 BlueLCD ㅡㅡ