Using Orange Board and Bluetooth (HC-06)

[ComputerWorld] Arduino means a close friend in Italian and is a typical piece of open-source hardware. It is a mini board made for anyone to easily access an Embedded field, which has been hard and difficult to approach. The Arduino Orange Board to be used in this tutorial is a Korean version of Arduino boards.

The distribution of Arduino boards triggered the spread of open-source hardware and significantly contributed to the spread of Maker Culture as well. Recently, conglomerates such as Intel and Microsoft have also started to enter this open-source hardware market. This indicates that the potential of open-source hardware market has grown likewise.


What is Bluetooth?

The Bluetooth that is being used this time is one of the communication methods used a lot in modern times. It is a Near Field Communication (NFC) standard that exchanges information by connecting portable devices such as smartphones and notebook computers to one another.

 

The name Bluetooth originated from King Harald Bluetooth (Son of Gorm), who unified Denmark and Norway in Scandinavia during the 10th century. There are two opinions about how King Harald Blatand got his nickname Bluetooth, i.e. blue tooth. One is that he injured his teeth during a battle and had false blue teeth put in, and the other is that he liked blueberries, so his teeth were always dyed in blue. The official logo of Bluetooth is a mark made by merging the rune characters of H and B of Harald Blatand.

 


Arduino and Bluetooth

The Bluetooth communication in Arduino is a very attractive factor, and has the advantages of wireless communication, being easy to use, and above all, being able to be connected to smartphones. Because of the advantages of making wireless connection between smartphones and Arduino, Bluetooth functions are used in many projects.

▲Bluetooth that works with Smartphone

The above projects are the representative projects using Bluetooth, such as remote controlled RC cars and mood lamps. People have the impression that they are difficult to make, because wireless communication is being used, but once you actually make them, you'll realize that it is quite easy to make compared to its functions. Using Bluetooth, you can easily implement IoT with Ardunio.
 

List of Items Required

The following items are required to make the Bluetooth measuring device using Orange Board.

 


Connecting to Hardware

 
 

Using the App

To use the Bluetooth, you need a smartphone app. If you are skilled enough, you can make your own app, add some desired functions, and use it. However, it would be much simpler to download and use an already existing app. The app being used for this project is BT Chat. Search BT Chat in the Play Store, and download it.

▲Search BT Chat in Google Play Store

Before you use the app, register your Bluetooth module on the smartphone. Then, run the app to pair with the Bluetooth that has been registered.

 


Source Code
 

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(2, 3); // Set Bluetooth RX & TX.

void setup() { // Initialize the setting.
// Initialize the serial communication.
Serial.begin(9600);
// Initialize the SoftwareSerial communication for the Bluetooth communication.
BTSerial.begin(9600);
Serial.println("BT Start!");
}

void loop() { // Execute Infinite Loop
if (BTSerial.available()) { //If there is data transmitted from the Bluetooth,
Serial.write(BTSerial.read()); // Display the transmitted data onto the serial monitor.
}
if (Serial.available()) { // If there is data transmitted from serial communication,
BTSerial.write(Serial.read()); // Deliver transmitted data to the Bluetooth module.
}
}

 

You can upload the above code to chat with the smartphone using Bluetooth communication. In the code, SoftwareSerial is used. To put it simply, this means making and using another serial communication port using software.

In the case of Arduino UNO, there is only 1 UART communication port. In the Arduino, RX and TX are written on the digital number 0 pin and the number 1 pin. This refers to the UART communication. Every Arduino differs, but in the case of Arduino Mega, there are 4 UART communication ports. (There are 4 pairs of TX and RX.)

If you use UART communication port of the Orange Board for communication between the computer and Orange Board, Orange Board would use up all the UART communication ports, making it impossible to communicate with Bluetooth. Using SoftwareSerial, it can be implemented with software, so you can use one more UART communication port.

The code below is the part where you set the pins of the Bluetooth module. Normally, digital pin 0 and 1 use UART, so digital pin 2 and pin 3 are often designated. The pin number 2 and pin number 3 mean RX and TX, respectively.

