Arduino 101 CurieBLE Button LED

With this tutorial you learn to use the Curie BLE library to connect your board with a smartphone or tablet.

With this tutorial you learn to use the Curie BLE library to connect your board with a smartphone or tablet. A pushbutton connected to the board allows you to turn on and off the onboard LED on Pin 13. The same action can be performed from the smartphone and the smartphone is capable of reading the LED status. This bidirectional communication happens between the BLE central (smartphone) and the peripheral (our 101 board).

Hardware Required

  • Arduino 101

  • pushbutton

  • 10k ohm resistor

  • breadboard

  • hook-up wires

  • Smartphone or Tablet Android or iOS

Software Required

  • nRF Master Control Panel (BLE) for Android and iOS

The Circuit

101 ButtonLed bb

image developed using Fritzing.

You need to connect a pushbutton to your 101 with a 10k ohm resistor that keeps the level of D4 LOW as long as the pushputton is not pressed. When this happens, the D4 pin is connected to 3.3V and it is read as HIGH.

Software Essentials

Libraries

CurieBLE.h is the library that gives access to all the parameters, features and functions of the BLE 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 either using the physical pushbutton or writing the value of a "virtual" pushbutton.

Functions

None

On the Smartphone

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

ButtonBLE 1

Tap on connect to open the following screen, where you find the description of our BLE 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.

ButtonBLE 2

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

ButtonBLE 3

If you tap on the second Unknown Characteristic and you set the rightmost little icon - the one with multiple down arrows - with an "X" on it, you enable notifications. This allows you to see the LED status that changes every time you press or release the pushbutton.

ButtonBLE 4

Tap the arrow pointing up in the first Unknown Characteristic to open the Write value popup, then set the data type to BYTE . This will add a small "0x" in front of the input line to show that an HEX format input is needed.

ButtonBLE 5

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

CallBackLED 6

Code

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
11const int ledPin = 13; // set ledPin to on-board LED
12
13const int buttonPin = 4; // set buttonPin to digital pin 4
14
15BLEPeripheral blePeripheral; // create peripheral instance
16
17BLEService ledService("19B10010-E8F2-537E-4F6C-D104768A1214"); // create service
18
19// create switch characteristic and allow remote device to read and write
20
21BLECharCharacteristic ledCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
22// create button characteristic and allow remote device to get notifications
23
24BLECharCharacteristic buttonCharacteristic("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify); // allows remote device to get notifications
25
26void setup() {
27
28 Serial.begin(9600);
29
30 pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output
31
32 pinMode(buttonPin, INPUT); // use button pin 4 as an input
33
34 // set the local name peripheral advertises
35
36 blePeripheral.setLocalName("ButtonLED");
37
38 // set the UUID for the service this peripheral advertises:
39
40 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());
41
42 // add service and characteristics
43
44 blePeripheral.addAttribute(ledService);
45
46 blePeripheral.addAttribute(ledCharacteristic);
47
48 blePeripheral.addAttribute(buttonCharacteristic);
49
50 ledCharacteristic.setValue(0);
51
52 buttonCharacteristic.setValue(0);
53
54 // advertise the service
55
56 blePeripheral.begin();
57
58 Serial.println("Bluetooth device active, waiting for connections...");
59}
60
61void loop() {
62
63 // poll peripheral
64
65 blePeripheral.poll();
66
67 // read the current button pin state
68
69 char buttonValue = digitalRead(buttonPin);
70
71 // has the value changed since the last read
72
73 boolean buttonChanged = (buttonCharacteristic.value() != buttonValue);
74
75 if (buttonChanged) {
76
77 // button state changed, update characteristics
78
79 ledCharacteristic.setValue(buttonValue);
80
81 buttonCharacteristic.setValue(buttonValue);
82
83 }
84
85 if (ledCharacteristic.written() || buttonChanged) {
86
87 // update LED, either central has written to characteristic or button state has changed
88
89 if (ledCharacteristic.value()) {
90
91 Serial.println("LED on");
92
93 digitalWrite(ledPin, HIGH);
94
95 } else {
96
97 Serial.println("LED off");
98
99 digitalWrite(ledPin, LOW);
100
101 }
102
103 }
104}
105
106/*
107
108 Copyright (c) 2016 Intel Corporation. All rights reserved.
109
110 This library is free software; you can redistribute it and/or
111
112 modify it under the terms of the GNU Lesser General Public
113
114 License as published by the Free Software Foundation; either
115
116 version 2.1 of the License, or (at your option) any later version.
117
118 This library is distributed in the hope that it will be useful,
119
120 but WITHOUT ANY WARRANTY; without even the implied warranty of
121
122 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
123
124 Lesser General Public License for more details.
125
126 You should have received a copy of the GNU Lesser General Public
127
128 License along with this library; if not, write to the Free Software
129
130 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-
131
132 1301 USA
133
134*/

Last revision 2016/04/05 by SM

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.