Using hackaBLE with Arduino

If you are planning to buy a Bluetooth Low Energy (BLE) board or want to incorporate BLE functionality in your product, then hackaBLE could be a great candidate for the job!

hackaBLE is a tiny Open Source Nordic nRF52832 based BLE development board developed by Electronut Labs that you can embed in your BLE projects. hackaBLE is compatible with the Arduino development environment. For programming hackaBLE, you can use the Electronut Labs Bumpy, which is a black magic probe based programmer/debugger. In this article, we will explore how you can implement basic BLE functionalities with hackaBLE and Bumpy using Arduino IDE.

Objective

In this project, we will create a BLE custom service which will notify the temperature from a BME280 sensor read by a hackaBLE board. We will program it using bumpy and the Arduino IDE.

Hardware used

  • hackaBLE
  • BME280
  • bumpy
  • snapVCC
  • 9V battery

Hardware Setup

Pin Diagram

Pin hackaBLE
bme-SCL 3
bme-SDA 2
bumpy-VDD VDD
bumpy-GND GND
bumpy-SWDIO SWDIO
bumpy-SWDCLK SWDCLK
bumpy-RX 7
bumpy-TX 8

Setting up Arduino IDE

First, you need to install Sandeep Mistry’s Arduino Core for Nordic Semiconductor nRF5 based boards. Please visit this link and follow the steps to install board packages. Setting up Arduino IDE for programming hackaBLE is quite straightforward. Open your Arduino IDE and follow these steps.

1.Go to tools -> boards -> board manager and type hackaBLE.

2. Install nRF5 boards library by Sandeep Mistry version 0.6.0.

Now, you will be able to see Electronut Labs hackaBLE among boards list inside tools.

3. Select black magic probe as programmer inside tools->programmer->Black Magic Probe(GDB)

Flashing Softdevice to nRF5 boards

A softDevice is Nordic’s term for their BLE stack. Before you flash your code you need to flash the softdevice first. Go to your sketch folder for Arduino and follow these steps.

OS X: ~/Documents/Arduino Linux: ~/Arduino Windows: ~/Documents/Arduino

  1. Create the following directories: tools/nRF5FlashSoftDevice/tool/
  2. Download nRF5FlashSoftDevice.jar to /tools/nRF5FlashSoftDevice/tool/
  3. Restart the Arduino IDE
  4. Select your nRF board from the Tools -> Board menu
  5. Select a SoftDevice from the Tools -> “SoftDevice: ” menu
  6. Select a Programmer (J-Link, ST-Link V2, or CMSIS-DAP) from the Tools -> “Programmer: ” menu
  7. Select Tools -> nRF5 Flash SoftDevice
  8. Read license agreement
  9. Click “Accept” to accept the license and continue, or “Decline” to decline and abort
  10. If accepted, SoftDevice binary will be flashed to the board
  11. Create a folder named hardware inside your Arduino sketchbook folder, if it does not exist, and change directories to it
  12. Clone this repository sandeepmistry/nRF5 from github, to do that type on your terminal git clone https://github.com/sandeepmistry/arduino-nRF5.git

Note: When flashing the softdevice you may find it is not able to download the file because the URL is old. In that case, manually download the softdevice from here and paste the hex file to “sandeepmistry/nRF5/cores/nRF5/SDK/components/softdevice/s132/hex/”.

Initialize BLE and create a custom Service

If you have installed the Bluetooth peripheral library as directed above, then you will be able to see all the examples under BLE peripheral. These examples will give you a good understanding of how to go about creating Bluetooth low energy functionalities in your code. Refer to the code below, which will create a custom service and attach UUID to it.

// create BLE peripheral object
BLEPeripheral blePeripheral = BLEPeripheral();

// create service
BLEService update_tempService = BLEService("19b10000e8f2537e4f6cd104768a1214");

// create switch characteristic
BLECharCharacteristic updateCharacteristic = BLECharCharacteristic("19b10001e8f2537e4f6cd104768a1214", BLERead | BLENotify);

And set them inside setup function in Arduino like this

// set advertised local name and service UUID
  blePeripheral.setLocalName("BME_Collect");
  blePeripheral.setAdvertisedServiceUuid(update_tempService.uuid());

  // add service and characteristic
  blePeripheral.addAttribute(update_tempService);
  blePeripheral.addAttribute(updateCharacteristic);

  // assign event handlers for connected, disconnected to peripheral
  blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler);
  blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);

  // assign event handlers for characteristic
  updateCharacteristic.setEventHandler(BLESubscribed, characteristicSubscribed);
  updateCharacteristic.setEventHandler(BLEUnsubscribed, characteristicUnsubscribed);

That’s it. It is extremely easy to create a custom service in Arduino as compared to native SDK where it takes quite an amount of effort.

Update Temperature Data to BLE Characteristics

In this exercised for interfacing BME280 module, I have used Adafruit BME280 library. Now, the next task is to update temperature data to the characteristic as notification. Inside the main loop() we will check if the central is connected, if it is, we will update the temperature value every 1000 ms.

// check the heart rate measurement every 1000ms
    // as long as the central is still connected:
    while (central.connected())
    {
      long currentMillis = millis();
      // if 1000ms have passed, check the heart rate measurement:
      if (currentMillis - previousMillis >= 1000)
      {
        previousMillis = currentMillis;
       update_temperature_data(central, updateCharacteristic); // Function to read temperature data and update the characteristic
      }
    }

These were the explanation of some part of the code, you can find complete source code here.

Now, that the coding part is done, flash the softdevice and then flash your code. You can see the result on the nRFConnect app and the device will be advertising with the name BME_Collect.

If you connect to it, you will see an unknown characteristic with the read and notify options. If you enable notification you will be able to see the temperature data update every 1 sec.

Conclusion

I hope it is clear from this exercise that it’s easy to code on hackaBLE using Arduino IDE. It certainly makes exploring BLE functionalities simpler!

Firmware and Scripts

The code for this project is available on gitlab under hackable_arduino repository.

Acknowledgements

I would like thank my colleagues at Electronut Labs for their help.

(Article edited by Mahesh Venkitachalam.)