LED Bluetooth Control Using NRF Connect
by TechMartian in Circuits > Arduino
5324 Views, 15 Favorites, 0 Comments
LED Bluetooth Control Using NRF Connect
This is the last module on the Arduino 101 Essentials series and marks the beginning of a new Arduino 101 BLE uses series.
This is an introduction on how to use the Bluetooth Low Energy capabilities of the Arduino 101 to control an LED remotely over BLE4.1. Furthermore, this uses an app that is very versatile and flexible for developers and is only compatible with the Arduino 101, called nRF Connect, which is quite different from it's other bluetooth counterparts like Blink. This can be applied to a number of other applications, from reading bluetooth unlocked.
The fact that the Arduino 101 possesses Bluetooth Low Energy (BLE4.1) is what makes it unique and different from all the other Arduino boards. It allows us to connect with our circuits remotely over bluetooth, even from another room!
BoM
- Arduino 101 (or any bluetooth capable Arduino boards)
- LED
- 100Ω resistor
- Jumper Wire
- Breadboard
Wiring
- Connect the anode (positive pin) of the LED to a 100Ω resistor.
- Then, connect this 100Ω resistor in series to pin 9 on the Arduino.
- Connect the cathode (negative pin) of the LED to the ground pin of the Arduino 101 labeled as GND.
Coding
The code is vastly different and more complex than any simple blinking an LED demo, since it needs to activate the bluetooth and interface properly. You can also change the name of your board under the command,
blePeripheral.setLocalName("Tech Martian");
This is the name that will show up in the App itself, so name it uniquely.
#include <CurieBLE>h>
BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming) BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
//set BLE characteristic BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
const int ledPin = 9; // pin to use for the LED
void setup() { // set LED pin to output mode pinMode(ledPin, OUTPUT);
// set advertised local name and service UUID: blePeripheral.setLocalName("Tech Martian"); blePeripheral.setAdvertisedServiceUuid(ledService.uuid());
// add service and characteristic: blePeripheral.addAttribute(ledService); blePeripheral.addAttribute(switchCharacteristic);
// set the initial value for the characeristic: switchCharacteristic.setValue(0);
// begin advertising BLE service: digitalWrite (ledPin, HIGH); blePeripheral.begin(); }
void loop() { // listen for BLE peripherals to connect: BLECentral central = blePeripheral.central();
// if a central is connected to peripheral: if (central) { // while the central is still connected to peripheral: while (central.connected()) { // if the remote device wrote to the characteristic, // use the value to control the LED: if (switchCharacteristic.written()) { // any value other than 0, turn on the LED if (switchCharacteristic.value()) { digitalWrite(ledPin, HIGH); } //else turn the LED off else { digitalWrite(ledPin, LOW); } } } } }
Downloads
Using the NRF Connect
- Connect with your device, mine is called "Tech Martian," but yours may change.
- Under Unknown Service >> Unknown Characteristic, press the upload button.
- Choose Uint 8 on the drop down box and type 0 for OFF and 1 for ON
Done
Play with the LED by setting different values on the app! This is just the beginnings of what you can do with bluetooth control. More is coming, stay tuned makers!