in ,

Arduino BLE Example Explained Step by Step

Arduino BLE Example Code Explained

In this tutorial series, I will give you a basic idea you need to know about Bluetooth Low Energy and I will show you how you can make Arduino BLE Chipset to send and receive data wirelessly from mobile phones and other Arduino boards. Let’s Get Started.

Arduino Nano 33 BLE Sense

Today, I am here with a new version of Arduino Nano – Arduino Nano 33 BLE Sense, Nano with BLE connectivity focussing on IOT, which is packed with a wide variety of sensors such as 9 axis Inertial Measurement Unit, pressure, light, and even gestures sensors and a microphone.

Arduino Nano BLE 33 Sense
Arduino Nano BLE 33 Sense

 It is powered by Nina B306 module that support BLE as well as Bluetooth 5 connection. The inbuilt Bluetooth module consumes very low power and can be easily accessed using Arduino libraries. This makes it easier to program and enable wireless connectivity to any of your projects in no time.You won’t have to use external Bluetooth modules to add Bluetooth capability to your project. Save space and power.

Arduino BLE – Bluetooth Low Energy Introduction

BLE is a version of Bluetooth which is optimized for very low power consuming situations with very low data rate. We can even operate these devices using a coin cell for weeks or even months.

Arduino have a wonderful introduction to BLE but here in this post, I will give you a brief introduction for you to get started with BLE communication.

Basically, there are two types of devices when we consider a BLE communication block.

  • The Peripheral Device
  • The Central Device

Peripheral Device is like a Notice board, from where we can read data from various notices or pin new notices to the board. It posts data for all devices that needs this information.

Arduino Bluetooth Tutorial for Beginners Step by Step - Image from - https://www.arduino.cc/en/Reference/ArduinoBLE
BLE Devices – Peripheral and Central Devices

Central Devices are like people who are reading notices from the notice board. Multiple users can read and get data from the notice board at the same time. Similarly multiple central devices can read data from the peripheral device at the same time. 

The information that is given by the Peripheral devices are structured as Services. And These services are further divided into characteristics. Think of Services as different notices in the notice board and services as different paragraphs in each notice board.

If Accelerometer is a service, then their values X, Y and Z can be three characteristics.

Now let’s take a look at a simple Arduino BLE example. 

Arduino BLE Example 1 – Battery Level Indicator

In this example, I will explain how you can read the level of a battery connected to pin A0 of an Arduino using a smartphone via BLE. This is the code here. This is pretty much the same as that of the example code for Battery Monitor with minor changes. I will explain it for you.

First you have to install the library ArduinoBLE from the library manager.

Just go to Sketch -> Include Library -> Manage Library and Search for ArduinoBLE and simply install it.

Arduino BLE Tutorial Battery Level Indicator Code

#include <ArduinoBLE.h>
BLEService batteryService("1101");
BLEUnsignedCharCharacteristic batteryLevelChar("2101", BLERead | BLENotify);

void setup() {
Serial.begin(9600);
while (!Serial);

pinMode(LED_BUILTIN, OUTPUT);
if (!BLE.begin()) 
{
Serial.println("starting BLE failed!");
while (1);
}

BLE.setLocalName("BatteryMonitor");
BLE.setAdvertisedService(batteryService);
batteryService.addCharacteristic(batteryLevelChar);
BLE.addService(batteryService);
batteryLevelChar.writeValue(oldBatteryLevel);

BLE.advertise();
Serial.println("Bluetooth device active, waiting for connections...");
}

void loop() 
{
BLEDevice central = BLE.central();

if (central) 
{
Serial.print("Connected to central: ");
Serial.println(central.address());
digitalWrite(LED_BUILTIN, HIGH);

while (central.connected()) {

      int battery = analogRead(A0);
      int batteryLevel = map(battery, 0, 1023, 0, 100);
      Serial.print("Battery Level % is now: ");
      Serial.println(batteryLevel);
      batteryLevelChar.writeValue(batteryLevel);
      delay(200);

}
}
digitalWrite(LED_BUILTIN, LOW);
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}
}

