Professional Documents
Culture Documents
Bluetooth Smart Communication Using Microchip RN4020 Module and 16-Bit PIC Microcontroller
Bluetooth Smart Communication Using Microchip RN4020 Module and 16-Bit PIC Microcontroller
INTRODUCTION
Most of the embedded applications require real-time
communications to support their application
environment. Wired standards of communication, such
as RS232, RN422, RS485 or Ethernet are not easily
implemented due to the infrastructure support required
in the end application. Wi-Fi and Bluetooth have
emerged as the standards of choice for connecting
local embedded applications to the cloud through a
router, smartphone or tablet (i.e. a service call).
Wi-Fi, based on IEEE 802.11 b/g/n/ac, is a standard
feature in laptops, smart phones, smart machines and
many other applications. Wi-Fi provides substantial
bandwidth for data transfer; however, it is heavy in
protocol stack and power consumption. Recent efforts
to reduce power consumption in Wi-Fi, yielded lowenergy Wi-Fi, suitable for man-to-man or man-tomachine embedded application designs.
Bluetooth is characterized by easy, temporary
connectivity to smartphones and tablets, and is
supported in many Android and iOS applications
today. It provides a convenient cable replacement for
applications, such as audio streaming and data
synchronization between devices. Initially Bluetooth
supported 1 Mbps data transfer (Bluetooth 1.2), and
later the data transfer rate has increased to 3 Mbps with
the Enhanced Data Rate version (Bluetooth 2.1 +
EDR), and further advanced to a high-speed version
(Bluetooth 3.0 + HS) to support large file transfers.
Many applications only need simple command and
control, or a quick status from a sensor. Therefore,
Bluetooth Low Energy (BLE) has evolved to support
these low-duty cycle applications.
BLUETOOTH SMART
COMMUNICATION
Bluetooth Low Energy, also known as Bluetooth Smart,
is a hallmark in the Bluetooth 4.0 specification. BLE is
intended for energy-constrained applications, such as
sensors or disposable devices. BLE is intended for
low-duty cycle devices that support low-data
throughput and can operate for a longer duration
compared to other protocols from a coin cell battery.
Key benefits in implementing the technology are
inexpensive
silicon,
light
MCU
processing
requirements, and reduced memory is also quite
suitable for applications related to the body area
network (BAN) which represents a connectivity bubble
that moves along with the individuals network.
BLE operates in the same spectrum band (i.e 2.400
GHz to 2.4835 GHz ISM band) as Classic Bluetooth
technology, but uses a different set of channels. Classic
Bluetooth has 79 channels, each of 1 MHz wide,
whereas Bluetooth Smart has 40 channels, each of
2 MHz wide. Within a channel, data is transmitted using
Gaussian Frequency Shift Keying (GFSK) modulation
technique, which is similar to Classic Bluetooth's FSK
modulation. The maximum over-the-air bit rate is
1 Mbps, and the maximum transmit power is 10 mW.
For additional information related to Bluetooth and its
specifications, refer to Bluetooth Core Specification
V4.0
from
the
following
web
site:
http://www.bluetooth.org.
The Microchip RN4020 is a fully-certified Bluetooth
Version 4.1 module for adding a low-power wireless
capability to applications and products. The surface
mounted module has the complete Bluetooth stack onboard and is controlled through simple ASCII
commands over the UART interface. The RN4020 also
includes most of the Bluetooth SIG standard profiles
and Microchips Low-energy Data Profile (MLDP) for
user specific serial data transfer over BLE. Developers
can utilize the scripting feature to enable standalone
operation without a Host microcontroller or processor.
The RN4020 can be remotely controlled or updated
over-the-air by another module over a secure
connection, or can be controlled or updated through its
UART interface.
DS00001861A-page 1
AN1861
Figure 1 illustrates the RN4020 module mounted on a
PICtail board.
FIGURE 1:
DS00001861A-page 2
AN1861
RN4020 MODULE AND PIC24 MCU
INTERFACE FRAMEWORK
The primary purpose of this application note is to help
users or developers to have a quick understanding of
the interface requirements and process of
communication using commands between the RN4020
modules and the PIC24 (16-bit) microcontroller over
FIGURE 2:
RN4020 Module
(A)
RN4020 Module
(B)
HARDWARE REQUIREMENTS
The following hardwares are used for the demo
application:
Two BLE RN4020 PICtail/PICtail Plus daughter
boards
Two Explorer 16 Development boards
Any one of the following Microchip development
tools for programming and debugging: MPLAB
REAL ICE,MPLAB ICD 3, or PICkit 3
Two Power supplies: 9V/0.75A or equivalent battery packs
SOFTWARE/UTILITY REQUIREMENTS
The demo application that is based on the RN4020
module is used to showcase communication between
the BLE-based embedded nodes. In the demo
application, the nodes can emulate a sensor, actuator
systems, fitness device, healthcare gadget and so on.
DS00001861A-page 3
AN1861
Figure 3 illustrates pin to pin configuration used in the
application
demo
code
between
the
PIC24FJ128GA010 microcontroller and the RN4020
BLE module.
FIGURE 3:
VDD (3.3V)
PIC24FJ128GA010
MICROCONTROLLER
UART_Tx(5)
U1Rx
UART_Rx(6)
U1Tx
GND
CMD/MLDP/PI04(8)
MLDP_EV/CS/PI02(11)
RF0
(1)
GPIO
WAKE_SW/PI03(7)
RF1
CONNECT_STATUS/
SCK/PI05 (10)
RF6
CONNECT_ACTIVE/PI07 (19)
RF8
VDD (3.3V)
GND
RA4
LED_D7
RA5
LED_D8
RD6
SW_S3 (SCROLL)
RD7
SW_S6 (SELECT)
Note 1: This pin is optional and it is not used in this demo application setup.
DS00001861A-page 4
AN1861
Figure 4 illustrates the RN4020 module is mounted on the Explorer 16 Development board.
FIGURE 4:
GETTING STARTED
To set up the RN4020 BLE module as wireless node,
perform the following actions:
1.
2.
3.
4.
5.
6.
7.
DS00001861A-page 5
AN1861
RN4020 Demo Application
FIGURE 5:
1.
2.
3.
4.
5.
Reboot
No
If in Scan Mode?
Yes
Search for Peripheral Device
using Scroll Button (SW3)
No
If Peripheral Device
found?
(with relevant MAC ID)
Yes
Join or Connect
(using SW6)
Press Switch
(Monitor LEDs on Node B)
Monitor LEDs
(When Switches are pressed
in Node B)
End
DS00001861A-page 6
AN1861
Figure 6 and Figure 7 illustrate the configuration and scanning of Central (Node A).
FIGURE 6:
SW3 SCROLL
SW6 SELECT
DS00001861A-page 7
AN1861
FIGURE 7:
Figure 8 illustrates the BLE Central node demo showing switches for scroll and select. Figure 9 illustrates the BLE
Central node in the connected mode.
FIGURE 8:
DS00001861A-page 8
AN1861
FIGURE 9:
DS00001861A-page 9
AN1861
Configuring the Node B as Peripheral
FIGURE 10:
3.
4.
5.
Reboot
No
If in Auto-Adv.?
Yes
Wait for Connection Request
from Node A
No
If Request
Available?
Yes
Join or Connect
Press Switch
(Monitor LEDs on Node A)
Monitor LEDs
(When Switches are pressed
in Node A)
End
DS00001861A-page 10
AN1861
Figure 11 and Figure 12 illustrate the configuration and auto advertising of the BLE Peripheral (Node B).
FIGURE 11:
FIGURE 12:
DS00001861A-page 11
AN1861
Figure 13 illustrates the BLE Peripheral node when connected.
FIGURE 13:
DS00001861A-page 12
AN1861
Connecting Two Devices
X // Stop scanning
E,0,00035B0358E6 // Try to establish connection
with the device of public MAC address,
0x00035B0358E6.
F // Start scan
FIGURE 14:
LCD Display
Block
Display Interface
Display Interface
LCD Display
Block
Peripheral (Node B)
Central (Node A)
BT Smart
PIC
16-Bit
MCU
UART1 Interface
I/O Block
(LEDs/
Switches)
RN4020
Module
Communication
PIC
16-Bit
MCU
UART1 Interface
RN4020
Module
I/O Block
(LEDs/
Switches)
DS00001861A-page 13
AN1861
MLDP Mode Details
MLDP mode is entered by setting the CMD/MLDP pin
high, all data from the UART is sent to the peer device
as a data stream. To exit MLDP mode, the CMD/MLDP
pin must be set low so that the RN4020 module is
returned to Command mode by outputting CMD to the
UART. The CMD/MLDP pin (pin 8) is used to control the
RN4020 module when an MLDP serial data service is
used. For more information on MLDP Commands,
refer to RN4020 Bluetooth Low Energy Module Users
Guide (DS70005191).
After the access and characteristics in public services
are verified, the MLDP service can be started. The
MLDP service is built on top of the private service, but
acts transparently by routing binary data read and write
to a private characteristic through the UART. To use the
MLDP service between two RN4020 devices, both the
devices must enable MLDP with the SR command.
The MLDP mode can only be started when two
RN4020 modules are MLDP-enabled and are
connected to each other. To start MLDP mode, perform
the following actions:
1.
2.
3.
4.
5.
6.
7.
DS00001861A-page 14
AN1861
Running the Demo
4.
2.
3.
FIGURE 15:
5.
6.
7.
8.
BLE SMART
COMMUNICATION
DS00001861A-page 15
AN1861
NOTES:
DS00001861A-page 16
AN1861
APPENDIX A:
SOURCE CODE
Software License Agreement
The software supplied herewith by Microchip Technology Incorporated (the Company) is intended and supplied to you, the
Companys customer, for use solely and exclusively with products manufactured by the Company.
The software is owned by the Company and/or its supplier, and is protected under applicable copyright laws. All rights are reserved.
Any use in violation of the foregoing restrictions may subject the user to criminal sanctions under applicable laws, as well as to civil
liability for the breach of the terms and conditions of this license.
THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR
STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE
FOR SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
DS00001861A-page 17
AN1861
APPENDIX B:
TABLE 1:
File Name
File Type
Description
Central
.c and .h files
Central_string
.c and .h files
Config
Config_fuse
.h file
.c file
LCD
.c and .h files
LCD interface
Main
.c and .h files
UART
.c and .h files
TABLE 2:
File Name
ADC
File Type
.c and .h files
Description
Not used for this demo application
Config
.h file
Config_fuse
.c file
LCD
.c and .h files
LCD interface
Main
.c and .h files
Peripheral
.c and .h files
Peripheral_string
.c and .h files
UART
.c and .h files
DS00001861A-page 18
APPENDIX C:
Figure C-1 illustrates the functions used by the Central main() program in the
application code.
FIGURE C-1:
AN1861
DS00001861A-page 19
FIGURE C-2:
AN1861
DS00001861A-page 20
Figure C-2 illustrates the functions used by the Peripheral main() program in
the application code.
AN1861
APPENDIX D:
REFERENCED
SOURCES
CONCLUSION
This application note is designed to enable Microchip
Bluetooth customers to acquire basic understanding of
the Bluetooth Low Energy (BLE) and to work with
Microchip BLE RN4020 module. This application note
also provides sample codes for enabling the RN4020
modules as a Central node and Peripheral node
through 16-bit PIC microcontroller. The interface and
code examples can be used further as a framework for
any of the user applications or projects.
Other Resources
Bluetooth Core Specification 4.1 Adopted
Documents: https://www.bluetooth.org/en-us/
specification/adopted-specifications
Bluetooth 4.1 GATT Definitions Browser: https://
developer.bluetooth.org/gatt/Pages/DefinitionBrowser.aspx
DS00001861A-page 21
AN1861
NOTES:
DS00001861A-page 22
Note the following details of the code protection feature on Microchip devices:
Microchip products meet the specification contained in their particular Microchip Data Sheet.
Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the
intended manner and under normal conditions.
There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchips Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.
Microchip is willing to work with the customer who is concerned about the integrity of their code.
Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not
mean that we are guaranteeing the product as unbreakable.
Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our
products. Attempts to break Microchips code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.
Trademarks
The Microchip name and logo, the Microchip logo, dsPIC,
FlashFlex, flexPWR, JukeBlox, KEELOQ, KEELOQ logo, Kleer,
LANCheck, MediaLB, MOST, MOST logo, MPLAB,
OptoLyzer, PIC, PICSTART, PIC32 logo, RightTouch, SpyNIC,
SST, SST Logo, SuperFlash and UNI/O are registered
trademarks of Microchip Technology Incorporated in the
U.S.A. and other countries.
The Embedded Control Solutions Company and mTouch are
registered trademarks of Microchip Technology Incorporated
in the U.S.A.
Analog-for-the-Digital Age, BodyCom, chipKIT, chipKIT logo,
CodeGuard, dsPICDEM, dsPICDEM.net, ECAN, In-Circuit
Serial Programming, ICSP, Inter-Chip Connectivity, KleerNet,
KleerNet logo, MiWi, MPASM, MPF, MPLAB Certified logo,
MPLIB, MPLINK, MultiTRAK, NetDetach, Omniscient Code
Generation, PICDEM, PICDEM.net, PICkit, PICtail,
RightTouch logo, REAL ICE, SQI, Serial Quad I/O, Total
Endurance, TSHARC, USBCheck, VariSense, ViewSpan,
WiperLock, Wireless DNA, and ZENA are trademarks of
Microchip Technology Incorporated in the U.S.A. and other
countries.
SQTP is a service mark of Microchip Technology Incorporated
in the U.S.A.
Silicon Storage Technology is a registered trademark of
Microchip Technology Inc. in other countries.
GestIC is a registered trademarks of Microchip Technology
Germany II GmbH & Co. KG, a subsidiary of Microchip
Technology Inc., in other countries.
All other trademarks mentioned herein are property of their
respective companies.
2014, Microchip Technology Incorporated, Printed in the
U.S.A., All Rights Reserved.
ISBN:978-1-63276-840-7
== ISO/TS 16949 ==
2014 Microchip Technology Inc.
DS00001861A-page 23
ASIA/PACIFIC
ASIA/PACIFIC
EUROPE
Corporate Office
2355 West Chandler Blvd.
Chandler, AZ 85224-6199
Tel: 480-792-7200
Fax: 480-792-7277
Technical Support:
http://www.microchip.com/
support
Web Address:
www.microchip.com
India - Bangalore
Tel: 91-80-3090-4444
Fax: 91-80-3090-4123
Austria - Wels
Tel: 43-7242-2244-39
Fax: 43-7242-2244-393
Denmark - Copenhagen
Tel: 45-4450-2828
Fax: 45-4485-2829
Atlanta
Duluth, GA
Tel: 678-957-9614
Fax: 678-957-1455
Austin, TX
Tel: 512-257-3370
Boston
Westborough, MA
Tel: 774-760-0087
Fax: 774-760-0088
Chicago
Itasca, IL
Tel: 630-285-0071
Fax: 630-285-0075
Cleveland
Independence, OH
Tel: 216-447-0464
Fax: 216-447-0643
Dallas
Addison, TX
Tel: 972-818-7423
Fax: 972-818-2924
Detroit
Novi, MI
Tel: 248-848-4000
Houston, TX
Tel: 281-894-5983
Indianapolis
Noblesville, IN
Tel: 317-773-8323
Fax: 317-773-5453
Los Angeles
Mission Viejo, CA
Tel: 949-462-9523
Fax: 949-462-9608
New York, NY
Tel: 631-435-6000
San Jose, CA
Tel: 408-735-9110
Canada - Toronto
Tel: 905-673-0699
Fax: 905-673-6509
DS00001861A-page 24
Australia - Sydney
Tel: 61-2-9868-6733
Fax: 61-2-9868-6755
China - Beijing
Tel: 86-10-8569-7000
Fax: 86-10-8528-2104
China - Chengdu
Tel: 86-28-8665-5511
Fax: 86-28-8665-7889
China - Chongqing
Tel: 86-23-8980-9588
Fax: 86-23-8980-9500
China - Hangzhou
Tel: 86-571-8792-8115
Fax: 86-571-8792-8116
China - Hong Kong SAR
Tel: 852-2943-5100
Fax: 852-2401-3431
China - Nanjing
Tel: 86-25-8473-2460
Fax: 86-25-8473-2470
China - Qingdao
Tel: 86-532-8502-7355
Fax: 86-532-8502-7205
China - Shanghai
Tel: 86-21-5407-5533
Fax: 86-21-5407-5066
China - Shenyang
Tel: 86-24-2334-2829
Fax: 86-24-2334-2393
China - Shenzhen
Tel: 86-755-8864-2200
Fax: 86-755-8203-1760
China - Wuhan
Tel: 86-27-5980-5300
Fax: 86-27-5980-5118
China - Xian
Tel: 86-29-8833-7252
Fax: 86-29-8833-7256
France - Paris
Tel: 33-1-69-53-63-20
Fax: 33-1-69-30-90-79
Germany - Dusseldorf
Tel: 49-2129-3766400
Germany - Munich
Tel: 49-89-627-144-0
Fax: 49-89-627-144-44
Germany - Pforzheim
Tel: 49-7231-424750
Italy - Milan
Tel: 39-0331-742611
Fax: 39-0331-466781
Italy - Venice
Tel: 39-049-7625286
Netherlands - Drunen
Tel: 31-416-690399
Fax: 31-416-690340
Malaysia - Penang
Tel: 60-4-227-8870
Fax: 60-4-227-4068
Poland - Warsaw
Tel: 48-22-3325737
Philippines - Manila
Tel: 63-2-634-9065
Fax: 63-2-634-9069
Singapore
Tel: 65-6334-8870
Fax: 65-6334-8850
Taiwan - Hsin Chu
Tel: 886-3-5778-366
Fax: 886-3-5770-955
Spain - Madrid
Tel: 34-91-708-08-90
Fax: 34-91-708-08-91
Sweden - Stockholm
Tel: 46-8-5090-4654
UK - Wokingham
Tel: 44-118-921-5800
Fax: 44-118-921-5820
Taiwan - Kaohsiung
Tel: 886-7-213-7830
Taiwan - Taipei
Tel: 886-2-2508-8600
Fax: 886-2-2508-0102
Thailand - Bangkok
Tel: 66-2-694-1351
Fax: 66-2-694-1350
China - Xiamen
Tel: 86-592-2388138
Fax: 86-592-2388130
China - Zhuhai
Tel: 86-756-3210040
Fax: 86-756-3210049
03/25/14