edit

Lesson 05 RGB LED

5

Introduction


RGB LED is a kind of LED. It can emit light with three different colors: red, green and blue. In this experiment, we are trying to make RGB LED gradually shift its light among the three different colors.

Component List


Hardware:

Tips: If you want to buy all components above, you may need Elecfreaks Micro:bit Starter Kit.

Software:

Microsoft Makecode Online Editor

Major Component Introduction


RGB LED

RGB LED is a kind of LED, which has integrated red LED, green LED, and blue LED into a component. We all knows that the three primary colors of light are red, green, and blue. With different groups of the three color lights, we can create all colors of the world. Similarly, if we use RGB LED to group lights with different brightness, then it can form various colors.


RGB LED can be divided into 2 types: common anode and common cathode. In common-cathode RGB LED, its common port usually connects GND, while in common-anode RGB LED, its common port connects VCC. In this experiment, we choose common-cathode RGB LED.

Hardware Connection


Please complete hardware connection according to the picture below.

After connection, you can see:

Programming


Open Microsoft Makecode, write code in edit area. I would like to suggest you program by yourself first.

Of course, if you want to see the whole program directly, you can click the link below. Just click “Edit”on the right top corner of the interface you opened, then click “Download” on the right bottom to download code into micro:bit directly.

Link of the whole program: https://makecode.microbit.org/_dMgdWubkTVaM

Code Explain


Change Value

Set the value for local and global variables. change r by 1 equals “r=r+1”.

repeat

Run part of the program the number of times you say.

function

A function is some amount of code you can reuse in your program. You create a function using a function definition which names the function and has its code. A function call is when you use a function by its name somewhere in your program.

call function

Use a function by its name somewhere in a program.

In this experiment, we have set three variables: r、g 、b, which is used to record brightness value of red light, green light, and blue light separately. At the initial status, only red light will be illuminated.

Then we have to create 3 functions to realize gradual change among the three colors.
For example, “RtoG” function is for realizing transformation of red light and green light. If we implement the circulation of the function for once, then P0 analog input “-1”( red light brightness decreased a little bit) , and P1 analog input “+1” ( green light brightness increased a little bit). After implement circulation for 1023 times, P0 analog input become “0” (red light turned off), and P1 analog input becomes “1023”(green light illuminated). Finally, it has realized gradual change smoothly.

Circulate the three functions to realize color gradual change smoothly.

Experiment Result


Press button A, LED turns red.
Press button B, LED turns green.
Press button A+B, LED turns blue.

Think


If we want to use RGB LED to emit cyan light, magenta light, yellow light, then how to design circuit and program? We look forward to your comments or further discussions with us.

Relative Readings


FAQ