Extending the Rainbow HAT for the Raspberry Pi with More PeripheralsCharles XieBlockedUnblockFollowFollowingFeb 27The Rainbow HAT for the Raspberry Pi comes with a “buffet” (in the manufacturer’s own words) of sensors and actuators that we can use to learn IoT programming.
If you are tired of eating the same buffet every day, you can connect your own peripherals to its open GPIO pins — PWM0, UART0, I2C1, and SPI0.
1 — to create more interesting projects.
The pins can be found on the left side of the board.
In this article, I will show how to connect a strip of 72 RGB LED lights through the SPI pins as an example of extending the functionality of the Rainbow HAT.
As a demo, I used this one from Adafruit, but any APA102 LED strip should work as well.
The half-meter-long Adafruit DotStar Digit LED Strip has a 4-pin JST SM male connector on one end (unlike other types of LED strip that also provide a female connector on the other end for assembling arbitrarily long cables).
Out of the four pins, one is supposed to connect to a power source (5V), one to the ground (GND), one to the clock (SCLK), and the last one to the data (MOSI).
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).
It is important to use a separate source to power the LED strip.
I use a 12 V power adapter and a YwRobot module to power my breadboard and then use 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.
1 mm plug and an ON/OFF switch that can be easily connected to and disconnected from the YwRobot module.
The advantage of the battery option is that you don’t have to be constrained to an electric outlet and can move your LED strips anywhere once you finish the project, provided that you also have a portable source to power your Raspberry Pi.
The last thing to remember is to connect the ground of the power source and a ground pin of the Raspberry Pi.
Otherwise, you won’t be able to set the correct state for your strip.
Figure 1 shows my setup.
1: My setup of an extended Rainbow HATAfter 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.
To be able to do so, we need to understand the APA102 serial LED data protocol for controlling the LED strip.
Figure 2 shows the APA102 scheme, which is actually quite simple.
Be sure to remember to start each data packet you send to the SPI interface with a start frame and end it with an end frame as suggested in Figure 2.
You can control the brightness of each LED light using the last five bits of the first data byte (marked as “Global” in Figure 2).
Although there are 32 levels of brightness, I found the first level is 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.
2: The APA102 serial LED data protocolOne interesting thing to do with a LED strip is to show a rainbow across it.
This can be easily done by using the HSV color scheme — all we need to do is to assign HSV(0, 1, 1) to the LED at one end of the strip and HSV(1, 1, 1) to the LED on the other end.
Any LED in the middle gets the interpolated HSV color.
Figure 3 shows the result of the rainbow coloring.
3: The rainbow pattern on a LED strip.
Figure 4 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 surely looks better than a tedious gradient color bar inside the screen, which does not glow.
4: Create a rainbow bar for a computer monitor.
To control the peripherals, I have started to develop a Java program for the Raspberry Pi called the IoT Workbench, which will eventually support the design and development of simple IoT apps.
Figure 5 shows the Rainbow HAT emulator in the IoT Workbench.
The user can click the buttons or LED lights in the emulator and interact with them.
The actual actuators will react to the user’s actions.
5: 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 6 and 7 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.
6: The temperature graph.
7: The pressure graph.
There is also an easy way to export the data collected by the sensors of the Rainbow HAT, as is shown in Figure 8.
8: 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 WorkbenchIn summary, the Rainbow HAT can be extended to provide users additional functions without removing the HAT from the Raspberry Pi.
An app, 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.