Sei sulla pagina 1di 44

UNIT 9 Bluetooth Wireless Communications

UNIT OBJECTIVE

When you have completed this unit, you will be able to explain the operation of
Bluetooth module and demonstrate cross platform control with Bluetooth.

DISCUSSION

Bluetooth is the most widely used short-range wireless communication technology.


Bluetooth operates at frequencies between 2402 and 2480 MHz, or 2400 and 2483.5
MHz including guard bands 2 MHz wide at the bottom end and 3.5 MHz wide at the
top. This is in the globally unlicensed (but not unregulated) Industrial, Scientific and
Medical (ISM) 2.4 GHz short-range radio frequency band.

Using Bluetooth link, you can exchange files, connect to the Internet, and construct a
private network as well as communicate with a keyboard, mouse, etc. without a
cable connection.

Android is a mobile operating system developed by Google, based on the Linux


kernel and designed for touchscreen mobile devices such as smartphones and
tablets.

In this unit, you’ll control your MTS-100 in Windows system over Bluetooth link, and
control your MTS-100 on Android phone.

EQUIPMENT REQUIRED

1. Personal Computer
2. MTS-100 Tutor for Arduino
3. MTS-100 CD
4. USB Cable
5. Jumper Wires
6. AC Power Cord
7. Android Smartphone

9-1
Exercise 9-1 Windows Cross Platform Control

EXERCISE OBJECTIVE

After completing this exercise, you should be able to:


1. State the operation of Bluetooth module.
2. Write sketches to control MTS-100 in Windows system over Bluetooth link.

DISCUSSION

Bluetooth Module

Bluetooth is a wireless technology standard, invented by telecom vendor Ericsson in


1994, for exchanging data over short distances from fixed and mobile devices, and
building Personal Area Networks (PANs). Bluetooth allows you to exchange files,
connect to the Internet, and construct a private network as well as communicate with
a keyboard, mouse, headset, speaker, etc. without a cable connection.

Bluetooth technology has the following features:

1. Bluetooth operates at frequencies between 2.4 and 2.485 GHz, in the unlicensed
Industrial, Scientific and Medical (ISM) 2.4 GHz short-range radio band.

2. Bluetooth uses a radio technology called Adaptive Frequency Hopping (AFH) to


avoid channel conflicts and reduce interference in environments where multiple
wireless technologies coexist.

3. The distance of Bluetooth wireless communication is less than 10 meters.

4. A Bluetooth device can exchange data with other devices such as a


microcontroller board and smartphone using a Universal Asynchronous
Receiver/Transmitter (UART). In other words, when using a Bluetooth device, a
serial port will be created on the device, as shown in Fig. 9-1.

In Fig. 9-1, the two blue teeth represent two Bluetooth devices. A Bluetooth device,
like a headset or smartphone, contains a tiny computer chip with a Bluetooth radio
and software that makes it easy to connect.

9-2
Fig. 9-1 Bluetooth wireless communication

The MTS-100 is equipped with a Bluetooth module, as shown in Fig. 9-2, which
contains not only a Bluetooth chip but also an antenna. As a user, you can ignore
the radio side of Bluetooth module, but care about the wired input and output
terminals; that is, the UART side of Bluetooth module. UART terminal pins may vary
from manufacturer to manufacturer, but 4 pins are indispensable. The pin function is
described in Table 9-1.

Fig. 9-2 Bluetooth module (HC-05)

Table 9-1 Bluetooth module pin function


Pin Name Description
VCC or +5V Power supply
GND Ground
TxD Serial transmit
RxD Serial receive

Basically, the UART of the Bluetooth module is a serial port operating in full duplex
mode. In the MTS-100, the Bluetooth module is installed on the P28 header as
shown in Fig. 9-3. To use this module, you may connect signals to the P28A header,
P28A-1 for TxD and P28A-2 for RxD. Terminals VCC and GND are internally wired
to +5V and ground, respectively.

9-3
Fig. 9-3 MTS-100’s Bluetooth circuit

Software Serial Port

The Arduino hardware has built-in support for serial communication on pins 0 and 1,
which also goes to the computer via the USB connection. The native serial support
happens via a piece of hardware (built into the chip) called a UART. This hardware
allows the Atmega microcontroller to achieve serial communication even while
working on other tasks, as long as there room in the 64 byte serial buffer.

Arduino pins 0 and 1 are not used as digital I/O in the example sketches in this
manual because these two pins are the pins of serial port on the Arduino Uno board:
pin 0 is RxD and pin 1 is TxD. On the Arduino Uno board, you'll see the RX and TX
LEDs blink as the sketch is uploaded. During uploading, data is transferred from the
computer to the FTDI chip (versions prior to R2) on Arduino Uno board over USB
port, and then sent through the UART port of FTDI chip to ATmega328P’s pins 2 and
3 (pins 0 and 1 on Arduino Uno), as shown in Fig. 9-4. In addition, the FTDI chip is a
USB to UART bridge chip.

Fig. 9-4 Uploading path of Arduino Uno

9-4
Arduino Uno R2 uses an ATmega8U2 microcontroller chip instead of FTDI chip,
whereas Arduino Uno R3 uses an ATmega16U2 instead of the ATmega8U2.

On the Arduino Uno board, the RX and TX LEDs will flash as the sketch is uploaded
or data is transferred over a UART. In addition, Arduino software includes a Serial
Monitor which allows simple textual data to be sent to and from the Arduino board
along the same path.

If you connect external loads to pin 0 or pin 1, errors may occur during data
transmission over the UART serial port due to load effect. In this case, Arduino IDE
will report error message.

