Introduction: Micro:bit Experiment 05: RGB LED —— Elecfreaks Mirco: Bit Starter Kit Course
RGB LED is a kind of LED. It can emit light with three different colors: red, green, blue. In this experiment, we are trying to make RGB LED gradually shift its light among the three different colors.
Step 1: Component List
1 x Micro:bit Board
1 x Micro-B USB Cable
1 x RGB LED
3 x 100 Ohm Resistors
Tips: If you want to buy all components above, you may need Elecfreaks Micro:bit Starter Kit.
Microsoft Makecode Online Editor
Step 2: Major Component Introduction
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 .
Step 3: Hardware Connection
Please complete hardware connection according to the picture below.
After connection, you can see:
Step 4: 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.
Step 5: Code Explanation
Set the value for local and global variables. change r by 1 equals "r=r+1".
Run part of the program the number of times you say.
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.
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.
Step 6: Experiment Result
RGB LED light will shift among the three colors: red, green, and blue smoothly in balance.
Step 7: Question:
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.
Step 8: Relative Readings:
We have a be nice policy.
Please be positive and constructive.