This is the first part of using a Bluetooth SoC from Nordic Semiconductor to drive a RGB LED strip composed of 12 WS2812b LEDs.
WS2812b LEDs are full color LEDs that can be controlled individually via one single wire, sold by AdaFruit as NeoPixel LEDs. I am not going to cover a lot of details about this LED but only the essentials. Much information is available online using keyword search on "ws2812b
" or "neopixel led
".
The nRF52832 is a Bluetooth SoC from Nordic Semiconductor. It features on chip Balun and is built around a 32-bit ARM® Cortex™-M4F CPU with 512kB + 64kB RAM, and come with support on Bluetooth 4.2. We are going to use this chip to drive a LED strip consists of 12 individually addressable WS2812b full color LEDs, and later in another blog, we will connect it using Bluetooth LE.
Instructions shown here assume you are working on a Mac. Other platforms such as Linux or Windows may need to adapt to a set of slightly different work flow. You can find more details at the bottom of this article about how to set up the environment on the Mac OS X for the development of applications on the nRF52832.
OverviewThe photo above shows all the components needed to develop the thing, including the nRF52 DK development board from Nordic Semiconductor, a voltage level shifter from Texas Instrument, a short strip of WS2812b LEDs, and a USB power bank capable of providing at least 2A of current, plus a few jumper wires.
WS2812bWS2812b LED is a LED integrated with a controller chip. The controller chip drives the three color LEDs as well as propagate the control signal to the LED upstream.
The format of the signal is very simple: 3 colors for each LED, ordered as Green-Red-Blue with MSB sent first. The timings of the binary 1 and 0, as well as the reset code are as below:
- A ‘0’ is denoted by a 0.35µs high followed by a 0.9µs low voltage.
- A ‘1’ is the reverse of a zero, i.e. a 0.9µs high followed by a 0.35µs low voltage.
A low of 50µs or longer denotes a Reset.
Note that both the '0' and '1' timings shall stay within ±150ns.
nRF52832The nRF52832 is a Bluetooth SoC with a powerful processor. To start development on the nRF52832, you will have to buy a development board called nRF52 DK. This development has on-board hardware debugger and firmware flasher, as well as few LEDs and buttons so that you can use it to prototype your Bluetooth hardware quickly.
Voltage ShifterI was hoping there was no need for any more components required other than the nRF52832 and the LEDs, believing the output of the SoC is enough to drive the LEDs like the Particle Photon. I was wrong. The output of the nRF52832 cannot drive the DI of the LED strip, I have to ended up using a logic level shifter to raise the signal to 5V TTL from the output of the nRF52832. I chose the (Takafumi Naka recommended) TXB0104 module for this. It’s 4-channels are a bit over-killed but I couldn’t find any TXB0101 (single channel) module out there and these chips are only available in SMT package.
The TXB01xx are bi-directional level shifters. They have two ports A and B. Signals are connected to both ports and their voltage levels are covereted to the corresponding power supply level of the individual ports. The ports on TXB01xx are bi-directional, that means it does not matter if it is A driving B or B driving A. The only requirement is the voltage level of B must be higher than that of A.
In our project, since our nRF52832 output has a lower voltage than we wanted and we need to convert it to a higher, 5V, voltage for the LEDs, therefore, port B of the TXB0104 will be our output connected to the LED (remember, voltage level of port B must be the higher one), and our port A (3.3V) will be our input connected to the output of nRF52832. We can use any one of the 4 ports available on the TXB0104 but the input port and the output port must have the same number.
The circuit is very simple, most of the heavy lifting works are already taken cared by the Bluetooth SoC and the LED strip, leaving only one external component – the voltage shifter:
On the left hand side, one connects 3 wires to the nRF52832 – VDD, GND and SDOUT (defaulted to pin 11 in the source code provided here but you are free to change it to any GPIO you can use on the nRF52832 in the file named config.h
).
On the right hand side, one connects another 3 wires to the WS2812b LED strip – +5V, GND and DI.
As each of the LED in the strip consumes ~60mA in full brightness, you will also use a 5V external power supply that can deliver enough current to your worse case scenario. See Power Supply below for more details.
Power SupplyPower supply to the nRF52832 is very simple, it can take anything from 1.8V to 4V and it consumes only very little current when the radio is not in use (in the range of 𝜇A). The same goes for the Voltage Level Shifter.
The LED strip, however, is very different. Each LED color consumes around 20mA in it’s maximum brightness (255) and linearly fall with the brightness value to 0mA (0) with a static current of ~1mA. That is, 10 LEDs on strip with full brightness will consume (60+1)x10 = 610mA; and 310mA in half-brightness.
You must therefore power the LED strip using a separate power supply that can deliver enough current for your LEDs.
CodeThe code is adapted from the one written by Takafumi Naka in his blog here. The version Takafumi Naka was using is only for nRF5 SDK 11 and does not work out of box on the latest SDK 12.1.0. However the changes are mostly superficial and can be quickly ported to the nRF5 SDK 12.
The file rainbow.c
provides the rainbow pattern to the LEDs. You can modify this file to design your own light patterns.
The timing logic is defined in the file i2s_ws2812b_drive.c
, and the supporting functions are defined in the file ws2812b_drive.c
. Usually, you shall have no reason to change this file unless you want to use a different type of LEDs such as WS2813 which has a different timing or signaling requirements.
You can just download the .hex file from my repository on Github and burn this to your nRF52832.
If you want to customize it or modify it for your own use, you will need to get the source code, also from my repository on Github. I will also assume you are familiar with the nRF5 SDK 12.1.0 and is comfortable working with the gcc
compiler or able to move a gcc-based project to your environment, whatever it is.
Create a directory inside your nRF52 examples/
directory, e.g. works/
, then place the directory containing the sources inside it. You should ended up with something like nRF52/examples/works/nRF52_WS2812B/.
You will need to have at least the nRF5 SDK 12.1.0 installed on your computer and a compatible compiler in order to compile from the source.
Assuming your nRF5 SDK is installed in ~/nRF52/
, simply change directory to pca10040/armgcc
and type make
. E.g.:
h:~ u$ pwd
/h/u/nRF52/examples/works/nRF52_WS2812B/
h:~ u$ cd pca10040/armgcc
h:~ u$ make
A make flash
will burn the firmware to the nRF52832. This version has not used any BLE functionality so there is no need to perform a make flash_softdevice.
The development board allows you to flash an external nRF52832 easily with only 3 wires (SWDIO, SWDCLK, GND). Get a nRF52832 module and flash it with the firmware, connect it to a battery and the voltage level shifter and off you go, a compact light show.
Setting up on the development environment a MacTo develop the nRF52832 on the Mac OS X, you will need the Xcode and have the xcode command line tools installed.
You will also need to obtain the nRF5 SDK (version 12.1.0 as of now) available from the nRF52 DK Download page, an IDE (Integrated Development Environment) and a compiler to create and build the program for the nRF52832.
One thing I like most from Nordic Semiconductor is that they have excellent tutorials to help you get start from the basics, and their tool chain works across different platforms from Windows to Linux to Mac OS X. I am using arm-gcc
for this project on my Mac using the arm-gcc for Mac but you have your choices over Eclipse and others. Make sure you have downloaded the latest updates of the nrfjprog programing tool for your platform (labeled as nRF5x-Command-Line-Tools-xxx in the nRF52 DK Download page) because you will need it to flash the compiled firmware (.hex file) onto the nRF52832.
Contrary to other vendor solutions, the nRF52 DK already contained within the development board a SEGGER JLink debugger allowing you to perform the debugging and flashing of the nRF52832 (both internally on the nRF52 DK or externally connected) without having to buy a separate JLink Debugger.
The SoftDevice is the Bluetooth stack necessary for any Bluetooth applications developed to work on the nRF52832. We don't need it for this part of the project and it is not required as part of the toolchain either. We will, however, need it in part two of this project when we added the Bluetooth control functionality. It is usually placed in <SDK>/components/softdevice/s132/hex/.
In summary, you will need the followings to start working on the Mac OS X:
- Xcode (Apple)
- Xcode Command Line Tool (Apple)
- gcc-arm for Mac (GNU ARM Embedded Toolchain )
- nRF5 SDK (Nordic Semiconductor)
- nRF5x-Command-Line-Tools-OSX (Nordic Semiconductor)
- s132 SoftDevice (Nordic Semiconductor)
Install all the above, most of them have their default install locations that you do not have to worry about. The nRF5 SDK can be extracted anywhere you prefer. You should also move the executable nrfjprog
to somewhere reachable from the PATH environment variable (/usr/local/bin
for example).
Make sure the arm-gcc
binary is in your PATH, verify using:
arm-none-eabi-gcc --version
Next, change the makefile.posix
inside the directory <
SDK
>/components/toolchain/gcc
where <
SDK
>
is the location of your nRF5 SDK. For example, if you have installed the arm-gcc
in /usr/local/arm-gcc
and it is of version 5.4, then the file should look like:
GNU_INSTALL_ROOT := /usr/local/arm-gcc
GNU_VERSION := 5.4
GNU_PREFIX := arm-none-eabi
To compile any of the examples that come with the nRF5 SDK, just open up a Terminal, change directory to one of the peripheral examples (not the BLE ones if you have not yet downloaded and installed the SoftDevices). Go deeper until you are in the same location as the Makefile
, then do a make
. You should see the output from the make producing a hex file. Type make flash
will flash the firmware to the nRF52832.
More detailed (outdated but largely applicable) information on working on a Mac with the nRF5 can be found here and here.
To-do'sNext we will add the BLE functionality to it so that we can control the pattern shown on the LED strip using a smartphone, or from NodeRed using noble.
Comments