The Arduino Leonardo shown in Fig. 9-5 is an improved version of Arduino Uno. It is
equipped with an ATmega32U4 microcontroller, which is a 44-pin SMD package as
shown in Fig. 9-6. Except the number of pins, the biggest difference between
ATmega32U4 and ATmega328P is that ATmega32U4 has an on-chip USB, so a
USB to UART bridge chip is not required anymore. In other words, the ATmega32U4
is equivalent to the combination of ATmega328P and ATmega16U2.

Fig. 9-5 Arduino Leonardo board

9-5
Fig. 9-6 ATmega32U4 microcontroller

The core of MTS-100 Tutor for Arduino is the Arduino Uno that is completely
compatible with the Arduino Leonardo. The example sketches in this manual are
available in both boards.

SoftwareSerial Library

Arduino SoftwareSerial library has been developed to allow serial communication on


other digital pins of the Arduino boards, using software to replicate the functionality
(hence the name "SoftwareSerial"). It is possible to have multiple software serial
ports with speeds up to 115200 bps. A parameter enables inverted signaling for
devices which require that protocol.

The SoftwareSerial library is included with the Arduino IDE software. To use the
SoftwareSerial library functions in your sketch, first add the SoftwareSerial library to
your sketch by inserting #include statement at the start of your code in Global
Declaration:

9-6
#include <SoftwareSerial.h>

Then create a new SoftwareSerial object using the SoftwareSerial() function below.

SoftwareSerial() function

Description:
The SoftwareSerial() function creates a new SoftwareSerial object, whose
name you need to provide as in the example below.

Syntax:

SoftwareSerial myCOM(RXpin, TXpin);

Parameters:
1. myCOM: a variable of type SoftwareSerial.
2. RXpin: the pin on which to receive serial data.
3. TXpin: the pin on which to transmit serial data.

Remember that the RX and TX lines must be cross connected. That is, RX
connected to TX of other device, and TX connected to RX of other device.

For example, to create a new SoftwareSerial object named newCOM, whose receive
pin (RX) is digital pin 11 and transmit pin (TX) is digital pin 10, use the statement:

SoftwareSerial newCOM(11, 10);

begin() function

Description:
The begin() function sets the speed (baud rate) for the serial communication.

Syntax:

myCOM.begin(bps);

Parameters:
1. bps: the baud rate (long), supported baud rates are 300, 600, 1200, 2400,
4800, 9600, 14400, 19200, 28800, 31250, 38400, 57600, and 115200. In
most Bluetooth modules, the default baud rate is 9600.

For example, the statement

myCOM.begin(9600);

initializes a software serial port to operate at 9600 baud.

9-7
available() function

Description:
The available() function is used to get the number of bytes (characters)
available for reading from a software serial port. This is data that's already
arrived and stored in the serial receive buffer.

Syntax:

recCounts = myCOM.available();

Parameter:
1. reCounts: the variable stores the number of bytes available to read.

read() function

Description:
The read() function returns a character that was received on the RX pin of the
software serial port.

Syntax:

recChar = myCOM.read();

Parameters:
1. recChar: the variable stores the character read, or -1 if none is available.

write() function

Description:
The write() function prints data to the transmit pin TX of the software serial port
as raw bytes.

Syntax:

snedBytes = myCOM.write(data);

Parameters:
1. data: the data to transmit.
2. sendBytes: the number of bytes written returned by the write() function.

print() function

Description:
The print() function prints data to the serial port as human-readable ASCII text.
This command can take many forms:

9-8
1. Numbers are printed using an ASCII character for each digit.
2. Floats are similarly printed as ASCII digits, defaulting to two decimal places.
3. Bytes are sent as a single character. Characters and strings are sent as is.

Syntax:

sendDatas = myCOM.print(val, format);

Parameters:
1. Val: the value to print - any data type
2. Format: The optional argument specifies the base (format) to use; permitted
values are BIN (binary, or base 2), OCT (octal, or base 8), DEC (decimal, or
base 10), HEX (hexadecimal, or base 16).
(1) BIN: binary format, e.g. Serial.print(81, BIN) gives “01010001”.
(2) OCT: octal format, e.g. Serial.print(81, OCT) gives “121”.
(3) DEC: decimal format, e.g. Serial.print(81, DEC) gives “81”.
(4) HEX: hexadecimal format, e.g. Serial.print(81, HEX) gives “51”.
For floating-point numbers, this parameter specifies the number of
decimal places to use.
3. sendDatas: the variable is the number of bytes written returned by the print()
function.

println() function

Description:
The println() function prints data to the transmit pin of the software serial port,
followed by a carriage return and line feed. Works the same as the print()
function.

overflow() function

Description:
The overflow() function tests to see if a software serial buffer overflow has
occurred. Calling this function clears the overflow flag, meaning that
subsequent calls will return false unless another byte of data has been received
and discarded in the meantime. The software serial buffer can hold 64 bytes.

Syntax:

yesNo = myCOM.overflow();

9-9
Parameter:
1. yesNo: the variable stores the return, in boolean, true if the overflow occurs;
false otherwise.

peek() function

Description:
The peek() function returns a character that was received on the RX pin of the
software serial port. Unlike read(), however, subsequent calls to this function
will return the same character. Note that only one SoftwareSerial instance can
receive incoming data at a time (select which one with the listen() function). The
data in the serial buffer read by read() will be deleted; while the peek() only
copies the data in the serial buffer.

Syntax:

recData = myCOM.peek();

Parameters:
1. recData: the variable stores the character read, or -1 if none is available.

listen() function

Description:
The listen() function enables the selected software serial port to listen. Only one
software serial port can listen at a time; data that arrives for other ports will be
discarded. Any data already received is discarded during the call to listen()
unless the given instance is already listening.

