Arduino 101 CurieBLE LED

With this tutorial you use the Arduino 101 Bluetooth® Low Energy capabilities to turn on and of the LED connected to Pin 13 from a smartphone or tablet.

With this tutorial you use the Arduino 101's onboard Bluetooth® Low Energy capabilities to turn on and of the LED connected to Pin 13 from a smartphone or tablet. You create a LED service and keep reading the Bluetooth® Low Energy central, looking for a writing event of the characteristic associated with the LED you want to control. This tutorial is similar to the Callback LED where the change is managed by polling and callback functions. The values are sent using nRF Master Control Panel(Bluetooth® Low Energy) app, available for Android and iOS.

Hardware Required

Software Required

  • nRF Master Control Panel(Bluetooth® Low Energy) for Android and iOS

The Circuit

genuino101fzz

image developed using Fritzing.

Software Essentials

Libraries

CurieBLE.h is the library that gives access to all the parameters, features and functions of the Bluetooth® Low Energy module of the 101 board. With Bluetooth® Low Energy it is possible to connect to and communicate with smartphones, tablets and peripherals that support this standard. In this tutorial it is used to establish a connection with a control application on the smartphone and get the value used to turn on or off a LED.

Functions

None

On the Smartphone

To drive the onboard LED of Arduino 101, you need the nRF Master Control Panel(Bluetooth® Low Energy) for Android and iOS. Launch it and do a SCAN. You should find the LED tab with a connect button.

BleLED 1

Tap on connect to open the following screen, where you find the description of our Bluetooth® Low Energy service offered by the 101 board. The unknown service has a UUID 19B10000-E8F2-537E-4F6C-D104768A1214 and it is set by the

BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214");
statement at the beginning of the sketch.

BleLED 2

Tap the Unknown Service to open up its characteristic, as shown below. It includes properties that can be written with a Write Request. you also find two icons on the right of the Unknown Characteristic. The arrow pointing down means read, the other means write.

BleLED 3

Tap the arrow pointing up to open the Write value popup, then set the data type to UINT 8

BleLED 4

Now tap on the line to write your chosen value (either "0" or "1"). As soon as you tap on send the value is sent to the 101 board and the LED instantly changes accordingly.

BleLED 5

Code

In this sketch you use the setup() to initialise and configure the Bluetooth® Low Energy peripheral. You start setting device name to LED, and configuring service UUID:

blePeripheral.setLocalName("LED");
blePeripheral.setAdvertisedServiceUuid(ledService.uuid());
Then you configure the Bluetooth® Low Energy service, and add switch characteristics (which is used to control the LED):

blePeripheral.addAttribute(ledService);
blePeripheral.addAttribute(switchCharacteristic);
You set switch characteristics value to 0 (default - LED off):

switchCharacteristic.setValue(0);
And finally you begin advertising the Bluetooth® Low Energy service that was set up in the previous steps:

blePeripheral.begin();

In the loop() you check the connection with a Bluetooth® Low Energy central and if connected you check if switch characteristic was written, and if so you read its value and set the LED state accordingly.

1/*
2
3 * Copyright (c) 2016 Intel Corporation. All rights reserved.
4
5 * See the bottom of this file for the license terms.
6
7 */
8
9#include <CurieBLE.h>
10
11BLEPeripheral blePeripheral; // Bluetooth® Low Energy Peripheral Device (the board you're programming)
12
13BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
14
15// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
16
17BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
18
19const int ledPin = 13; // pin to use for the LED
20
21void setup() {
22
23 Serial.begin(9600);
24
25 // set LED pin to output mode
26
27 pinMode(ledPin, OUTPUT);
28
29 // set advertised local name and service UUID:
30
31 blePeripheral.setLocalName("LED");
32
33 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());
34
35 // add service and characteristic:
36
37 blePeripheral.addAttribute(ledService);
38
39 blePeripheral.addAttribute(switchCharacteristic);
40
41 // set the initial value for the characeristic:
42
43 switchCharacteristic.setValue(0);
44
45 // begin advertising BLE service:
46
47 blePeripheral.begin();
48
49 Serial.println("BLE LED Peripheral");
50}
51
52void loop() {
53
54 // listen for Bluetooth® Low Energy peripherals to connect:
55
56 BLECentral central = blePeripheral.central();
57
58 // if a central is connected to peripheral:
59
60 if (central) {
61
62 Serial.print("Connected to central: ");
63
64 // print the central's MAC address:
65
66 Serial.println(central.address());
67
68 // while the central is still connected to peripheral:
69
70 while (central.connected()) {
71
72 // if the remote device wrote to the characteristic,
73
74 // use the value to control the LED:
75
76 if (switchCharacteristic.written()) {
77
78 if (switchCharacteristic.value()) { // any value other than 0
79
80 Serial.println("LED on");
81
82 digitalWrite(ledPin, HIGH); // will turn the LED on
83
84 } else { // a 0 value
85
86 Serial.println(F("LED off"));
87
88 digitalWrite(ledPin, LOW); // will turn the LED off
89
90 }
91
92 }
93
94 }
95
96 // when the central disconnects, print it out:
97
98 Serial.print(F("Disconnected from central: "));
99
100 Serial.println(central.address());
101
102 }
103}
104
105/*
106
107 Copyright (c) 2016 Intel Corporation. All rights reserved.
108
109 This library is free software; you can redistribute it and/or
110
111 modify it under the terms of the GNU Lesser General Public
112
113 License as published by the Free Software Foundation; either
114
115 version 2.1 of the License, or (at your option) any later version.
116
117 This library is distributed in the hope that it will be useful,
118
119 but WITHOUT ANY WARRANTY; without even the implied warranty of
120
121 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
122
123 Lesser General Public License for more details.
124
125 You should have received a copy of the GNU Lesser General Public
126
127 License along with this library; if not, write to the Free Software
128
129 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
130
131*/

Last revision 2016/04/05

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.