Arduino Bluetooth Battery Level Indicator Code Explained

#include <ArduinoBLE.h>
BLEService batteryService("1101");
BLEUnsignedCharCharacteristic batteryLevelChar("2101", BLERead | BLENotify);

The first line of the code is to include the ArduinoBLE.h file. Then we will declare the Battery Service as well the battery level characteristics here. Here we will be giving two permissions  – BLERead and BLENotify. 

BLERead will allow central devices (Mobile Phone) to read data from the Peripheral device (Arduino).  And BLENotify allows remote clients to get notifications if this characteristic changes. 

Now we will jump on to the Setup function.

Serial.begin(9600);
while (!Serial);
pinMode(LED_BUILTIN, OUTPUT);
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}

Here it will initialize the Serial Communication and BLE and wait for serial monitor to open. 

Set a local name for the BLE device. This name will appear in advertising packets and can be used by remote devices to identify this BLE device.

BLE.setLocalName("BatteryMonitor");
BLE.setAdvertisedService(batteryService);
batteryService.addCharacteristic(batteryLevelChar);
BLE.addService(batteryService);
batteryLevelChar.writeValue(oldBatteryLevel);

Here we will add and set the value for the Service UUID and the Characteristic.

BLE.advertise();
Serial.println("Bluetooth device active, waiting for connections...");

And here, we will Start advertising BLE.  It will start continuously transmitting BLE advertising packets and will be visible to remote BLE central devices until it receives a new connection.

BLEDevice central = BLE.central();
if (central) {
Serial.print("Connected to central: ");
Serial.println(central.address());
digitalWrite(LED_BUILTIN, HIGH);

And here, the loop function. Once everything is setup and have started advertising, the device will wait for any central device. Once it is connected, it will display the MAC address of the device and it will turn on the builtin LED.

while (central.connected()) {
int battery = analogRead(A0);
int batteryLevel = map(battery, 0, 1023, 0, 100);
Serial.print("Battery Level % is now: ");
Serial.println(batteryLevel);
batteryLevelChar.writeValue(batteryLevel);
delay(200);
}

Now, it will start to read analog voltage from A0, which will be a value in between 0 and 1023 and will map it with in the 0 to 100 range. It will print out the battery level in the serial monitor and the value will be written for the batteryLevelchar charecteristics and waits for 200 ms. After that the whole loop will be executed again as long as the central device is connected to this peripheral device.

digitalWrite(LED_BUILTIN, LOW);
Serial.print("Disconnected from central: ");
Serial.println(central.address());

Once it is disconnected, a message will be shown on the central device and LED will be turned off. 

Installing the App for Android

In your Android smartphone, install the app “nRF Connect”. Open it and start the scanner. You will see the device “Battery Monitor” in the device list. Now tap on connect and a new tab will be opened. 

Arduino BLE Example
nRF Connect

Go to that and you will see the services and characteristics of the device.Tap on Battery service and you will the battery levels being read from the Arduino

Arduino Bluetooth Example
Arduino BLE with nRF Connect

In the next post, I will show you how you can send inbuilt sensor values such as accelerometer, gyroscope, color sensor and gesture sensor from the Arduino to your phone as well as another Arduino via BLE.

Written by Jithin

3 Comments

Leave a Reply
  1. Thanks a lot for your explanation. It’s really helpful. I am looking forward for your next post on sending the built-in sensor values to phone and sending parameters back from phone.

    The compilation of this program on for Nano BLE 33 sense takes a really long time 5-10mins. Is this normal ? if not how can I get over this problem.

  2. Your code does not compile.

  3. I get lots of errors like “ATT.cpp: warning: variable length array is used”. Please help!

Leave a Reply

Your email address will not be published. Required fields are marked *

Online PCB Assembly

Online PCB Assembly | JLCPCB SMT Assembly Service – JLCPCBA

Arduino Motor Shield

Arduino Motor Shield PCB V1 | 4 Motors at Once