Syntax:

myCOM.listen();

Parameter:
1. myCOM: the name of the instance to listen.

isListening() function

Description:
The isListening() function tests to see if requested software serial port is
actively listening.

9-10
Syntax:

yesNo = myCOM. isListening();

Parameter:
1. yesNo: the variable stores the return, in boolean, true if the port is in
listening mode; false otherwise.

Windows Bluetooth Settings

Most Windows-based computers now support Bluetooth communication. However,


desktop personal computers usually come without a built-in Bluetooth receiver, while
notebooks have a built-in Bluetooth. To achieve Bluetooth communication on your
PC, an external Bluetooth adapter, also called the USB Bluetooth dongle is required,
as shown in Fig. 9-7. Dongles are USB network adapters that allow a computer to
communicate with Bluetooth devices such as mobile phones, mice, keyboards,
remotes, and headsets. In Windows 7, version 2.0 will be installed automatically (no
extra driver needed), while version 4.0 needs an extra driver. There is no difference
between USB Bluetooth dongle V2.0 and V4.0 for Arduino board users.

(a) V2.0 (b) V4.0

Fig. 9-7 USB Bluetooth dongles

Windows Cross Platform Control

In this exercise, you will design a cross platform system which controls the MTS-100
in the Windows system. MTS-100 CD provides an executable file of UnoSmile
Playground.exe in the Apps folder, which is used to control the DC motors on
MTS-100 over Bluetooth link. Once the UnoSmile Playground.exe is executed on PC,
the Uno & Simle Playground window will open as shown in Fig. 9-8. This program
uses six buttons to control the operation of DC motors DCM1 and DCM2:

9-11
Fig. 9-8 Uno & Smile Playground window

1. Motor 1 (DCM1):
(1) When the button is clicked, the program will send an instruction of
11 to MTS-100, and the DCM1 will rotate CW.
(2) When the button is clicked, the program will send an instruction of
12 to MTS-100, and the DCM1 will rotate CCW.
(3) When the button is clicked, the program will send an instruction of
10 to MTS-100, and the DCM1 will stop immediately.

2. Motor 2 (DCM2):
(1) When the button is clicked, the program will send an instruction of
21 to MTS-100, and the DCM2 will rotate CW.
(2) When the button is clicked, the program will send an instruction of
22) to MTS-100, and the DCM2 will rotate CCW.
(3) When the button is clicked, the program will send an instruction of
20 to MTS-100, and the DCM2 will stop immediately.

In addition, the temperature measured by the DHT11 Humidity/Temperature Sensor


will send from MTS-100 to PC and display in the Temperature field of the
thermometer.

9-12
PROCEDURE

Pairing Bluetooth in Windows

To pair the Bluetooth module on the MTS-100 with your Bluetooth dongle, do the
following steps:

1. Once a USB Bluetooth dongle is plugged on your computer and the driver is
installed, you can click button and select Devices and Printers. The
Devices and Printers window opens as shown in Fig. 9-9. In the Devices, you’ll
see a Bluetooth USB Dongle icon.

Fig. 9-9 Devices and Printers window

2. Connect a correct AC voltage from a wall outlet to your MTS-100 using the AC
power cord, and then turn on the power. The Bluetooth module (HC-05 or HC-06)
on the MTS-100 will flash rapidly.

3. In Windows, open the Devices and Printers window and select Add a device.
The Add a device dialog appears as shown in Fig. 9-10. The system will look for
new devices and display them here. The Bluetooth module on the MTS-100 is
HC-05 or HC-06. Your Bluetooth name is usually labeled on the sticker on
Bluetooth module, for example "KHBT0001". The following uses the Bluetooth
name HC-05 as an example.

9-13
Fig. 9-10 Add a device dialog - Select a device to add to this computer

4. Every Bluetooth device has a unique hardware ID code. It is a good idea to write
down and stick ID code on the Bluetooth module. To query the ID code of your
Bluetooth module, right click the Bluetooth device and select the Properties from
the pop-up menu, the HC-05 Properties dialog will appear. Select the Bluetooth
tab, right-click the HC-05 icon and choose Properties from the pop-up menu.
The HC-05 Properties dialog opens as shown in Fig. 9-11. The unique identifier
of HC-05 is shown on Bluetooth tab.

Fig. 9-11 HC-05 Properties dialog - Bluetooth tab

9-14
5. Once completed, click button to return back to the previous dialog in
Fig. 9-9. Select the HC-05 Bluetooth and click button. The Select a
pairing option dialog opens as shown in Fig. 9-12.

Fig. 9-12 Add a device dialog - Select a pairing option

6. To pair manually, select Enter the device’s pairing code, and then click
button to open the next dialog as shown in Fig. 9-13.

Fig. 9-13 Add a device dialog - Enter the pairing code for the device

9-15
7. Enter the pairing code 1234 for the Bluetooth module, and click button
to go to the next dialog, as shown in Fig. 9-14. Click button to close
this dialog and return back to the Devices and Printers window, you will see a
new Bluetooth device (HC-05) in this window.

Fig. 9-14 Add a device dialog – HC-05 has been added to the computer

8. Once a Bluetooth is paired, the system will assign a UART serial port (com) to
the Bluetooth module. In the Devices and Printers window, you may query the
new UART serial port by right-clicking the HC-05 icon in the Devices and select
the Properties from the pop-up menu. The HC-05 Properties dialog opens.
Select the Hardware tab, as shown in Fig. 9-15, you will see the message
“Standard Serial over Bluetooth link (COM3)” in the Name field. This means
that your Bluetooth module will transmit/receive data over the serial port COM3.
Note: Remember this COM port for later use.

9-16
Fig. 9-15 HC-05 Properties – Hardware tab

