Arduino BLE Example Explained Step By Step - RootSaid
Maybe your like
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.
It is powered by Nina B306 module that supports 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.
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); 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);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.
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
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.
Tag » Arduino Nano 33 Ble Example Code
-
Connecting Nano 33 BLE Devices Over Bluetooth
-
Getting Started With The Arduino Nano 33 BLE
-
Getting Started With The Arduino Nano 33 BLE Sense
-
Get Started With Arduino Nano 33 BLE - OKdo
-
Arduino BLE Example Explained Using Arduino Nano 33 BLE Sense
-
Arduino NANO 33 Made Easy BLE, Sense And IoT - Instructables
-
Getting Started With Bluetooth LE On The Arduino Nano 33 Sense –
-
Arduino Nano 33 BLE Sense Overview
-
Tutorial 01: Accessing Sensor Values | Arduino Nano 33 BLE Sense ...
-
BLE On Arduino Nano 33 BLE Sense - BlueZ Connect - Blog
-
Arduino Nano 33 Sense | BLE Battery Level Tutorial
-
Communication Entre ARDUINO NANO 33 BLE SENSE?
-
Arduino Nano 33 BLE Sense - Kiwi Electronics
-
TannerGilbert/Arduino-Nano-33-BLE-Sense-Code-Collection - GitHub