Controlling RGB LED Through Bluetooth®
Learn how to control the built in RGB LED on the Nano 33 BLE Sense board over Bluetooth®, using an app on your phone.
In this tutorial we will use an Arduino Nano 33 BLE Sense, to turn on an RGB LED over Bluetooth®, made possible by the communications chipset embedded on the board.
Goals
The goals of this project are:
- Learn what Bluetooth® Low Energy and Bluetooth® are.
- Use the Arduino BLE library.
- Learn how to create a new service.
- Learn how to turn on a RGB LED from an external device (smartphone).
Hardware & Software Needed
- This project uses no external sensors or components.
- In this tutorial we will use the Arduino Web Editor to program the board.
Bluetooth® Low Energy and Bluetooth®
Bluetooth® Low Energy separates itself from what is now known as “Bluetooth® Classic” by being optimized to use low power with low data rates. There are two different types of Bluetooth® devices: central or peripheral. A central Bluetooth® device is designed to read data from peripheral devices, while the peripheral devices are designed to do the opposite. Peripheral devices continuously post data for other devices to read, and it is precisely what we will be focusing on this tutorial.
Service & Characteristics
A service can be made up of different data measurements. For example, if we have a device that measures wind speed, temperature and humidity, we can set up a service that is called “Weather Data”. Let’s say the device also records battery levels and energy consumption, we can set up a service that is called “Energy information”. These services can then be subscribed to central Bluetooth® devices.
Characteristics are components of the service we mentioned above. For example, the temperature or battery level are both characteristics, which record data and update continuously.
Unique Universal Identifier (UUID)
When we read data from a service, it is important to know what type of data we are reading. For this, we use UUIDs, who basically give a name to the characteristics. For example, if we are recording temperature, we want to label that characteristic as temperature, and to do that, we have to find the UUID, which in this case is “2A6E”. When we are connecting to the device, this service will then appear as “temperature”. This is very useful when tracking multiple values.
If you want to read more about UUIDs, services, and characteristics, check the links below:
Creating the Program
1. Setting up
Let's start by opening the Arduino Web Editor, click on the Libraries tab and search for the ArduinoBLE library. Then in > Examples > Peripheral, open the LED sketch and once it opens, you could rename it as desired.
2. Connecting the board
Now, connect the Arduino Nano 33 BLE Sense to the computer and make sure that the Web Editor recognizes it, if so, the board and port should appear as shown in the image below. If they don't appear, follow the instructions to install the plugin that will allow the Editor to recognize your board.
3.Turning ON the LED
Now we will need to modify the code on the example, in order to turn the RGB LED on in different colors, based on the information we sent through the smartphone.
After including the
ArduinoBLE.h
library, in the ledService()
function change the argument to "180A"
which is translated to "Device Information". Then in the switchCharacteristic()
function change the argument to "2A57"
which is translated to "Digital Output".1BLEService ledService("180A"); // BLE LED Service2
3// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central4BLEByteCharacteristic switchCharacteristic("2A57", BLERead | BLEWrite);
In the
setup()
after initializing the serial communication we set the LED's pins as OUTPUT
, and then turn them OFF, including the LED_BUILTIN
that we will use to show if we are connected to the board. 1// set LED pin to output mode2pinMode(LEDR, OUTPUT);3pinMode(LEDG, OUTPUT);4pinMode(LEDB, OUTPUT);5pinMode(LED_BUILTIN, OUTPUT);6
7digitalWrite(LED_BUILTIN, LOW); // when the central disconnects, turn off the LED8digitalWrite(LEDR, HIGH); // will turn the LED off9digitalWrite(LEDG, HIGH); // will turn the LED off10digitalWrite(LEDB, HIGH); // will turn the LED off
In the
BLE.setLocalName()
we need to change the name to "Nano 33 BLE Sense"
in order to identify the board we are using.1// set advertised local name and service UUID:2 BLE.setLocalName("Nano 33 BLE Sense");3 BLE.setAdvertisedService(ledService);
Now, in the
loop()
after the Serial.println(central.address());
we need to set the LED_BUILTIN
to HIGH
to turn on the LED when the phone is connected to the board. 1// print the central's MAC address:2Serial.println(central.address());3digitalWrite(LED_BUILTIN, HIGH); // turn on the LED to indicate the connection
Then we replace the second
if()
function inside the while
loop with a switch case
function, where each case will turn each color of the LED.1// while the central is still connected to peripheral:2while (central.connected()) {3
4// if the remote device wrote to the characteristic,5// use the value to control the LED:6
7if (switchCharacteristic.written()) {8switch (switchCharacteristic.value()) { // any value other than 09 case 01:10 Serial.println("Red LED on");11 digitalWrite(LEDR, LOW); // will turn the LED on12 digitalWrite(LEDG, HIGH); // will turn the LED off13 digitalWrite(LEDB, HIGH); // will turn the LED off14 break;15 case 02:16 Serial.println("Green LED on");17 digitalWrite(LEDR, HIGH); // will turn the LED off18 digitalWrite(LEDG, LOW); // will turn the LED on19 digitalWrite(LEDB, HIGH); // will turn the LED off20 break;21 case 03:22 Serial.println("Blue LED on");23 digitalWrite(LEDR, HIGH); // will turn the LED off24 digitalWrite(LEDG, HIGH); // will turn the LED off25 digitalWrite(LEDB, LOW); // will turn the LED on26 break;27 default:28 Serial.println(F("LEDs off"));29 digitalWrite(LEDR, HIGH); // will turn the LED off30 digitalWrite(LEDG, HIGH); // will turn the LED off31 digitalWrite(LEDB, HIGH); // will turn the LED off32 break;33 }34 } 35}
Lastly, after the last
Serial.println()
function we use the digitalWrite()
to turn off the LED once the phone is disconnected from the board.1// when the central disconnects, print it out:2Serial.print(F("Disconnected from central: "));3Serial.println(central.address());4digitalWrite(LED_BUILTIN, LOW); // when the central disconnects, turn off the LED5digitalWrite(LEDR, HIGH); // will turn the LED off6digitalWrite(LEDG, HIGH); // will turn the LED off7digitalWrite(LEDB, HIGH); // will turn the LED off8}9}
4. Complete code
If you choose to skip the code building section, the complete code can be found below:
1#include <ArduinoBLE.h>2
3BLEService ledService("180A"); // BLE LED Service4
5// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central6BLEByteCharacteristic switchCharacteristic("2A57", BLERead | BLEWrite);7
8void setup() {9 Serial.begin(9600);10 while (!Serial);11
12 // set LED's pin to output mode13 pinMode(LEDR, OUTPUT);14 pinMode(LEDG, OUTPUT);15 pinMode(LEDB, OUTPUT);16 pinMode(LED_BUILTIN, OUTPUT);17 18 digitalWrite(LED_BUILTIN, LOW); // when the central disconnects, turn off the LED19 digitalWrite(LEDR, HIGH); // will turn the LED off20 digitalWrite(LEDG, HIGH); // will turn the LED off21 digitalWrite(LEDB, HIGH); // will turn the LED off22
23 // begin initialization24 if (!BLE.begin()) {25 Serial.println("starting Bluetooth® Low Energy failed!");26
27 while (1);28 }29
30 // set advertised local name and service UUID:31 BLE.setLocalName("Nano 33 BLE Sense");32 BLE.setAdvertisedService(ledService);33
34 // add the characteristic to the service35 ledService.addCharacteristic(switchCharacteristic);36
37 // add service38 BLE.addService(ledService);39
40 // set the initial value for the characteristic:41 switchCharacteristic.writeValue(0);42
43 // start advertising44 BLE.advertise();45
46 Serial.println("BLE LED Peripheral");47}48
49void loop() {50 // listen for Bluetooth® Low Energy peripherals to connect:51 BLEDevice central = BLE.central();52
53 // if a central is connected to peripheral:54 if (central) {55 Serial.print("Connected to central: ");56 // print the central's MAC address:57 Serial.println(central.address());58 digitalWrite(LED_BUILTIN, HIGH); // turn on the LED to indicate the connection59
60 // while the central is still connected to peripheral:61 while (central.connected()) {62 // if the remote device wrote to the characteristic,63 // use the value to control the LED:64 if (switchCharacteristic.written()) {65 switch (switchCharacteristic.value()) { // any value other than 066 case 01:67 Serial.println("Red LED on");68 digitalWrite(LEDR, LOW); // will turn the LED on69 digitalWrite(LEDG, HIGH); // will turn the LED off70 digitalWrite(LEDB, HIGH); // will turn the LED off71 break;72 case 02:73 Serial.println("Green LED on");74 digitalWrite(LEDR, HIGH); // will turn the LED off75 digitalWrite(LEDG, LOW); // will turn the LED on76 digitalWrite(LEDB, HIGH); // will turn the LED off77 break;78 case 03:79 Serial.println("Blue LED on");80 digitalWrite(LEDR, HIGH); // will turn the LED off81 digitalWrite(LEDG, HIGH); // will turn the LED off82 digitalWrite(LEDB, LOW); // will turn the LED on83 break;84 default:85 Serial.println(F("LEDs off"));86 digitalWrite(LEDR, HIGH); // will turn the LED off87 digitalWrite(LEDG, HIGH); // will turn the LED off88 digitalWrite(LEDB, HIGH); // will turn the LED off89 break;90 }91 }92 }93
94 // when the central disconnects, print it out:95 Serial.print(F("Disconnected from central: "));96 Serial.println(central.address());97 digitalWrite(LED_BUILTIN, LOW); // when the central disconnects, turn off the LED98 digitalWrite(LEDR, HIGH); // will turn the LED off99 digitalWrite(LEDG, HIGH); // will turn the LED off100 digitalWrite(LEDB, HIGH); // will turn the LED off101 }102}
Testing It Out
Once we are finished with the coding, we can upload the sketch to the board. When it has successfully uploaded, open the Serial Monitor. In the Serial Monitor, the text "BLE LED Peripheral" will appear as seen in the image below.
We can now discover our Nano 33 BLE Sense board in the list of available Bluetooth® devices. To access the service and characteristic we recommend using the LightBlue application. Follow this link for iPhones or this link for Android phones.
Once we have the application open, follow the image below for instructions:
To control the RGB LED, we simply need to write 1,2 or 3 in the "WRITTEN VALUES" field to turn on the red, blue or the green LED and any other value to turn them off. This is within the "Digital Output" characteristic, which is located under "Device Information".
Troubleshoot
Sometimes errors occur, if the code is not working there are some common issues we can troubleshoot:
- Missing a bracket or a semicolon.
- Arduino board connected to the wrong port.
- We haven't opened the Serial Monitor to initialize the program.
- The device you are using to connect has its Bluetooth® turned off.
Conclusion
In this tutorial we have created a basic Bluetooth® peripheral device. We learned how to create services and characteristics, and how to use UUIDs from the official Bluetooth® documentation. Lastly, we turn on different colors of the RGB LED based on the values sent from the smartphone.
Now that you have learned a little bit how to use the ArduinoBLE library, you can try out some of our other tutorials for the Nano 33 BLE Sense board. You can also check out the ArduinoBLE library for more examples and inspiration for creating Bluetooth® projects!
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.