You should note that the TX and RX of the Bluetooth should be connected to the RX and TX of the Orange Board respectively. You should also note that the Transmission Port (TX) of the Bluetooth should be connected to the Receiving Port (RX) of the Orange Board, and Receiving Port (RX) of the Bluetooth should be connected to the Transmission Port (TX) of the Orange Board.

 

SoftwareSerial BTSerial(2, 3); // Set Bluetooth RX & TX.

BTSerial is also serial communication, so you need to initialize the setting separate from the Serial. As with the Serial, you can initialize the communication and set speed through begin().

// Initialize the SoftwareSerial communication for the Bluetooth communication.
BTSerial.begin(9600);


The code below is the part where the Bluetooth and Arduino communicate with each other. The Bluetooth and Arduino communicate with each other in the order that you can see in the picture below.

▲Communication between Bluetooth and Arduino

A wireless Bluetooth communication is carried out between the smartphone and Bluetooth module, the Bluetooth module and Orange Board use BTSerial as the SoftwareSerial, and Orange Board and the computer communicates on Hardware Serial.

To transmit data from a smartphone to the computer, BTSerial.available() returns True if there exists data on the BTSerial communication line between the Bluetooth module and Orange Board. If data exists on the BTSerial, that data is read and entered in Serial communication line. Through this, the data received from the Bluetooth is displayed on the serial monitor.

Transmitting the data entered into the computer to the smartphone is the opposite. Run the Serial.available() and check whether there exists data on the Serial communication line. If there is data, read that data and enter into BTSerial. Then, the Bluetooth module delivers the data to the paired smartphone, and then the data that has been entered into the computer can be read from the smartphone.

if (BTSerial.available()) { // If there is data transmitted from the Bluetooth,
Serial.write(BTSerial.read()); // Display transmitted data onto the serial monitor.
}
if (Serial.available()) { // If there is data transmitted from serial communication,
BTSerial.write(Serial.read()); // Deliver transmitted data to the Bluetooth module.
}

 

 

Conclusion

It is essential to learn how to use Bluetooth in order to advance from Arduino beginner level to a higher level. If you can make smartphone apps yourself, then you can make various projects. Thus, we recommend that you study how to use Bluetooth as well as other languages such as Android Studio and App Inventor. The concept of Bluetooth communication is difficult, but its actual content is not hard to grasp, so it is better for you to understand it fully.

More on the project above can be found at http://kocoafab.cc as well.

 

 

------------------------------------------------------------


[강좌] 쉽게 따라하는 오렌지보드 튜토리얼(12)
오렌지 보드와 블루투스(HC-06) 사용하기

 

아두이노는 이탈리어로 ‘친한 친구’라는 뜻을 가진 대표적인 오픈소스 하드웨어다. 딱딱하고 접근하기 힘들었던 임베디드 분야를 누구나 쉽게 접근할 수 있도록 만든 미니 기판이라 할 수 있다. 이번 강좌에 사용하는 아두이노 오렌지보드는 한국형 아두이노라 할 수 있다.

아두이노의 보급은 오픈소스 하드웨어의 확산을 불러일으켰고 메이커 문화의 확산에도 큰 기여를 했다. 최근에는 인텔, 마이크로소프트 등 대형 기업들도 이런 오픈소스 하드웨어시장에 뛰어들기 시작했다. 그 만큼 오픈소스 하드웨어 시장의 잠재력이 커졌다는 얘기이다.


블루투스란?

이번에 사용되는 블루투스(Bluetooth)는 현재 많이 쓰이는 통신방법 중 하나로 스마트폰이나 노트북 등의 휴대기기를 서로 연결해 정보를 교환하는 근거리 무선기술 표준을 말한다.

 

