Lesson 05 – RGB LED
An RGB LED is a type of LED. It can emit light with three different colors: red, green and blue. In this experiment, we are going to make an RGB LED gradually shift its light between the three different colors.
What you need
- micro:bit board
- Micro USB Cable
- micro:bit breadboard adaptor
- RGB LED
- 100 ohm resistors x3
- Male to male jumper wires
RGB LED is a type of LED, which has integrated red LED, green LED, and blue LED in a single package. We all know that the three primary colours of light are red, green, and blue. With different combinations of the three colours, we can create thousands of colour variations by mixing them together. Similarly, if we use RGB LED to combine colours with different brightness, then it can form various colours.
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.
Please complete hardware connection according to the breadboard diagram below:
After connecting the circuit, you should see something similar like in the below image:
Open Microsoft Makecode, and write the code in edit area. Try to 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
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 state.
A function is a section of code you can reuse in your program when you call it using its function name. You create a function using a function definition, which names the function and has its code inside. A function call is when you use a function by its name somewhere in your program, usually in the main area of your code.
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 realise gradual change among the three colors.
For example, “RtoG” function is for transformation of red light to green light. If we implement the circulation of the function 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). The results are gradual change from one LED colour to another, smoothly.
Loop the three functions to show colour gradual change, smoothly.
Press button A, LED turns red.
Press button B, LED turns green.
Press button A+B, LED turns blue.
Taking it further
If we want to use RGB LED to emit cyan light, magenta light, yellow light, then how would you design the circuit and program?
micro:bit Starter Kit Lessons
- Lesson 01 – LED
- Lesson 02 – Button
- Lesson 03 – Trimpot
- Lesson 04 – Photocell
- Lesson 05 – RGB LED
- Lesson 06 – Self-lock Switch
- Lesson 07 – Temperature Sensor
- Lesson 08 – Servo
- Lesson 09 – Buzzer
- Lesson 10 – Motor
- Lesson 11 – Rainbow LED
- Lesson 12 – Accelerometer
- Lesson 13 – Compass
- Lesson 14 – Ambient Light