Using LEDs with micro:bit

Difficulty
Easy
Steps
9

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 external LEDs to the micro:bit, and create a light show.

Complete this guide to learn how to use external LEDs with the micro:bit!

Parts Used in This Guide

Buy

Step 1  The Parts

Before we begin, 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  Connect Cathode to GND

Step 3  Add a resistor

Step 4  Connect Anode to P2

Step 5  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 6  Using Multiple LEDs

To use more than one external LED, follow these steps!

We'll use multiple LEDs with the micro:bit to create a light show. So go on and add a four more LEDs as shown in the diagram.

We'll use a total of five LEDs to create a light show by programming the micro:bit using the MakeCode editor, and then with the Arduino IDE.

Each of the cathode (negatively charged lead) of the LEDs needs to be connected to the negative rail. 

Each of the anode (positively charged lead) of the LEDs need to be connected to P0 to P4, with the use of a resistor.

Remember to connect the negative rail to a GND pin.

Step 7  MakeCode for Light Show!

  • Get this code by clicking the download button below. 
  • Upload it to the micro:bit and you should see the LEDs lighting up one after another and then going off.
  • Note: Pins 3 and 4 GPIO are shared with the micro:bit's LED screen. To use Pins 3 and 4 for the light show, we will first disable the LED screen by using the 'led enable false' block on start

Step 8  Arduino Code for Light Show


              const int lowestPin = 0;//the lowest one attach to pin 0
const int highestPin = 4;//the highest one attach to pin 4


void setup()
{
  //set pins 0 through 4 as output
  for(int thisPin = lowestPin;thisPin <= highestPin;thisPin++)
  {
    pinMode(thisPin,OUTPUT); //initialize thisPin as an output
  }
}

void loop()
{
    //iterate over the pins
    //turn the led on from lowest to the highest
    for(int thisPin = lowestPin;thisPin <= highestPin;thisPin++)
    {
      digitalWrite(thisPin,HIGH);//turn this led on
      delay(1000);//wait for 1 second
    }
    //fade from the highest to the lowest
    for(int thisPin = highestPin;thisPin>=lowestPin;thisPin--)
    {
      digitalWrite(thisPin,LOW);//turn this led off
      delay(1000);//wait for 1 second
    }
  }
            

Optionally, you may want to use the micro:bit with the Arduino IDE.

  • Open the Arduino IDE. If you have not got it installed, please follow our previous guide on how to install and set up the Arduino IDE for micro:bit.  
  • It requires a few more steps than just installing the IDE, to get it working with the micro:bit, so please follow that guide! 
  • Copy and paste this code to the Arduino IDE 

Step 9  Upload the code

It's time to upload the code to the micro:bit. 

  • In the Arduino IDE, click on the tick icon on the top left-hand corner to verify the code. 
  • Then click on the upload button next to it. 
  • Wait for the code to finish uploading. You will know it is done when it states 'Done uploading' on the bottom left-hand corner.