Getting Started with MicroPython and the SparkFun Inventor's Kit for micro:bit
Experiment 4: Driving an RGB LED
You know what’s even more fun than a blinking LED? Changing colors with one LED! In this circuit you’ll learn how to use an RGB LED to create unique color combinations. Depending on how bright each diode is, nearly any color is possible!
You will need the following parts:
- 1x Breadboard
- 1x micro:bit
- 1X micro:bit Breakout with Headers
- 1x Common Cathode RGB LED
- 3x 100Ω Resistors
- 5x Jumper Wires
Didn't Get the SIK for micro:bit?
If you are conducting this experiment and didn't get the Inventor's Kit, we suggest using these parts:
Introducing the Red/Green/Blue (RGB) LED
The Red/Green/Blue (RGB) LED is three LEDs in one. The RGB has four pins with each of the three shorter pins controlling an individual color: red, green or blue. The longer pin of the RGB is the common ground pin. You can create a custom-colored LED by turning different colors on and off to combine them. For example, if you turn on the red pin and green pin, the RGB will light up as yellow.
But which pin is which color? Pick up the RGB so that the longest pin (common ground) is aligned to the left as shown in the graphic below. The pins are Red, Ground, Green and Blue --- starting from the far left.
Ready to start hooking everything up? Check out the wiring diagram and hookup table below to see how everything is connected.
|Polarized Components||Pay special attention to the component’s markings indicating how to place it on the breadboard. Polarized components can only be connected to a circuit in one direction.|
Wiring Diagram for the Experiment
Run Your Script
language:python # SparkFun Electronics # Experiment 4.0 # Driving an RGB LED from microbit import * from random import randint 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)
Code to Note
Using the random module
MicroPython comes with a
random module to introduce a little unknown into your programs. There are a few ways you can use the random module beyond using it as a random number generator the way it is used in this experiment. To see the documentation, read the official docs on the random module.
In this experiment we are using random to generate a random number between 0 and 255
button_a.is_pressed() and button_b.is_pressed()
button_b represent the left and right buttons on the micro:bit respectively. There are three built-in functions that can be attached to
is_pressed()--- Will return
Trueif the button specified is pressed. If the specified button is not pressed, it will return
was_pressed()--- Will return
Falsedepending on whether the specified button was pressed since start-up or since the last time this statement (method) was called.
get_pressed()--- Will return the number of times the specified button has been pressed since the device started or since the last time the statement (method) was used. Once the method is used, it will reset to zero.
elif statement lets you check for multiple expressions that might be
True. In this experiment we want to do different things whether button A pressed is
True or button B pressed is
What You Should See
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.
LED Remains Dark or Shows Incorrect Color
With the four pins of the LED so close together, it’s sometimes easy to misplace one. Double check that each pin is where it should be.
The red diode within the RGB LED may be a bit brighter than the other two. To make your colors more balanced, use a higher ohm resistor.