Extending the Rainbow HAT for the Raspberry Pi with More Peripherals
The Rainbow HAT for the Raspberry Pi is a board for learning physical computing and IoT programming recommended for Google’s Android Things. The HAT comes with a “buffet” (in the manufacturer’s own words) of sensors and actuators that are fun to mess with. But if you are tired of eating the same buffet every day, you can always connect your own peripherals to its breakout GPIO pins — PWM0, UART0, I2C1, and SPI0.1 — to create more interesting projects. These open pins can be found on the left side of the board. In this article, I show how to connect a strip of RGB LED lights through the Serial Peripheral Interface (SPI) bus as an example of extending the functionality of the Rainbow HAT. If you would like to create the animated glowing effect shown in Figure 1 below with the Rainbow HAT, this article is for you.
The Adafruit DotStar model
As the first try, I used the half-meter DotStar Digit LED Strip from Adafruit. This strip has 72 addressable RGB LED lights and a 4-pin JST SM male connector on one end. Out of the four pins, one is supposed to connect to a power source (3V3 or 5V), one to the ground (GND), one to the Serial Clock (SCLK), and the last one to the Master Output Slave Input (MOSI) to which the data will be written. You should connect the two non-power pins to the SCLK and MOSI pins of the Rainbow HAT (preferably using two female-to-female jump wires to connect them directly if you have them). Note that the LED lights on the added strip and those on the HAT are controlled by the same master on the SPI bus (Figure 2). Although we can specify which slave the master will control by using the Slave Select (SS) line, I haven’t figured out how to do that with the Rainbow HAT. As a result, the seven LED lights on the HAT display the same pattern as the first seven LED lights on the strip.
You can directly power the LED strip using a 5V pin and a ground pin from the Rainbow HAT, as shown in Figure 3 below (the power wires of the strip are connected to the upper-left corner of the breadboard in the image).
Alternatively, you can also use a separate power source. I used a 12 V power adapter and a YwRobot module to power my breadboard and then used two jump wires to connect the breadboard +/- lines to the power and ground pins of the JST SM connector. An alternative power source is a 9 V battery holder that has a 5.5 mm×2.1 mm plug and an ON/OFF switch that can be easily connected to and disconnected from the YwRobot module. An important thing to remember is to connect the ground of the external power source and a ground pin of the Rainbow HAT. Otherwise, you won’t be able to set the correct state for your strip. Figure 4 shows the setup with an external power source.
The APA102 serial LED data protocol
After we are done with the wiring part, the next step is to write computer code that drives the LED strip to exhibit interesting patterns — something known as LED art. This is the most fun part of the project. You can choose your favorite programming language supported in the Raspberry Pi to do the job (I used Java with the Pi4J library in my case). Before we start coding, we need to understand the APA102 serial LED data protocol for controlling the LED strip. Figure 5 illustrates the APA102 scheme, which is actually quite simple. Be sure to remember to start each data packet you send to the SPI bus with a start frame and end it with an end frame as suggested in Figure 5. You can control the brightness of each LED light using the last five bits of the first data byte (marked as “Global” in Figure 5). Although there are 32 levels of brightness, I found the first level was all I need. A brightness level of 32 is too strong to eyes if you have to look at the lights at a close distance for a long time.
One interesting thing to do with a LED strip is to show a rainbow across it. This can be easily done in the HSV color space— all we need to do is to assign HSV(0, 1, 1) — which is red — to the LED at one end of the strip and HSV(1–1/N, 1, 1) — which is close to red — to the LED on the other end, assuming N is the number of LEDs on the strip. Any LED in the middle gets the interpolated HSV color based on its position. Figure 6 shows the result of the rainbow continuum.
Figure 7 shows that by attaching the strip to a computer monitor, we can create an additional display. I am not sure about the actual applications, though. But a glowing LED strip looks better than a tedious gradient color bar inside the screen, which does not glow.
The SK9822 model
I also tested the SK9822 model, which is not waterproof but significantly cheaper than the DotStar model. The SK9822 is similar to APA102 but has a slightly different behavior. I taped two of them to the edge side of my table so that I could easily see them while tinkering, as shown in Figure 8. Unlike the DotStar strip, the SK9822 has a male JST SM connector at one end and a female one at the other end, which we can use to link the next strip. So I plugged in the connector and made a two-meter-long LED strip.
The particular setup — with one strip above the other — allowed me to create visual loops like the one shown in the video below.
To control the peripherals, I have started to develop a Java program for the Raspberry Pi called the IoT Workbench, which will eventually support lay persons to design and develop simple IoT apps. Figure 9 shows the Rainbow HAT emulator in the IoT Workbench. The goal of the emulator is to support the user to design a virtual prototype to test an idea without connecting to the actual hardware. In the case of the Rainbow HAT, the user can click the buttons or LED lights in the emulator and interact with them in the way that they are programmed. The actual actuators will react to the user’s actions in the same way once they are connected.
In addition to the example code that can drive the basic peripherals and the extended APA102 LED strip, this emulator also provides graphs that visualize the sensor data collected over time. The graphs are shown on a translucent window superimposed on the emulator canvas. Figures 10 and 11 show the temperature and the barometric pressure as a function of time, recorded over 20 minutes, respectively. The fluctuation of the temperature roughly reflects the duty cycle of the heating system of my home. The average temperature is higher than the room temperature because the on-board sensor is affected by the heat dissipation of the Raspberry Pi when we run the app. While the on-board sensors are not very useful in reality, we can connect other sensors through the I2C and SPI interfaces and use the app as a data logger and display. Extending the sensing capability of the Rainbow HAT will be covered in my future articles.
The app also provides an easy way to export the data logged by the sensors of the Rainbow HAT, as is shown in Figure 12.
The following video shows the IoT Workbench in action with the extended Rainbow HAT. The demonstrated patterns were pre-programmed in Java.
In summary, the Rainbow HAT can be extended to provide users additional functions without removing the HAT from the Raspberry Pi. An app, called the IoT Workbench, can be used to control both the basic and the extended parts of the board. What I didn’t show above is that the app can choose to control any number of LED lights available on the strip. This flexibility makes it possible for the app to work with LED strips of any length.