Raspberry Pi Bluetooth LE Controller for WS2812B (NeoPixels)


Bluetooth Low Energy (aka BLE/Bluetooth 4.0/Bluetooth Smart) is the most recent incarnation of Bluetooth technology developed by the Bluetooth SIG (the organization that maintains the specification). This communication protocol is designed for applications where data needs to be transferred in small amounts at relatively low speed while consuming low amounts of power (e.g., heart rate monitor, step counter, wireless keyboard). This latest version of the protocol is not compatible with its predecessor (Bluetooth classic), as an upside, long gone are the days where pairing devices was necessary!

The goal of this Instructable is to demonstrate how you can setup your Raspberry Pi to control RGB LEDs from a Bluetooth Low Energy (BLE) device nearby. Specifically, we’ll be using the RFduino microcontroller, which has a built-in BLE module. We’ll easily program the microcontroller to listen to messages sent by the Pi and control the LED color accordingly.

Let’s get started!Raspberry Pi Bluetooth LE Controller for WS2812B NeoPixels

Step 1: List of Materials

In this Instructable we’ll be using the following materials:

Materials:

1 x Raspberry Pi 2 with a 5V USB Power Supply

1 x 8GB MicroSD Card

1 x USB Bluetooth 4.0 Adapter – Most USB BLE adapters are RPi compatible (use CSR chip); check the compatibility here.

1 x RFduino Essentials Kit (2 pieces)

1 x 8mm PL9823-F8 (NeoPixels) RGB LED – Through-Hole, Diffused

1 x Premium Jumper Wires

Tools (Optional):

MicroSD USB Adapter

Step 2: System Overview

In order to control an RGB LED wirelessly we’ll be using a few different hardware and software components that we’ll outline here.

Raspberry Pi

We’ll be making use of a Raspberry Pi, a single-board computer running the Raspbian distribution of the Linux Operating System. Because the Pi doesn’t have a native BLE interface, we’ll connect a USB Bluetooth LE adapter to it. A script that we’ll write using the Python programming language will let us send commands BLE via the USB adapter. The commands sent over BLE will be received by a separate microcontroller via its own BLE interface (RFduino).

The list of components that will be used on this side of things is:

  • Raspberry Pi (hardware)
  • USB BLE Adapter (hardware)
  • Raspbian (software; Operating System)
  • Bluez Bluetooth stack for Linux (software; libraries, tools)
  • Python Script (software; user code)

RFduino

To control an RGB LED we’ll use the RFduino microcontroller connected to it on a solderless breadboard. The tiny-sized RFduino can be programmed over USB from any computer capable of running the Arduino IDE. In this case we’ll use a Mac laptop to write code in C++ using version 1.6.4 of the Arduino IDE. We’ll use the same IDE to upload the code to the RFduino. Once the code is running on the RFduino we’ll go back to the Pi and run the Python script to change the color of the RGB LED.

The list of components that will be used on this side of things is:

  • RFduino (hardware)
  • RGB LED (hardware)
  • Arduino IDE (software; program)
  • C++ firmare (software; user code)

Step 3: Configuring your Raspberry Pi to use Bluetooth LE

Currently, Bluetooth Low Energy (BLE) is not well supported by the standard Raspberry Pi distributions, thus some additional work is required to get it working. We’ve put together a comprehensive guid on how to get started in a previous Instructable:

http://www.instructables.com/id/Control-Bluetooth-LE-Devices-From-A-Raspberry-Pi/

Step 4: Configuring the RFduino in the Arduino IDE

After configuring the Raspberry Pi to use BLE, we’re ready to load a program to our RFduino. But, first we need to prepare our computer to recognize the RFduino boards.

At the time of this writing the following instructions have been tested in the latest version of the Arduino IDE (1.6.5). With your RFduino disconnected from the computer where you’ll be using the Arduino IDE to program the RFduino.

  1. Download and install Arduino IDE v1.6.5 or newer for your Operating System (OSX, Windows, or Linux)
  2. Edit the Arduino IDE Preferences/Settings, and under Additional Board Manager URLs add:
    http://rfduino.com/package_rfduino_index.json
  3. Navigate to Tools > Board > Board Manager and scroll down to RFduino Boards by RFduino. Click and select install the RFduino library.
  4. Double check that “RFduino” is one of the entries now under Tools > Board
  5. Connect the RFduino DIP Module to the RFduino USB Shield by matching the Pinouts. Then proceed to connect it to the computer’s USB port.
  6. Restart the Arduino IDE and the connected RFduino should be available under Tools > Port otherwise you may need to install the FTDI drivers for your Operating System.

Having trouble? Consult the comprehensive Quick Start guide by the RFduino creators.

Now that we’re able to program the RFduino, we can write the code that we’ll upload to it!

Step 5: Firmware for the RFduino: Receiving Messages

The goal for the program on the RFduino is to receive messages from the Pi, process them, and use the sent values to change the colors of the RGB LED. We’ll break down the development of the code into these 3 tasks.Raspberry Pi Bluetooth LE Controller for WS2812B NeoPixels schematich

Receiving Messages

The first step is to write the code that will allow the RFduino to receive messages from the Pi or any other BLE-capable device. The first thing we’ll setup is to broadcast under the id “My BLE LED”, and to idle in low-power mode while a message is received.

As with every program written in the Arduino IDE, we want to define the setup() and loop() functions:

void setup() {
// this is the data we want to appear in the advertisement
RFduinoBLE.advertisementData = “My BLE LED”;
// Start the BLE stack
RFduinoBLE.begin();
}

void loop() {
// Switch to lower power mode
RFduino_ULPDelay(INFINITE);
}

The RFduino functions we’ve called are standard for any piece of code that we write using this neat board. In order to receive messages, we’ll add the following 3 functions:

void RFduinoBLE_onConnect() {
// Debug message printed to Serial interface
Serial.println(“RFduino connected”);
}

void RFduinoBLE_onDisconnect() {
// Debug message printed to Serial interface
Serial.println(“RFduino disconnected”);

}

void RFduinoBLE_onReceive(char *data, int len) {
// Debug message printed to Serial interface
Serial.println(“Data received: “);
for(int i=0;i Serial.print(data[i]);
Serial.println();
Serial.println(data);
}

Now that we’ve examined the code, we’re ready to upload it to our RFduino. To do this, connect the RFduino DIP Module and the RFduino USB Shield. Then proceed to connect the connected boards to the USB port of your computer.

After uploading this simple program to the RFduino, we can test the communication from/to the Raspberry Pi!

 

 

For more detail: Raspberry Pi Bluetooth LE Controller for WS2812B (NeoPixels)


© 2015 Powered By Engineering Projects Team, Raspberry Pi Projects

Scroll to top