Home Code Micro:bit RGB led examples in Micropython

Micro:bit RGB led examples in Micropython

by shedboy71

Tri-color LEDs contain three different LED emitters in one case. Each emitter is connected to a separate lead so they can be controlled independently. A four-lead arrangement is typical with one common lead (anode or cathode) and an additional lead for each color. Others, however, have only two leads (positive and negative) and have a built-in electronic controller.

I used an RGB LED module, I find this is easier than using a breadboard with an RGB led and required resistors

Table of Contents

Parts List

Name Link
Micro:bit Micro:bit Development Board
RGB Board Full Color LED Module 10mm Bright RGB Board Electronic Building Blocks For Arduino
Edge Breakout I/O Expansion Edge Breakout I/O Expansion Extension Board for BBC micro:bit
Connecting cables Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Layout

 

microbit and rgb led example microbit and rgb led example

Code

The first example just shows some extra colour combinations

[codesyntax lang=”python”]

from microbit import *

while True:  
    pin0.write_digital(0)
    pin1.write_digital(1)
    pin2.write_digital(1)
    sleep(500)
    pin0.write_digital(1)
    pin1.write_digital(0)
    pin2.write_digital(1)
    sleep(500)
    pin0.write_digital(1)
    pin1.write_digital(1)
    pin2.write_digital(0)
    sleep(500)
    pin0.write_digital(0)
    pin1.write_digital(0)
    pin2.write_digital(1)
    sleep(500)
    pin0.write_digital(1)
    pin1.write_digital(0)
    pin2.write_digital(0)
    sleep(500)
    pin0.write_digital(0)
    pin1.write_digital(1)
    pin2.write_digital(0)
    sleep(500)
    pin0.write_digital(0)
    pin1.write_digital(0)
    pin2.write_digital(0)
    sleep(500)

[/codesyntax]

The next example displays a colour depending on whether button a, button b or no button is pressed

[codesyntax lang=”python”]

from microbit import *
while True:
    if button_a.is_pressed():
        pin0.write_digital(0)
        pin1.write_digital(1)
        pin2.write_digital(1)
    elif button_b.is_pressed():
        pin0.write_digital(1)
        pin1.write_digital(0)
        pin2.write_digital(1)
    else:
        pin0.write_digital(1)
        pin1.write_digital(1)
        pin2.write_digital(0)

[/codesyntax]

You may also like

Leave a Comment