Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

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.

../_images/4_color.png

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

Programming¶

1. Select sprite

Delete the default sprite, then choose the Ball sprite.

../_images/4_ball.png

And duplicate it 5 times.

../_images/4_duplicate_ball.png

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

Note

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

../_images/4_rgb1.png

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.

../_images/4_rgb_addition.png

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

../_images/4_yellow.png

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)

../_images/4_blue.png

4. Ball3 sprite (red)

../_images/4_red.png

5. Ball4 sprite (green)

../_images/4_green.png

6. Ball5 sprite (purple)

../_images/4_purple.png