In this upcoming chapter, we delve into the process of assembling components and creating our inaugural application for controlling an Arduino board using Bluetooth Low Energy (BLE). The rationale behind exclusively adopting BLE for our Bluetooth-oriented undertakings is its status as the cutting-edge communication standard during the book's publication period. Unlike earlier Bluetooth modules, BLE modules exhibit diminished energy consumption, owing to their burst-operating nature instead of maintaining a continuous connection. Moreover, BLE boasts minimal transmission delays and offers a range akin to that of its older Bluetooth counterparts.
Our focal point entails establishing a connection between a BLE module and an Arduino, coupled with an LED that we'll manage through an Android app. Subsequently, we'll craft an Arduino sketch employing the aREST library, facilitating the reception of Bluetooth-initiated commands from a smartphone or tablet. The Android application itself will empower remote control over the board, and we'll have the chance to elevate user interaction by assimilating buttons for toggling the LED's state.
Key takeaways from this chapter encompass:
- Establishing a linkage between a BLE module and an Arduino board
- Developing an Arduino sketch to facilitate Bluetooth communication on the Arduino platform
- Crafting an Android app to dispatch commands to the Arduino board via Bluetooth
Hardware and software requirements
To begin this project, your initial requirement will be an Arduino Uno board. Subsequently, acquiring a BLE module becomes essential. We opted for the Adafruit nRF8001 chip due to its convenient Arduino library and pre-existing Android app examples for module control. Presented below is a detailed image of the module employed in our project:
You will require the following components for this project:
- An LED in a color of your preference
- A 330 Ohm resistor
- A breadboard
- Jumper wires
Here is a list of the hardware components along with web links for reference:
- Arduino Uno board: Link
- LEDs: Link
- 330 Ohm resistor: Link
- Adafruit nRF8001 breakout board: Link
- Breadboard: Link
- Jumper wires: Link
On the software side, you will need the following:
To install a library, extract the folder into your Arduino /libraries
directory or create the directory if it doesn't exist. To locate your Arduino folder or set a new one, access the Preferences option in the Arduino IDE.
Hardware Configuration:
Now, let's proceed with building the hardware part of the project. For your convenience, here's a schematic of the project:
Below are the sequential steps we will carry out:
Begin by situating both the Bluetooth module and the LED onto the breadboard.
Proceed to establish the power connection between the Arduino board and the breadboard as follows: Connect the Arduino board's 5V to the red power rail, and its GND to the blue power rail.
Now, we will establish the connection for the BLE module. Initially, link the module's power supply: Attach GND to the blue power rail, and VIN to the red power rail.
Subsequently, establish connections for the Serial Peripheral Interface (SPI) communication wires: Connect SCK to Arduino pin 13, MISO to Arduino pin 12, and MOSI to Arduino pin 11.
Next, establish connections for the REQ pin to Arduino pin 10. Additionally, connect the RDY pin to Arduino pin 2, and the RST pin to Arduino pin 9.
Addressing the LED component, place a resistor on the breadboard in series with the LED. Connect the resistor to the LED's anode, which is the longer pin.
Connect the other end of the resistor to Arduino pin 7.
Lastly, connect the LED's cathode (the shorter pin) to the blue power rail, i.e., the ground.
The project's fully assembled configuration is depicted in the provided image.
Writing the Arduino sketch
We will now write the Arduino sketch so that the Arduino board can talk with the BLE module and receive commands from Android via Bluetooth. Here is the complete sketch for this part:
// Control Arduino board from BLE
// Enable lightweight mode
#define LIGHTWEIGHT 1
// Libraries
#include <SPI.h>
#include "Adafruit_BLE_UART.h"
#include <aREST.h>
// Pins
#define ADAFRUITBLE_REQ 10
#define ADAFRUITBLE_RDY 2 // This should be an interrupt pin, on Uno thats #2 or #3
#define ADAFRUITBLE_RST 9
// Create aREST instance
aREST rest = aREST();
// BLE instance
Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);
void setup(void)
{
// Start Serial
Serial.begin(9600);
Serial.println(F("Adafruit Bluefruit Low Energy nRF8001"));
// Start BLE
BTLEserial.begin();
// Give name and ID to device
rest.set_id("001");
rest.set_name("my_arduino");
}
aci_evt_opcode_t laststatus = ACI_EVT_DISCONNECTED;
void loop() {
// Tell the nRF8001 to do whatever it should be working on.
BTLEserial.pollACI();
// Ask what is our current status
aci_evt_opcode_t status = BTLEserial.getState();
// If the status changed....
if (status != laststatus) {
// print it out!
if (status == ACI_EVT_DEVICE_STARTED) {
Serial.println(F("* Advertising started"));
}
if (status == ACI_EVT_CONNECTED) {
Serial.println(F("* Connected!"));
}
if (status == ACI_EVT_DISCONNECTED) {
Serial.println(F("* Disconnected or advertising timed out"));
}
// OK set the last status change to this one
laststatus = status;
}
// Handle REST calls
if (status == ACI_EVT_CONNECTED) {
rest.handle(BTLEserial);
}
}
This code is designed to control an Arduino board using Bluetooth Low Energy (BLE) communication. It uses the Adafruit Bluefruit LE UART Friend module to establish a wireless connection between the Arduino and another device (like a smartphone or computer) through BLE. The code also integrates the aREST library to enable remote control and monitoring of the Arduino's functions and data via REST API calls over the BLE connection.
Let's break down the code step by step:
Enable Lightweight Mode: A preprocessor directive is used to enable lightweight mode. This could be used to control the size of the compiled code, but specific details about how lightweight mode functions in this context are not provided here.
Import Libraries: Several libraries are imported for the functionality of the code:
SPI.h
: This library allows the Arduino to communicate over the Serial Peripheral Interface (SPI) protocol.Adafruit_BLE_UART.h
: This library provides functions for communication with the Adafruit Bluefruit LE UART Friend module.aREST.h
: This library enables RESTful communication between the Arduino and other devices.
Define Pins: The pins used for communication with the Adafruit Bluefruit LE UART module are defined. These include the request pin (
ADAFRUITBLE_REQ
), ready pin (ADAFRUITBLE_RDY
), and reset pin (ADAFRUITBLE_RST
).Create aREST Instance: An instance of the
aREST
class is created. This instance will be used to handle RESTful communication with the Arduino.Create BLE Instance: An instance of the
Adafruit_BLE_UART
class is created, initializing it with the previously defined pins.Setup Function: This function is executed once at the beginning of the program:
- Serial communication is started at a baud rate of 9600.
- A message is printed to indicate the start of the program.
- BLE communication is started by calling
BTLEserial.begin()
. - The Arduino's device ID and name are set using the
rest.set_id()
andrest.set_name()
functions.
Loop Function: The
loop()
function is where the main functionality of the program takes place:- The
BTLEserial.pollACI()
function is called to allow the nRF8001 (BLE module) to perform its necessary tasks. - The current status of the BLE connection is queried using
BTLEserial.getState()
. - If the status has changed since the last iteration, a corresponding message is printed based on the status change.
- If the status is "Connected" (
ACI_EVT_CONNECTED
), therest.handle()
function is called to handle incoming REST API calls from the connected device.
- The
Overall, this code sets up a communication link between an Arduino board and a BLE-enabled device, allowing for remote control and monitoring of the Arduino's functions through RESTful communication over BLE.
Now is the moment to transfer the sketch to your Arduino board. Once this step is complete, you can proceed with creating the Android app for managing the Arduino board using the BLE sketch.
See next an example project: Creating your first Android project:Hello Arduino