Circuit Connections

Perform the following steps and refer to the wiring diagram in Fig. 9-16 to complete
the connections for DC motor control and temperature measurement:

1. Connect the Arduino Uno board to your computer using the USB cable.

2. Connect the Arduino pins 11 and 10 to the header pins P28A-1 and P28A-2
(BLUETOOTH), respectively. This makes the Bluetooth module available.

3. Connect the Arduino pin 12 to the header pin P27 (HUMIDITY/TEMPERATURE


SENSOR). This makes the DHT11 Humidity/Temperature Sensor available.

4. Connect the Arduino pins 2~7 to the header pins P18A1-1~P18A1-6 (DC
MOTOR), respectively. This makes DC motors DCM1 and DCM2 available.

9-17
Fig. 9-16 Wiring diagram of DC motor control and temperature measurement

Program Design

5. Open the Arduino IDE window. In the text editor, write your sketch as shown
below and save it as “ex9-1-1.ino”. Note: You can omit the program comments to
save time for the actual study of the sketch.

//========== ex9-1-1.ino ============


// DC motors: pins 2~7 connected to P18A1-1~P18A1-7
// Bluetooth module: pins 11~10 connected to P28A-1~2
// Humidity/Temperature Sensor: pin 12 connected to P27
//========== Global Declaration ============
const int EN_M1 = 2; // DCM1 enable pin connected to digital pin 2

9-18
const int M1_A = 3; // DCM1 A pin connected to digital pin 3
const int M1_B = 4; // DCM1 B pin connected to digital pin 4
const int EN_M2 =5; // DCM2 enable pin connected to digital pin 5
const int M2_A = 6; // DCM2 A pin connected to digital pin 6
const int M2_B = 7; // DCM2 B pin connected to digital pin 7
const int RX = 10; // Bluetooth TxD connected to digital pin 10 (cross
connection)
const int TX = 11; // Bluetooth RxD connected to digital pin 11
(cross connection)
const int dht = 12; // DHT11 connected to digital pin 12
#include "DHT.h" // include the DHT library
DHT myDHT(dht,DHT11); // create a DHT object named myDHT
#include <SoftwareSerial.h> // include the SoftwareSerial library
SoftwareSerial myBT(RX, TX); // create a SoftwareSerial object named myBT
float temp; // declare float variable named temp
//========== Initialization ============
void setup()
{ myBT.begin(9600); // open the software serial port at 9600 bps
pinMode(EN_M1,OUTPUT); // configure digital pin 2 as an output
pinMode(M1_A,OUTPUT); // configure digital pin 3 as an output
pinMode(M1_B,OUTPUT); // configure digital pin 4 as an output
pinMode(EN_M2,OUTPUT); // configure digital pin 5 as an output
pinMode(M2_A,OUTPUT); // configure digital pin 6 as an output
pinMode(M2_B,OUTPUT); // configure digital pin 7 as an output
digitalWrite(EN_M1, 0); // turn off DCM1
digitalWrite(EN_M2, 0); // turn off DCM2
}
//========== Main Code ============
void loop()
{ temp=myDHT.readTemperature(0); // read degrees Celsius
myBT.write(temp); // send temperature to Windows (in bytes)
delay(100); // wait for 0.1 seconds
if ( myBT.available()) // if an instruction is received,
{ int instruction = myBT.read(); // read instruction
switch (instruction) // run the case statements according to
the instruction
{ case 10: // when instruction equals 10
digitalWrite(EN_M1, 0); // stop DCM1
break; // exit
case 11: // when instruction equals 11
digitalWrite(M1_A, 1); // DCM1 rotates CW
digitalWrite(M1_B, 0);
digitalWrite(EN_M1, 1);
break; // exit

9-19
case 12: // when instruction equals 12
digitalWrite(M1_A, 0); // DCM1 rotates CW
digitalWrite(M1_B, 1);
digitalWrite(EN_M1, 1);
break; // exit
case 20: // when instruction equals 20
digitalWrite(EN_M2, 0); // stop DCM2
break; // exit
case 21: // when instruction equals 21
digitalWrite(M2_A, 1); // DCM2 rotates CW
digitalWrite(M2_B, 0);
digitalWrite(EN_M2, 1);
break; // exit
case 22: // when instruction equals 22
digitalWrite(M2_A, 0); // DCM2 rotates CCW
digitalWrite(M2_B, 1);
digitalWrite(EN_M2, 1);
break; // exit
}
}
}

Compiling and Uploading

6. Once the editing is completed, press + keys to compile your sketch.


If “Error compiling” appears in the console, the message area will show the error
message with line number. If “Done compiling” appears in the console, the
message area will display memory usage for code and variables as follows:

Sketch uses 4,686 bytes (14%) of program storage space. Maximum is 32,256
bytes.

Global variables use 146 bytes (7%) of dynamic memory, leaving 1,902 bytes
for local variables. Maximum is 2,048 bytes.

7. Press + keys to upload the code to the Arduino board.

8. In Windows, execute the UnoSmile Playground.exe program. The Uno & Smile
Playground window should open as shown Fig. 9-17.

9-20
Fig. 9-17 Uno & Smile Playground window

9. In the COM port field, select a correct COM port (COM3 here). If an invalid com
port is selected, this field will show COM1 by default.

10. Test Motor 1 (DCM1):


(1) Click button. The DCM1 should rotate CW.
(2) Click button. The DCM1 should stop.
(3) Click button. The DCM1 should rotate CCW.
(4) Click button. The DCM1 should stop.

11. Test Motor 2 (DCM2):


