BlocklyProp makes it very easy to control individual WS2812B RGB LEDs. These LED modules may be daisy-chained together by connecting one module’s serial-out pin to the next module’s serial-in pin. Each LED has 16 million different colors and it’s own “address” so you can identify which one to control in your BlocklyProp program. The difficult work to be done by the C code libraries used within BlocklyProp. The WS2812 RGB LEDs may be faded, dimmed, pulsed, and scrolled individually or as groups.
This project is separated into two parts that demonstrate different ways to use these LEDs with BlocklyProp. If you are interested in going further, there is a third part to this series (Rainbows and Robotics with WS2812 LED Strip Lighting [1]) that uses LED strips with Blockly and the S3.
WS2812 RGB LEDs were used with the Propeller multicore microcontroller in the Bud Light PAC-MANTM commercial, originally aired during Super Bowl XLIX, to light up full-size human “ghosts” on roller skates (click here to read our news story about it [2]). Each costume has several thousand LEDs to show when the ghost is in chase mode or ready to be eaten by PAC-MAN.
Common applications of single LEDs are to display various states of your program for human feedback, but the larger “rolls” of LED tape have pre-made connections to power, ground and data. You can use the Blockly system to control these systems, too.
Parallax’s WS2812 RGB LEDs are sold in 10-packs (#28086) [3] or individually (#28085) [4]. To keep your costs low, the 3-pin headers need to be soldered on after you receive them. Students are always interested in being able to solder and build; this process is an important and rewarding step in building the circuit. The soldering process is very easy, too.
The WS2812 RGB LED blocks are found in the COMMUNICATE menu and documented in the RGB LED BlocklyProp reference section [5]. Setting the blocks is simply a matter of initializing the communication pin and setting the number of LEDs. The sequence begins at number one.
The single biggest technical challenge with these LEDs is attempting to control an LED which isn’t present. The example circuit has three LEDs. If you specify four or more LEDs in the RGB LED init block the daisy-chained integrated circuits will fail to enumerate themselves and your LEDs will remain off regardless of the control signal you send.
Program your circuit with the sample program WS2812 Setting Color to set the three LEDs with red, white and blue. Experiment with different colors!
Immediately you’ll likely notice that these LEDs are simply too bright and that perhaps you'd like to apply some dimming. By choosing BlocklyProp’s bitwise block from the Numbers menu you can mix any color with another. Mix each color with a grey shade to dim the LED. This has another benefit - the current draw of each LED is much lower, enabling you to control more LEDs from your computer’s USB port which is limited to around 500 mA.
A modification of the above program is the BlocklyProp program Simple Color Control with Dimming [5] from the reference section.
Color pulsing is similar to dimming - but it’s more like a form of advanced brightness control. Instead of adding a single color (like grey, above), increasing the amount of the color from 0 to 255 drives the LED from off to full brightness. This can easily be done by using a repeat loop for each LED, one at a time. The WS2812 Color Pulsing code example demonstrates this capability; download it here:
Project4375-WS2812-Color-Pulsing.svg [6]
BlocklyProp has a Random number block where you can set the range of values. The WS2812 Random Colors code example demonstrates this feature.
Project4580-WS2812-Random-Colors.svg [7]
The ColorPal and WS2812 RGB LED blocks were both designed to combine all red, green and blue color values into a single 24-bit number. While the individual red, green and blue components may also be captured individually, it’s really easy to use these two blocks together with one variable named color.
Simply sense the color with the ColorPal, save it in a variable, and output it on the WS2812 RGB LEDs.
Color sorting has many applications beyond putting Skittles in the right cup like our Color Candy Sorter Project. Identifying fruit colors, color paint chip matching, robot navigation, and industrial automation are typical uses for sensors like the ColorPal.
Load the ColorPal Sensor and Color Display code below and try it out. This Blockly code is simple enough to explain itself, yet the omission of any block of code would cause it not to work. Test it with fruit, paper, a smartphone, and book covers by placing your color samples in front of the ColorPal sensor.
Project4378-WS2812-ColorPal-And-Display.svg [8]
Links
[1] http://learn.parallax.com/tutorials/language/blocklyprop/rainbows-and-robotics-ws2812-led-strip-lighting
[2] https://www.parallax.com/news/2015-01-29/multicore-propeller-used-real-life-pac-man-bud-light-super-bowl-xlix-commercial
[3] https://www.parallax.com/product/28086
[4] https://www.parallax.com/product/28085
[5] http://learn.parallax.com/support/reference/activity-board-wx-block-reference/communicate/rgb-leds
[6] https://learn.parallax.com/sites/default/files/content/AB-Blockly/Light-It-Up/Project4375-WS2812-Color-Pulsing.svg
[7] https://learn.parallax.com/sites/default/files/content/AB-Blockly/Light-It-Up/Project4580-WS2812-Random-Colors.svg
[8] https://learn.parallax.com/sites/default/files/content/AB-Blockly/Light-It-Up/Project4378-WS2812-ColorPal-And-Display.svg