RGB LEDs

An RGB LED is essentially a red, green and blue LED combined into one package.

RGB LED

An RGB LED has four legs: an anode to drive each of the three component colours and a common cathode. The longest pin is the common cathode (-) and is used by all other LEDs in the package.

Each of the colours—red, green and blue—can be made my supplying current to the shorter pins. Usually the order of the pins is the same as the diagram above; but occassionally they can change, and sometimes it’s even a common anode.

RGB LED

Any coloured light can be made by varying how bright the red, blue and green LEDs are lit. This can be done with pin0.write_analog(n) where n is a value between 0 (off) and 1023 (on full).

Electronics

220Ω resistors are placed before the each of the anode pins. 220Ω is a safe choice and will cover all RGB LEDs. You can consult the datasheet of the LED if you have it; you may not even need a resistor but it’s better safe than sorry!

RGB LED

Which Resistor to use with an LED?

A resistor is required to limit the power supplied to an LED. The LED could ‘blow’ if it supplied directly by the microbit. The resistor’s value can be calculated by using the typical forward voltage of the LED. This is usually on the data sheet for the component and looks similiar to this: “2V @ 20mA”.

With this is it is possible calculate the resistor required with Ohm’s Law (or a handy online LED calculator)

A rough, conservative estimate is good enough. These are the resistors required when a typical LED is driven by the microbit:

ColourResistor
IR100Ω 1/8W
Red68Ω 1/8W
Green56Ω 1/8W
White1Ω 1/8W
Blue1Ω 1/8W

Throughout these pages I’ve used 220Ω resistors because they’re ubiquitous and will cover pretty much every LED. The downside is that LEDs lit with 220Ω resistors will be slightly dimmer than they could be.

To get brighter LEDs, use the resistors mentioned above or be lazy and use 68Ω resistors for everything.

Code

Code to show a yellow light for two seconds then show a red light slowly turning magenta
from microbit import *

# assign colour names to each of the pins
red = pin0
blue = pin1
green = pin2

while True:

    # Make Yellow
    red.write_digital(1)
    green.write_digital(1)
    sleep(2000)			 # Show yellow for 2 seconds

    # Turn off Yellow
    red.write_digital(0)
    green.write_digital(0)

    # turn on red
    # slowly increment the intensity of blue to make magenta
    red.write_digital(1)
    for i in range(0,1023):
        blue.write_analog(i)
        sleep(10)

    # turn off pins before repeating
    red.write_digital(0)
    blue.write_digital(0)

Notes

Digital vs. Analog Write

A pin can output a digital signal (either on or off) or an analog signal (between 0 and 1023).

The analog output uses pulse width modulation where the current is quickly turned on and off. write_analog(1023) sends 3.3v to the pin; it is equivant of write_digital(1).

write_analog(511) the power is on exactly 50% of the time; or is at 50% duty. The total energy of the signal is 1.65v, or half of 3.3v.

There is a more detailed explaination of PWM on the micropython documentation.

Analog Write

pin0.write_analog(1024)

This MS Blocks & Python code does exactly the same thing: set pin0 to maximum (3.3v). A value of 512 would be 50% and so on. If lighting an LED, it would set the brightness to 50% of maximum.

Digital Write

pin0.write_digital(1)

Digital can either be on (1) or off (0). In this example, it turns pin0 on (1).

Experiment

Share On
Facebook Twitter Google Plus

Published: December 23, 2016 Updated: December 23, 2016

Acknowledgements: RGB LED teaser image by Oomlout (CC-BY-2.0). RGB spectrum image public domain.

RGB LEDs

Connect a RGB LED to make any colour you wish!

Jez Dean
Teacher