(1) Click button. The DCM2 should rotate CW.
(2) Click button. The DCM2 should stop.
(3) Click button. The DCM2 should rotate CCW.
(4) Click button. The DCM2 should stop.

12. Test Thermometer:


(1) The current temperature should be displayed in the Temperature field of the
thermometer.
(2) Blow on the DHT11 Humidity/Temperature Sensor on the MTS-100 to warm
it. After a while, the displayed temperature value should increase.

9-21
Exercise 9-2 Android Cross Platform Control

EXERCISE OBJECTIVE

After completing this exercise, you should be able to:


1. Pair Bluetooth module with Android phone.
2. Install App on your Android phone using File Manger or Google Drive.
3. Demonstrate the cross platform control using Android phone.

DISCUSSION

Android System

Android is a mobile operating system (OS) developed by Google, based on the Linux
kernel and originally designed for touchscreen mobile devices such as smartphones
and tablets. Android's user interface is based on direct manipulation, using touch
gestures that loosely correspond to real-world actions, such as swiping, tapping and
pinching, to manipulate on-screen objects along with a virtual keyboard for text input.

To control the serial RGB LED and the DHT11 Humidity/Temperature Sensor on the
MTS-100 with an Android phone/tablet over Bluetooth link, you should perform the
following tasks:
1. Installing App (UnoSmile_E.apk) on your Android phone
2. Pairing Bluetooth module with Android phone
3. Executing UnoSmile_E.apk on Android phone

Installing App (UnoSmile_E.apk) on Your Android Phone

There are several ways to install apps onto your Android phone or tablet. The
following introduces two common ways:

1. Using File Manager


This way requires a USB cable to connect your Android phone to PC. Most
smartphones or tablets shipped with an included USB cable (A plug to B plug) for
charging battery and transferring data. A Sony Z3 Android phone will be used in
the following demonstration.

9-22
Note: If your Android phone has been already installed apps related to File
Manager, skip steps 1 and 2.

(1) Tap the Play Store to open the Google Play page as shown in Fig. 9-18.

Fig. 9-18 Google Play Store

(2) Tap the button to open the Apps screen as shown in Fig.
9-19(a). Tap and then enter file manager in the
upper box, the system will search and list the items related to file manager,
as shown in Fig. 9-19(b). Tap Well File Manager and follow the instructions
to download and install the Well File Manager on Android phone.

9-23
(a) (b)

Fig. 9-19 Downloading/Installing the Well File Manager

(3) Connect the powered Android phone to your computer using the USB cable,
an external hard drive and a CD drive will appear on the computer screen.
The CD drive contains a driver program. Execute the setup.exe in CD drive
to install the driver on your computer. Once installed, the Android phone can
communicate with your computer.

(4) Insert MTS-100 CD into your CD drive. Copy the UnoSmile_E.apk file from
the Apps folder on CD to the Internal Storage folder of Android phone.

(5) On the Android phone, open File Manager or Well File Manager and go to
Internal Storage. You’ll see the UnoSmile_E.apk file, as shown in Fig. 9-20.

9-24
Fig. 9-20 UnoSmile_E.apk in Internal Storage

2. Using Google Drive


Google Drive is a cloud storage service. If you have a Gmail account, you can
create a Google Drive. With Google Drive, you can upload and store your apps
online. Then you can download the apps from Google Drive to your Android
phone/tablet.

(1) In Windows, open the Google Chrome Brower, and then click the
Google apps button. A dropdown menu opens as shown in Fig. 9-21.

9-25
Fig. 9-21 Google Chrome

(2) To create a new folder called MTS-100 App in the My Drive folder, click My
Drive and select New folder from the dropdown menu, enter MTS-100 App
in the New folder dialog and click Create button. The MTS-100 App folder
appears, then double click the MTS-100 App (My Drive > MTS-100 App).

(3) Right-click on empty space and then select Upload files… from the pop-up
menu, an Open dialog box appears. Insert MTS-100 CD into your CD-ROM
drive, select the UnoSmile_E.apk file in the Apps folder on CD, and then
click Open button. The UnoSmile_E.apk file will be uploaded and located in
the My Drive > MTS-100 App folder, as shown in Fig. 9-22.

9-26
Fig. 9-22 Google Drive

(4) On the Android phone you have installed Google Drive app, open Google
Drive as shown in Fig. 9-23.

Fig. 9-23 Google Drive on Android phone

9-27
(5) Open the MTS-100 App folder, you’ll see the UnoSmile_E.apk file in the
folder, as shown in Fig. 9-24.

Fig. 9-24 UnoSmile_E.apk file in the MTS-100 App folder

(6) The cloud drive is an extension of smartphone or tablet. It can be considered


as part of cell phone or tablet, so you can directly install an App from cloud
drive to your Android phone. When you see the UnoSmile.apk file in your
Android phone, tap on the file name, a prompt dialog appears as shown in
Fig. 9-25(a). Tap SETTINGS button to open the Security page as shown in
Fig. 9-25(b), you’ll see “Unknown sources: Allow installation of apps from
unknown sources.”

9-28
(a) Prompt dialog (b) Security page

Fig. 9-25 Warning message and security setting

(7) Check the Unknown sources box. A prompt dialog appears as shown in Fig.
9-26. Tap OK button to confirm that you want to enable.

Fig. 9-26 Prompt dialog

9-29
(8) Return back to the UnoSmile_E.apk file. Tap on the UnoSmile_E.apk file
name again, a new screen showing the UnoSmile name at the top is
displayed as shown in Fig. 9-27(a). This screen gives you more details about
the application including the different functionalities it will need to access. To
install the application, just tap the INSTALL button.

(9) Once the installation is completed, a new screen is displayed as shown in Fig.
9-27(b). You can choose to immediately run the application by tapping the
OPEN button, or to quit and close this screen by tapping the DONE button.

