2.3 Colorful Balls

In this project, we will make the RGB LEDs display different colors.

Clicking on different colored balls on the stage area will cause the RGB LED to light up in different colors.


You Will Learn

  • The principle of RGB LED

  • Copy sprites and select different costumes

  • Three primary colors superimposed

Build the Circuit

An RGB LED packages three LEDs of red, green, and blue into a transparent or semitransparent plastic shell. It can display various colors by changing the input voltage of the three pins and superimpose them, which, according to statistics, can create 16,777,216 different colors.

../_images/4_rgb.png ../_images/rgb_circuit.png


1. Select sprite

Delete the default sprite, then choose the Ball sprite.


And duplicate it 5 times.


Choose different costumes for these 5 Ball sprites and move them to the corresponding positions.


Ball3 sprite costume color needs to be manually changed to red.


2. Make RGB LEDs light up in the appropriate color

Before understanding the code, we need to understand the RGB color model.

The RGB color model is an additive color model in which red, green, and blue light are added together in various ways to reproduce a broad array of colors.

Additive color mixing: adding red to green yields yellow; adding green to blue yields cyan; adding blue to red yields magenta; adding all three primary colors together yields white.


So the code to make the RGB LED light yellow is as follows.


When the Ball sprite (yellow ball) is clicked, we set pin 11 high (red LED on), pin 10 high (green LED on) and pin 9 low (blue LED off) so that the RGB LED will light yellow.

You can write codes to other sprites in the same way to make the RGB LEDs light up in the corresponding colors.

3. Ball2 sprite (light blue)


4. Ball3 sprite (red)


5. Ball4 sprite (green)


6. Ball5 sprite (purple)