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.

Fig. 1: An animated glowing rainbow in pitch darkness

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.

Fig. 2: The added LED strip is controlled by the same master of the 7 LEDs on the HAT

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).

Fig. 3: My setup of an extended Rainbow HAT using the power from Raspberry Pi

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.

Fig. 4: My setup of an extended Rainbow HAT using an external power source

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.

Fig. 5: The APA102 serial LED data protocol (image from manufacturer’s specs)

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.

Fig. 6: The rainbow pattern on a LED strip.

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.

Fig. 7: Create a rainbow bar for a computer monitor.

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.

Fig. 8: Two one-meter LED strips that lighten my table

The particular setup — with one strip above the other — allowed me to create visual loops like the one shown in the video below.

Video: Two linked LED strips form a loop.

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.

Fig. 9: The Rainbow HAT emulator.

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.

Fig. 10: The temperature graph.
Fig. 11: The pressure graph.

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.

Fig. 12: Export the data collected by a sensor.

The following video shows the IoT Workbench in action with the extended Rainbow HAT. The demonstrated patterns were pre-programmed in Java.

Video: The extended Rainbow HAT and the IoT Workbench

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.

Computational Scientist, Physicist, & Inventor at the Institute for Future Intelligence https://intofuture.org

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store