(a) (b)
Fig. 9-27 UnoSmile installation

Android Cross Platform Control

In this exercise, you’ll control the serial RGB LED on the MTS-100 using an Android
phone/tablet. If you have installed the UnoSmile_E.apk app from MTS-100 CD to
your Android phone using either File Manager or Google Drive as mentioned above,
this app will control the serial RGB LED and the DHT11 Humidity/Temperature
Sensor on the MTS-100 over the Bluetooth link. Once the UnoSmile_E.apk app is
executed, the Uno & Smile screen on Android phone is shown in Fig. 9-28.

9-30
Fig. 9-28 Uno & Smile screen on Android phone

Control Buttons

: This button is used to choose a Bluetooth device from the available


devices.

: This button connects Android phone to other Bluetooth devices.

: This button disconnects Android phone from other Bluetooth device.

Status: This field displays status of Bluetooth connection as well as prompt


message.

1. RGB LED:
(1) When the button is tapped, an instruction “A” will be sent to
MTS-100, and the serial RGB LED will show the lighting effect of running
LED.
(2) When the button is tapped, an instruction “B” will be sent to
MTS-100, and the serial RGB LED will show the lighting effect of windscreen
wiper.

9-31
(3) When the button is tapped, an instruction “C” will be sent to
MTS-100, and the serial RGB LED will show the lighting effect of smiling
eyebrows.
(4) When the button is tapped, an instruction “D” will be sent to
MTS-100, and the serial RGB LED will show the lighting effect of jumping
lights.
(5) When the button is tapped, an instruction “E” will be sent to
MTS-100, and the serial RGB LED will show a rainbow pattern.
(6) When the button is tapped, an instruction “F” will be sent to
MTS-100, and the serial RGB LED will show the lighting effect of rainbow
cycle.

2. Digital Thermometer: The current temperature detected by the DHT11


Humidity/Temperature Sensor will be displayed in the Temperature: field in
degrees Celsius.

PROCEDURE

Pairing Bluetooth Module with Android Phone

Generally Android phones or tablets have built-in Bluetooth. To pair the Bluetooth
module on the MTS-100 with your Android phone (Sony Z3), do the following steps:

1. Connect a correct AC voltage from a wall outlet to your MTS-100 using the AC
power cord, and then turn on the power. The LED on the Bluetooth module will
flash rapidly.

2. On the Home screen of Android phone, tap button or button (icons

may vary among Android versions). The Settings page opens as shown in Fig.
9-29(a).

9-32
1. Tap Bluetooth to
2. Slide the switch to
open Bluetooth
the right to turn on
page.
the Bluetooth.

(a) Settings page (b) Bluetooth page

Fig. 9-29 Settings and Bluetooth page

3. In the Wireless & networks, tap Bluetooth to open Bluetooth page as shown in
Fig. 9-29(b). Slide the Bluetooth switch to the right to turn on the Bluetooth. The
system will automatically search other nearby Bluetooth devices, and list them in
the Available devices, as shown in Fig. 9-30.

Fig. 9-30 Bluetooth devices searched

9-33
4. Select the Bluetooth device HC-05 to pair. The Bluetooth pairing request page
opens as shown in Fig. 9-31. Tap a text field to open the onscreen keyboard, and
then tap the PIN code 1234.

2. Enter 1234.

1. Tap here to open


onscreen keyboard

Fig. 9-31 Bluetooth pairing request page

5. Tap OK. If the PIN code is correct, the paired Bluetooth device name HC-05 will
be displayed in the Paired devices, as shown in Fig. 9-32.

Fig. 9-32 Paired Bluetooth device

9-34
Circuit Connections

Perform the following steps and refer to the wiring diagram in Fig. 9-33 to complete
the connections for Android cross platform control:

Fig. 9-33 Wiring diagram of serial RGB LED control and temperature measurement

1. Connect the Arduino Uno board to your computer using the USB cable.

2. Connect the Arduino pins 11 and 10 to the header pins P28A-1 and P28A-2
(BLUETOOTH), respectively. Remember that the RX and TX lines must be cross
connected.

3. Connect the Arduino pin 12 to the header P27 (HUMIDITY/TEMPERATURE


SENSOR). This makes the DHT11 Humidity/Temperature Sensor available.

9-35
4. Connect the Arduino pin 7 to the header P10B (SERIAL RGB LED). This makes
the serial RGB LED available.

5. Place the SW10 in the ON position to power the Serial RGB LED.

Program Design

6. Open the Arduino IDE window. In the text editor, write your sketch as shown
below and save it as “ex9-2-1.ino”. Note: You can omit the program comments to
save time for the actual study of the sketch.

//========== ex9-2-1.ino ============


