6. case 06 Rainbow LED#

6.1. Our Goal#

  • Use LEDs on robit to make dazzling colorful light.

6.2. Material Needed#

  • 1 x Robit

  • 1 x Mbot Car

6.3. Background Knowledge#

Rainbow LED#

RGB color model is a color standard in the industry. It can be used to create a variety of colors through the change of RGB channels and their interactive mixture. RGB stands for red, green and blue. This standard has almost included all kinds of colors that are visually perspective by human eyes. It is one of the most widely used color system.

Rainbow LED is a kind of LED. It has integrated red, green and blue LED into one component. We all know that the three primary colors of light are red, green and blue. With different color combination, we can synthesize the color of all things. Similarly, by combining different RGB brightness of light, we can also create an infinite number of colors.

The LEDs on Robit are WS2812b LEDs, which have similar outlook with 5050 bead. Each bead is a pixel. The picture below is a single bead that has four pins.


6.4. Hardware Connection#

Two rainbow LEDs onboard are connected to P12 on micro:bit.



6.5. Software#

Microsoft Makecode

6.6. Programming#

Step 1#

Click Advanced in the code drawer of MakeCode to see more code options.


To program for robit, we have to add a package. Find Add Package in the bottom of code drawer and click it. This will pop up a dialogue box. Search Robit and then click to download this package.


Note: If you get a hint that some packages will be deleted due to the problem of incompatibility, you can either follow the prompts, or create a new project in the project menu.

Step 2#

When start, we have to create a variable item to assign values to LEDs. Let’s define its initial color to be 0.

Then create a forever loop and make variable item increase by 5 during each circulation. If the value of item equals to 360, then set it to be 0 to restart loop.


Note: Picture below is the detailed code explain of LED.


6.7. Program#

You can see the whole program from the link here: https://makecode.microbit.org/_DLqKtp38EVro

Or you can download it from the page below.

6.8. Result#

We can see the color of LED light is changing circularly.


6.9. Think#

How to make the color change swiftly instead of smooth change?

6.10. FAQ#

Q: How to change RGB flash speed?

A: Renew the change rate of the variable item.