LEDs

It’s possible to use the external pins on the micro:bit to control external LEDs.

Electronics

Electric current can only flow one way through a light emitting diode (LED). It must flow through its anode (+) and out its cathode (-) to light.

The anode has a longer leg and often has a bend in it.

Single LED

A 220Ω resistor is added to reduce the voltage; without it the LED would blow.

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

Flash a Single LED

from microbit import *

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

Fade an LED

from microbit import *

fade_amount = 5   # How much to change brightness each iteration
speed = 10        # speed brightness changes
brightness = 0

while True:
    brightness = brightness + fade_amount
    if (brightness <= 0) or (brightness >= 1023):
        fade_amount = -fade_amount
    else:
        pin0.write_analog(brightness)
    sleep(speed)

Creates a lot of flickering in Python; not quite sure yet.

Flash a Single LED

Experiment

Share On
Facebook Twitter Google Plus

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

Acknowledgements: Teaser image by Adafruit (CC-BY).