// serial RGB LED connection: pin 7 connected to P10B
// Bluetooth module connections: pins 11~10 connected to P28A-2~1
// Humidity/Temperature Sensor connection: pin 12 connected to P27
//========== Global Declaration ============
const int RGB = 7; // serial RGB LED connected to digital pin 7
const int RX = 10; // Bluetooth TxD connected to digital pin 10 (cross connection)
const int TX = 11; // Bluetooth RxD connected to digital pin 11 (cross connection)
const int dht = 12; // DHT11 connected to digital pin 12
#include "DHT.h" // include the DHT sensor library
DHT myDHT(dht,DHT11); // create a DHT object named myDHT
#include <SoftwareSerial.h> // include the SoftwareSerial library
SoftwareSerial myBT(RX, TX); // create a SoftwareSerial object named myBT
float temp,temp0=0; // declare variable named temp
#include <Adafruit_NeoPixel.h> // include the Adafruit_NeoPixel library
Adafruit_NeoPixel LED=Adafruit_NeoPixel(20,RGB,NEO_GRB+NEO_KHZ800);
//========== Initialization ============
void setup()
{ myBT.begin(9600); // open the software serial port at 9600 bps
pinMode(RGB,OUTPUT); // configure digital pin 7 as an output
LED.begin(); // initialize the Adafruit_NeoPixel library
LED.show(); // update the whole strip
}
//========== Main Code ============
void loop()
{ temp=myDHT.readTemperature(0); // read temperature Celsius
if (temp!=temp0) // a change in temperature
{ myBT.write(temp); // send new temperature to Android (in bytes)
temp0=temp; // save new temperature value
}
delay(100); // wait for 0.1 seconds

9-36
if ( myBT.available()) // if an instruction is received,
{ int instruction = myBT.read(); // read instruction
switch (instruction) // run the case statements according to the instruction
{ case 'A' : // when instruction equals A,
knight(); // call the knight() function
break; // exit
case 'B': // when instruction equals B,
wiper(); // call the wiper() function
break; // exit
case 'C': // when instruction equals C,
eyebrows(); // call the eyebrows() function
break; // exit
case 'D': // when instruction equals D,
frog(); // call the frog() function
break; // exit
case 'E': // when instruction equals E,
for(int i=0;i<8;i++) // execute the for loop 8 times
rainbow1(50); // call the rainbow1() function
break; // exit
case 'F': // when instruction equals F,
rainbow2(3,50); // call the rainbow2() function (3 times)
break; // exit
}
}
}
//========== Functions ================
//========== knight() function ============
void knight()
{ pili1(LED.Color(50,0,0),50); // red
pili1(LED.Color(0,50,0),50); // green
pili1(LED.Color(0,0,50),50); // blue
}
//========== wiper() function ============
void wiper()
{ for(int i=0; i<3;i++) // call the pili2() 3 times
{ pili2(LED.Color(30,0,0),100); // red
pili2(LED.Color(0,30,0),100); // green
pili2(LED.Color(0,0,30),100); // blue
}
}
//========== eyebrows() function ============
void eyebrows()
{ for(int i=0; i<3;i++) // call the pili3() 3 times
{ pili3(LED.Color(30,0,0),50); // red

9-37
pili3(LED.Color(0,30,0),50); // green
pili3(LED.Color(0,0,30),50); // blue
}
}
//========== frog() function ============
void frog()
{ for(int i=0; i<3;i++) // call the jumping() 3 times
{ jumping(LED.Color(30,0,0),65); // red
jumping(LED.Color(0,30,0),65); // green
jumping(LED.Color(0,0,30),65); // blue
}
}
//========== pili1() function ============
void pili1(uint32_t c24, uint8_t waiting)
{ // an ON LED shifts right
for(int i=0; i<LED.numPixels(); i++) // fill the pixels one by one with a color
{ LED.setPixelColor(i, c24); // set the color of pixels
LED.show(); // update the whole strip
delay(waiting); // a delay of waiting value
LED.setPixelColor(i, 0); // turn all pixels off
}
// an ON LED shifts left
for(int i=0; i<LED.numPixels(); i++) // fill the pixels one by one with a color
{ LED.setPixelColor(LED.numPixels()-1-i, c24); // set the color of pixels
LED.show(); // update the whole strip
delay(waiting); // a delay of waiting value
LED.setPixelColor(LED.numPixels()-1-i, 0); // turn all pixels off
}
clearLED(); // turn all LEDs off
}
//========== pili2() function ============
void pili2(uint32_t c24, uint8_t waiting)
{ // turn on pixels from left to middle, turn on pixels from middle to right
for(int i=0; i<LED.numPixels()/2; i++) // fill the pixels one by one with a color
{ LED.setPixelColor(i, c24); // set the left pixels color
LED.setPixelColor(LED.numPixels()/2+i, c24); // set the right pixels color
LED.show(); // update the whole strip
delay(waiting); // a delay of waiting value
}
// turn off LEDs from middle to left, and turn off LEDs from right to middle
for(int i=0; i<LED.numPixels()/2; i++) // fill the pixels one by one with a color
{ LED.setPixelColor(LED.numPixels()/2-1-i, 0); // turn the left pixels off
LED.setPixelColor(LED.numPixels()-1-i, 0); // turn the right pixels off
LED.show(); // update the whole strip

9-38
delay(waiting); // a delay of waiting value
}
clearLED(); // turn all LEDs off
}
//========== pili3() function ============
void pili3(uint32_t c24, uint8_t waiting)
{ // turn on a single pixel from middle to left, and turn on a single pixel from middle to
right
for(int i=0; i<LED.numPixels()/2; i++) // fill the pixels one by one with a color
{ LED.setPixelColor(LED.numPixels()/2-1-i, c24); // turn the left pixels on
LED.setPixelColor(LED.numPixels()/2+i, c24); // turn the right pixels on
LED.show(); // update the whole strip
delay(waiting); // a delay of waiting value
LED.setPixelColor(LED.numPixels()/2-1-i, 0); // turn the left pixels off
LED.setPixelColor(LED.numPixels()/2+i, 0); // turn the right pixels on
}
clearLED(); // turn all LEDs off
}
//========== jumping() function ============
void jumping(uint32_t color, uint8_t waiting)
{ for (int i=0; i<6; i++) // repeat 6 times
{ for (int cShift=0; cShift < 3; cShift ++)
{ for (int j=0; j < LED.numPixels()/2; j+=3) // turn every third pixel on
{ LED.setPixelColor(LED.numPixels()/2-1-j+cShift, color); // left pixels
LED.setPixelColor(LED.numPixels()/2+j+cShift, color); // right pixels
}
LED.show(); // update the whole strip
delay(waiting); // a delay of waiting value
for (int j=0; j < LED.numPixels()/2; j+=3) // turn every third pixel off
{ LED.setPixelColor(LED.numPixels()/2-1-j+cShift, 0); // left pixels
LED.setPixelColor(LED.numPixels()/2+j+cShift, 0); // right pixels
}
}
}
clearLED(); // turn all LEDs off
}
//========== rainbow1() function ============
// static rainbow
void rainbow1(uint8_t waiting)
{ for(int j=0; j<256; j+=8) // decrease brightness gradually
{ LED.setBrightness(j/2); // set the brightness
for(int k=0; k<LED.numPixels(); k++) // fill the pixels one by one with a color
LED.setPixelColor(k, Wheel((k* 256 / LED.numPixels()) & 255));
LED.show(); // update the whole strip

9-39
delay((255-j)/3); // a delay of waiting value
}
clearLED(); // turn all LEDs off
}
//========== rainbow2() function ============
// counts represent the number of cycles
// change the waiting value to adjust the display time
void rainbow2(uint8_t counts, uint8_t waiting)
{ for(int i=0; i<counts; i++) // cycle the rainbow “counts” times
{ LED.setBrightness((i+1)*25); // set the brightness
for(int j=0; j<256; j++) // cycle all 256 colors in the wheel
{ for(int k=0; k< LED.numPixels(); k++) // fill the pixels one by one with a color
LED.setPixelColor(k, Wheel(((k* 256 /LED.numPixels()) + j) & 255));
LED.show(); // update the whole strip
delay(waiting); // a delay of waiting value
}
}
clearLED(); // turn all LEDs off
}
//========== Wheel() function ============
// Input a wheel position between 0 and 255 to create color value
// The colors are a transition: red  blue  green  red
uint32_t Wheel(byte WheelPos)
{ WheelPos = 255 - WheelPos;
if (WheelPos < 85) // if wheel position < 85
return LED.Color(255 - WheelPos * 3, 0, WheelPos * 3);
// fill color: R=255 - 3 x wheel position, G=0, B= 3 x wheel position
// wheel position =0: R=255, G=0, B= 0; red
// wheel position =84: R=3, G=0, B= 252; blue
// The colors are a transition from red to blue
else if (WheelPos < 170) // if 85 < wheel position < 170,
{ WheelPos -= 85; // subtract 85 from the wheel position
return LED.Color(0, WheelPos * 3, 255 - WheelPos * 3);
// fill color: R=0, G= 3 x wheel position, B= 255 - 3 x wheel position
// wheel position =0 (85): R=0, G=0, B= 255; blue
// wheel position =84 (169): R=0, G=252, B=3; green
// The colors are a transition from blue to green.
}
else // if wheel position > 170
{ WheelPos -= 170; // subtract 170 from the wheel position
return LED.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
// fill color: R=3 x wheel position, G= 255 – 3 x wheel position, B= 0
// wheel position =0 (170): R=0, G=255, B=0; green
// wheel position =84 (255): R=252, G=3, B=0; red

9-40
// The colors are a transition from green to red.
}
}
//========== clearLED() function ============
void clearLED()
{ for (int i=0;i<LED.numPixels();i++) // fill the pixels one by one with a color
LED.setPixelColor(i,0); // turn all pixels off
LED.show(); // update the whole strip
}

