Using an LED with micro:bit
A light-emitting diode (LED) is a semiconductor device that emits light when an electric current passes through it.
In this guide, you will learn to connect a single external LED to the micro:bit and program it in Makecode.
Complete this guide to learn how to use an external LED with the micro:bit!
Step 1 The LED
Let's take a look at the external LED.
- The LED has two leads, a negative lead and a positive lead.
- The longer leg is the positive lead, also called the anode.
- The shorter leg is the negative lead, also called the cathode. This will be connected to GND.
If the legs are trimmed, you can also tell by looking at the flat edge on the LED's outer casing. The pin closest to the flat edge will be the negative pin, the cathode pin.
Step 2 The breadboard
- In this guide, we'll be using a 400-tie breadboard. These breadboards have conductive material that connects the holes (known as points) inside the breadboard.
- The breadboard points are connected to each other vertically, with a trough in the middle that enables more prototyping area.
- The conductive material (and electrically joined points) are shown in this diagram in gold.
- The power rails are labelled '+' and '-'. The holes on the power rail are to each other horizontally.
- The power rails are used to connect to '3.3V' and 'GND' on the micro:bit.
Step 3 Connect Cathode to GND
Step 4 Add a resistor
Step 5 Connect Anode to P2
Step 6 MakeCode!
Programming the external LED is similar to how it was done for the Two Colour LED module. We'll use 'digital write pin ... to ... ' blocks to turn the LED on and off.
- Add two 'digital write pin ... to ... ' blocks
- Set it as P2. The LED is connected to Pin 2 on the micro:bit.
- Add two 'pause (ms)' blocks with a value of 500 for a pause of half a second between the LED turning on and off.
- Rearrange as shown.
- Upload this Makecode to the micro:bit!
Step 7 Using the LED with a different pin!
Some pins share function with on-board components like the pushbuttons or LED display. To use them with the LED, the LED display will first need to be disabled.
For instance, by default, Pin 3 is used to control the first column of the micro:bit's LED display.
To use it with the LED, click on the 'Led' tab and add a 'led enable false' block to the 'on start' block.
Note: For more information on what each pin does, check out our guide, 'Meet the micro:bit'.
Step 8 Upload the code to micro:bit
To upload the code to the micro:bit, follow these steps:
- Click the download button in the bottom left corner of the screen.
- This will download a hex file (computer program file) which the micro:bit can read.
- Next, plug in your micro:bit, this will reveal a USB on your computer.
- Copy across the downloaded file to the micro:bit by dragging and dropping it to the MICRO:BIT drive
- The micro:bit will flash as the code is being uploaded. Once done, unplug the micro:bit.