Mix and math
A method for RGBA color mixing using Bluetooth® low energy communications
Namrata Dalvi, Microchip Technology
Controlling the color balance of light-emitting diodes (LEDs) accurately and wirelessly can be achieved using an 8-bit microcontroller and a Bluetooth® 4.1 low energy module to control the red, green, blue, alpha (RGBA) color space. The demonstration board shown in Fig. 1 has four LEDs – one each of red, green, blue and amber. The brightness of each of these LEDs is controlled through a pulse-width modulation (PWM) duty cycle.
This can be achieved using Microchip’s PIC16F1579 microcontroller, which has four 16-bit PWMs that are used to drive the LEDs. The 16-bit PWMs allow for precise control over the intensity of each color LED and the mixing of different RGBA brightness levels to create different colors.
Using mTouch® capacitive touch sensing technology enables the operation of two capacitive touch sliders. The on-board RN4020 Bluetooth module is used for receiving the PWM values from the Android™ mobile application or desktop program using Bluetooth low energy communication. The board is powered by a 1.5V AAA battery.
The light produced by the LEDs varies due to several factors. The brightness, measured in lumens, will vary for LEDs of different types, and between LEDs of the same type. For color LEDs, the specific color measured by the chromaticity values will differ from one LED to another. Small samples of a particular brand of LEDs were measured to develop a brightness and chromaticity profile. The values were then used as typical values in the hardware design and in the software’s chromaticity calculations. This process is called color tuning. The resistor values were fixed so that each color produced the same number of lumens. The LED series resistors are red 820W, blue 400W, green 500W and amber 500W.
There are two modes of operation: the first is hue saturation value plus white (HSVW) and brightness sliders mode; and the second is chromaticity selector using Bluetooth low energy. The board initially powers-up in mode one. There are two capacitive touch sliders on the board, one for color input and the other for controlling the brightness levels. If the first slider is touched while in slider mode, the color selected on the slider is output on the LEDs. The selected color is displayed until another input is received. The brightness of a particular color can be controlled with the other slider.
For the second mode, the color values (PWM) are selected using an Android-based mobile application or Windows-based desktop application. The respective PWM values are then sent to the board via a Bluetooth connection. The application uses the CIE 1931 XY chromaticity chart, see Fig. 2.
The exact PWM values for the selected color and brightness levels are computed and sent to the RGBA board over the Bluetooth connection. The Bluetooth module on the board then receives the PWM values, which are used by the RGBA board firmware to display the selected color.
fig. 2: CIE 1931 color space with RGBA LED color gamut
The chromaticity selector application GUI consists of the CIE 1931 xy chromaticity chart. The CIE 1931 color space shows a wide range of colors in terms of chromaticity (x) and luminance (y). The color and brightness levels of red, green and blue LEDs mapped onto the CIE color space define a triangle that encompasses all possible shades that can be generated by the output of three devices; this is known as the color gamut.
To obtain a better range of colors, an amber LED has been added. The xy data for the amber LED are mapped onto the CIE 1931 xy color space. This defines another triangle between the red, amber and green coordinates. Mixing red, amber and green in different proportions produces the colors within the color gamut in Fig. 2. The PC GUI and the Android applications used in this mode implement this color-mixing algorithm to calculate the PWM duty cycle values necessary to produce the desired color.
The chromaticity selector application sends the PWM values over a Bluetooth connection. This connectivity module will be able to communicate with mobile phones and PCs that contain Bluetooth v4.0 [and higher] transceivers. The module is primarily used for receiving duty cycle values from master devices that run the chromaticity selector application. The pin-to-pin configuration between the microcontroller and the BLE module is shown in Fig. 3.
Fig. 3: Interfaces between the Bluetooth low energy module on the left and the microcontroller
There are two types of Bluetooth devices – Bluetooth classic and Bluetooth low energy. A Bluetooth low energy device can only communicate with another BLE device or Bluetooth dual-mode device, which has both classic and low energy capabilities. Hence, the master host device must be BLE or Bluetooth dual-mode to communicate with the RN4020 module used on the RGBA board. The module complies with the Bluetooth core v4.1 specification and is controlled by the user through input and output lines and a UART interface. The UART supports ASCII commands to control or configure the module for any requirement based on the application.
When the board is operating in mode two, the desired LED color is selected from the chromaticity chart in the chromaticity selector application either from the RGBA color mixing desktop or Android application. The red, blue, green and amber PWM duty cycles are calculated by the application. Duty cycle values are passed on to the board by a Bluetooth low energy connection. The desktop application used was developed using Visual Studio C#.NET. The application follows the MVC principle with various classes.
The RGBA view controller class acts as the GUI or view manager and as the controller of the application. This class is at the top of the hierarchy responsible for making new objects of classes and performing dependency injection. It also handles all the GUI events and calls appropriate methods. The RGBA calculation class is responsible for finding out if the selected point is either inside the RGB or the RGA triangle or outside of these triangles, and calculates the duty cycle per color for all LEDs. The matrix 3x3 class implements all 3x3 matrix math operations such as inverse, determinant, transpose, co-factor and multiply. The Vector 3 class implements a column vector of size three to be used in matrix maths for the matrix 3x3 class. The RGBA data class is a custom data type to store the duty cycle values of all colors.
In the wireless communications wrapper class, the interface contains all the methods required by the wireless communications to implement the RGBA application. This interface can be used by any wireless communications method such as Bluetooth low energy and Bluetooth classic. The Bluetooth low energy communications are done using the RN4020 PICtail™ card through RS232 communications by implementing this interface for the RGBA board. The programmer can make a new class to implement wireless communications through built-in Bluetooth low energy libraries in Visual Studio or third party libraries. This interface decouples the implementation of the communications from the actual controller, so if new communications are implemented, the view controller and other classes will not change. The RGBA Bluetooth low energy communications via the RN4020 device class implements the wireless communications wrapper interface for Bluetooth low energy communications with the RGBA board. The PICtail card is used and connected to a PC via the UART or RS232 port. The serial communications are established and commands are sent for Bluetooth low energy communications.
The Bluetooth low energy device information class stores the basic information about the remote connectivity device - its name, address and supported server service. This information is used to identify and connect to a remote device. In the search result delegate class, the delegate services the event from the Bluetooth low energy class when it finishes the search for devices and the devices are available as a list for the user. The necessary time for the search operation is ten seconds. With the connection state change delegate class, the delegate services the event from the Bluetooth low energy class to determine if the master PICtail card is connected to a remote device or not and displays the current connection state to the user. The constants class stores all the constants required for the application, such as RN4020 module commands and responses, service and characteristic UUIDs and so on.
The Java™ application class for the Android operating system follows the MVC principle closely as well, using Android activity classes that are structurally similar to the desktop application. However, the Android application uses the built-in Bluetooth low energy hardware of the Android phone. The Android operating system provides all the necessary libraries for Bluetooth low energy communications with all required events and call backs. The RGBA view activity class is similar to the view controller class on a desktop except for the GUI controls, which are defined in an XML file instead of a class.
This article has shown how a 16-bit PWM allows for precise control over the intensity of each LED. The RGBA LED color mixing board described has slider capacitive touch buttons for color input and brightness control functions. A Bluetooth 4.1 low energy module was used for communications so the user could send PWM values to the RGBA board to output the desired color. The color was selected on a chromaticity selector application on a Windows desktop or on an Android-based phone.
LATEST issue 4/2018