Connecting an LED to the micro:bit
A Light Emitting Diode(LED) is an electronic component that lights up when a current is passed through it. They are either single colour or Red, Green and Blue (RGB) that when combined can produce a wide range of colours. It’s easy to connect LEDs to the micro:bit either using crocodile/alligator leads or a breadboard, but to avoid any damage to your micro:bit or the LEDs it’s important to understand a little bit about the electronic workings of theses devices before you start.
LEDs are diodes that emit light, and as diodes they only allow current to flow in one direction, so it’s important to connect them correctly. They should have a long (+) wire (often called a leg) and a shorter (-) wire.
The micro:bit can switch the LED on/off by providing power from one of its pins eg Pin0 using a digital write function.
The reference documentation of the individual LED you are using will list it’s current rating. It is often around 20mA and providing any more than this will likely burn out the LED.
The micro:bit has an on board budget of 90mA to provide to peripherals. Exceeding this could damage the device. Each pin of the micro:bit also has a current supply limit of 5mA and a maximum of 3 pins can be configured in this high drive mode at one time.
So to prevent either of these outcomes, we can introduce a current limiting resistor to our circuit. For our example, we will use a 1kOhm resistor. You can likely use a lower value resistor than 1kOhm, but this value is a good start and easy to remember. See this article on choosing a resistor to match your LED for more information.
The LED can be driven by connecting Pin0 to the resistor and then the + leg of the LED and connecting the - leg to GND.
You can connect the components with Alligator clips or by using a breadboard.
Using a digital write, we can change the status of the LED from off(0) to on (1).
In this program we are setting the pin to 1 on button A pressed and 0 on button B pressed.
from microbit import * while True: if button_a.was_pressed(): pin0.write_digital(1) elif button_b.was_pressed: pin0.write_digital(0) sleep(20)
It is also possible to purchase LEDs with inbuilt current limiting resistors if you want to keep the circuit as simple as possible.