RGB LEDs
RGB LEDs can emit three colors: red, green, and blue. They have four pins, one for each color; the remaining pin acts as a common cathode or common anode. The following circuit diagram shows the common cathode LED connection to the Micro:bit:
Figure 6.18 – An RGB LED (common cathode)
In a common cathode LED, we must apply a positive voltage to the color pins to activate the corresponding LEDs. We have connected the common cathode to the ground, the red pin to pin 0, the green pin to pin 1, and the blue pin to pin 2. The program that cycles through all the colors that this combination of digital outputs can produce is quite simple and intuitive, as follows:
from microbit import * red = pin0 green = pin1 blue = pin2 def glow(red_val, green_val, blue_val): red.write_digital(red_val) green.write_digital(green_val) blue.write_digital(blue_val) try: ...