Compiling and Uploading

7. Once the editing is completed, press + keys to compile your sketch.


If “Error compiling” appears in the console, the message area will show the error
message with line number. If “Done compiling” appears in the console, the
message area will display memory usage for code and variables as follows:

Sketch uses 8,516 bytes (26%) of program storage space. Maximum is 32,256
bytes.

Global variables use 181 bytes (8%) of dynamic memory, leaving 1,867 bytes
for local variables. Maximum is 2,048 bytes.

8. Press + keys to upload the code to the Arduino board.

9. On your Android phone, execute the UnoSmile_E.apk app. The Uno & Smile
screen should appear as shown in Fig. 9-34.

9-41
Fig. 9-34 Uno & Smile screen

10. Tap button, the available Bluetooth devices will appear with a
unique identifier, followed by a name, as shown in Fig. 9-35(a). Select the
Bluetooth module HC-05. The prompt message “press Connect button” will
show in the Status field, as shown in Fig. 9-35(b).

(a) Available Bluetooth devices (b) HC-05 selected


Fig. 9-35 Select a BT device

9-42
11. Tap button to connect your Android phone to the Bluetooth module on
the MTS-100. If connected, the message “Connection Success” will be
displayed in the Status field, as shown in Fig. 9-36.

Fig. 9-36 Bluetooth connected

12. Test Serial RGB LED:


(1) Tap button. The serial RGB LED should show the lighting effect of
running LED.
(2) Tap button. The serial RGB LED should show the lighting effect of
windscreen wiper.
(3) Tap button. The serial RGB LED should show the lighting effect of
smiling eyebrows.
(4) Tap button. The serial RGB LED should show the lighting effect of
jumping lights.
(5) Tap button. The serial RGB LED should show a rainbow pattern.
(6) Tap button. The serial RGB LED should show the lighting effect of
rainbow cycle.

9-43
13. Test Digital Thermometer:
(1) Read the current temperature displayed in the Temperature: field.
(2) Blow on the DHT11 Humidity/Temperature Sensor to warm it. After a while,
the temperature displayed should increase.

14. Tap button. The Bluetooth link will be disconnected immediately, and
the “User Disconnection” message will be displayed in the Status field, as
shown in Fig. 9-37.

Fig. 9-37 Bluetooth disconnected

9-44