Micropython Lesson 13
Use RGB LED to change colors as required
An RGB LED has 4 pins, one for each color (Red, Green, Blue) and a common cathode. It has three different color-emitting diodes that can be combined to create all sorts of color. R- Red G- Green B- Blue
- Using a RGB led and changing its color as the required
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. The name of the model comes from the initials of the three additive primary colors, red, green, and blue. The main purpose of the RGB color model is for the sensing, representation, and display of images in electronic systems, such as televisions and computers, though it has also been used in conventional photography. Before the electronic age, the RGB color model already had a solid theory behind it, based in human perception of colors
For this demonstration, you have to install Adafruit NeoPixel library. For more details Click here. As usually connect the RGB module to your Magicbit, for this, we take data pin as pin 32. After connect the RGB module to the Magicbit, connect it to your pc and upload following code.
from microbit import * from random import randint button_a=Pin(34, Pin.IN) button_b=Pin(35, Pin.IN) redVal = randint(0, 255) greenVal = 0 blueVal = 0 while True: if button_a.is_pressed(): redVal = 0 blueVal = 0 greenVal = randint(0, 255) pin0.write_analog(redVal) pin1.write_analog(greenVal) pin2.write_analog(blueVal) elif button_b.is_pressed(): redVal = 0 blueVal = randint(0, 255) greenVal = 0 pin0.write_analog(redVal) pin1.write_analog(greenVal) pin2.write_analog(blueVal) else: pin0.write_analog(redVal) pin1.write_analog(greenVal) pin2.write_analog(blueVal)
You should see your LED turn on red. If you press the A button on the micro:bit, the color will change to green, and if you press the B button, the color will change to blue.