블루투스라는 이름은 10세기에 스칸디나비아에서 덴마크와 노르웨이를 통일했던 국왕인 하랄드 블라톤(Harald Bluetooth, Son of Gorm)의 이름에서 유래됐다. 블루투스, 즉 파란 이빨은 하랄드 블라톤의 별명으로 전투 중에 치아를 다쳐 푸른색 의치를 넣었다는 설과 블루베리를 좋아해 항상 이빨이 푸른색으로 물들어 있다는 두 가지 설이 있다. 블루투스의 공식 마크 또한 하랄드 블라톤의 H와 B의 룬 문자를 합성하여 만든 마크다.


아두이노와 블루투스


아두이노에서 블루투스 통신은 상당히 매력적인 요소로써, 무선통신이 가능하고 사용하기 쉬우며 무엇보다 스마트폰과 연동이 된다는 장점이 있다. 스마트폰과 아두이노를 무선으로 연동하는 장점 때문에 많은 프로젝트에서 블루투스 기능이 쓰이고 있다.

▲스마트폰과 연동되는 블루투스

위의 프로젝트들은 블루투스를 이용한 대표적인 프로젝트로 무선으로 제어하는 RC카와 무드램프다. 무선으로 통신한다는 이유 때문에 어렵다는 이미지가 강한데 막상 만들고 나면 기능에 비해 만들기 쉽다는 것을 느낄 수 있다. 블루투스를 사용하면 아두이노를 이용해 IoT(사물인터넷)를 간단하게 구현해 볼 수 있다.


필요한 부품 목록
오렌지보드로 블루투스를 제어하기 위해서는 다음과 같은 준비물이 필요하다.

 

 


하드웨어 연결하기

 
 


어플 사용하기

블루투스를 사용하기 위해서는 스마트폰의 애플리케이션(App,앱)이 필요하다. 능력이 된다면 앱을 직접 만들어 원하는 기능을 추가해 사용할 수 있지만 간편하게 사용하기 위해서는 기존에 제작된 앱을 받아서 사용하는 것이 좋다. 이번 프로젝트에 사용되는 앱은 BT Chat이라는 앱이다. 아래와 같이 플레이스토어에서 BT Chat을 검색하여 앱을 받는다.


▲플레이스토어에서 BT Chat 검색

앱을 사용하기 전 사용하는 블루투스 모듈을 스마트폰에 등록한다. 그 다음 앱을 실행시켜 등록시킨 블루투스를 페어링해 사용한다.

 



소스코드

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(2, 3); // 블루투스 RX, TX 설정

void setup() { //세팅 초기화
// 시리얼 통신 초기화
Serial.begin(9600);
// 블루투스 통신을 하기위한 소프트웨어시리얼 통신 초기화
BTSerial.begin(9600);
Serial.println("BT Start!");
}

void loop() { //무한 반복 실행
if (BTSerial.available()) { //블루투스에서 전송된 데이터가 있을 경우
Serial.write(BTSerial.read()); //시리얼 모니터에 전송된 데이터 출력
}
if (Serial.available()) { //시리얼통신으로 전송된 데이터가 있을 경우
BTSerial.write(Serial.read()); //블루투스모듈로 전송된 데이터 전달
}
}


위 코드를 업로드해 블루투스 통신으로 스마트폰과 채팅할 수 있다. 코드에서는 SoftwareSerial을 사용하는데 간단하게 설명해 소프트웨어적으로 시리얼통신 포트를 하나 더 만들어서 사용하는 것을 말한다.

아두이노UNO의 경우 UART통신 포트가 1개밖에 없다. 아두이노에는 디지털 0번핀, 1번핀에 RX, TX가 적혀있다. 이것이 UART통신을 의미한다. 아두이노마다 다르지만 아두이노Mega의 경우 UART통신 포트가 4개 있다(TX, RX가 4쌍 존재한다).

오렌지보드의 UART통신 포트를 컴퓨터와 오렌지보드 간 통신에 써버리게 되면 오렌지보드는 UART통신 포트를 다 써버리게 돼 블루투스와 통신할 수 없다. SoftwareSerial을 사용하면 소프트웨어적으로 구현해 UART통신포트를 하나 더 사용할 수 있다.

