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 colors. R- Red G- Green B- Blue.
- Using an 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 on human perception of colors.
For this demonstration, you have to install the 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 connecting the RGB module to the Magicbit, connect it to your pc and upload the 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 red. If you press the A button on the Magicbit, the color will change to green, and if you press the B button, the color will change to blue.