아래 코드는 블루투스모듈의 핀을 설정하는 부분이다. 보통 디지털 0, 1번핀은 하드웨어시리얼(UART)을 사용하기 때문에 디지털 2, 3핀 지정을 한다. 2번핀 3번핀은 각각 RX, TX를 의미한다. 유의할 점은 블루투스의 TX와 RX가 각각 오렌지보드의 RX, TX와 연결되야 한다는 점이다. 블루투스의 전송포트(TX)는 오렌지보드의 수신포트(RX)와 연결되고, 블루투스의 수신포트(RX)는 오렌지보드의 전송포트(TX)와 연결된다는 점을 유의해야 한다.

SoftwareSerial BTSerial(2, 3); // 블루투스 RX, TX 설정

BTSerial도 시리얼통신이기 때문에 Serial과 별개로 초기화 설정이 필요하다. Serial과 마찬가지로 begin()을 통해 통신 초기화 및 속도를 설정할 수 있다.

// 블루투스 통신을 하기위한 소프트웨어시리얼 통신 초기화
BTSerial.begin(9600);

아래 코드는 블루투스와 아두이노가 서로 통신을 하는 부분이다. 블루투스와 아두이노는 아래 그림과 같은 순서로 통신을 하게 된다.

▲블루투스와 아두이노가 서로 통신

스마트폰과 블루투스모듈은 무선으로 블루투스 통신을 하고, 블루투스 모듈과 오렌지 보드는 SoftwareSerial로 BTSerial을 사용하며 오렌지보드와 컴퓨터는 Hardware Serial을 통해 통신을 한다.

스마트폰에서 컴퓨터로 데이터를 전송하기 위해 BTSerial.available()은 블루투스모듈과 오렌지보드 사이에 있는 BTSerial 통신 라인에 데이터가 존재할 경우 True(참)를 반환한다. 데이터가 BTSerial에 존재하면 그 데이터를 읽어와 Serial통신 라인에 입력한다. 이를 통해 블루투스에서 받아온 데이터가 시리얼 모니터에 출력된다.

컴퓨터에 입력한 데이터를 스마트폰으로 전송하는 방법은 이와 반대로 하면 된다. Serial.available()을 실행하여 Serial통신 라인에 데이터가 존재하는지 확인한다. 데이터가 있을 경우 그 데이터를 읽어와서 BTSerial쪽으로 입력한다. 그렇게 되면 블루투스 모듈은 페어링된 스마트폰으로 그 데이터를 전달해 스마트폰에서 컴퓨터에 입력한 데이터를 볼 수 있게 된다.

if (BTSerial.available()) { //블루투스에서 전송된 데이터가 있을 경우
Serial.write(BTSerial.read()); //시리얼 모니터에 전송된 데이터 출력
}
if (Serial.available()) { //시리얼통신으로 전송된 데이터가 있을 경우
BTSerial.write(Serial.read()); //블루투스모듈로 전송된 데이터 전달
}

 

 

마치며

블루투스 사용은 아두이노 초급에서 더 높은 단계로 올라가기 위해 필수로 익혀야 할 내용이다. 스마트폰 앱을 직접 만들어 볼 수만 있다면 정말로 다양한 프로젝트를 만들어 볼 수 있기 때문에 블루투스 사용과 더불어 안드로이드 스튜디오나 앱인벤터와 같은 다른 언어를 같이 공부하는 것을 추천한다.

블루투스 통신에 대한 개념은 어렵지만 내용은 어렵지 않기 때문에 꼭 숙지하는 것이 좋다.
위 튜토리얼에 대한 글은 http://kocoafab.cc에서도 확인할 수 있다.

 

 코코아팹은 한국 메이커 문화를 만들어가는 온라인 메이커포털이다. 오픈소스 하드웨어 오렌지보드의 생산과 활용법 공개 등 많은 활동을 진행하고 있다.


 
 

저작권자 © 아이티데일리 무단전재 및 재배포 금지