Using LEDs with micro:bit

Difficulty
Easy
Steps
17

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 light-dependent resistor module to the micro:bit, and program it to turn an LED on when it get dark enough! In the second part of this guide, you will create a light show by using multiple external LEDs!

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 closer look at the light-dependent resistor module. It has four pins:

  • Voltage Common Collector (VCC)
  • Ground (GND)
  • Digital Output (DO)
  • Analog Output (AO)

Now let's take a look at an 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.

Step 2  Connect module to breadboard

Step 3  Connect 3.3V to VCC

Step 4  Connect GND to GND

Step 5  Connect P5 to DO

Step 6  Connect P1 to AO

Step 7  Connect GND to LED (Negative lead)

Step 8  Add a resistor

Step 9  Connect P2 to resistor

Step 10  Create the variables!

Open up MakeCode editor and start a new project.

If it is dark, we will get the LED to turn on.

  • First, create two variables: 'sensorVal' and 'threshold'.
  • As the AO pin of the light-dependent resistor is connected to Pin 1 of the micro:bit, we set sensorVal (short for sensor value) to analog read pin P1.
    The third variable, "threshold" is set to 500. 
  • Next, add a 'show number' block and change its value to 'sensorVal'. Upload this code now, and test it. What values show across the screen of your micro:bit's 5X5 LED matrix?

Step 11  The loop!

  • The next step is to create an 'if... then ... else if ... then' loop. So drag and drop that block from the 'Logic' tab into the editor.
    • To add an 'else if' block, click on the white plus (with a circular border) icon twice.
    • Delete the extra 'else' block by clicking on the white minus (with a circular border) icon next to it.
  • We will set two conditions here. 
    • If sensorVal > threshold, then turn the LED on by using a 'digital write pin P2 to 1' block. 
    • The second condition is 'sensorVal <= threshold', then we turn the LED off by using a 'digital write pin P2 to 0' block

Step 12  Use two forever loops!

 If you uploaded the previous MakeCode to the micro:bit, you will notice that the LED will not light up until the number has been displayed. To get around this issue, we can use two forever loops. 

  • Place 'show number sensorVal' in this other forever loop. 
  • You can also move the two 'set sensorVal ..' and 'set threshold ...' blocks over.
  • After uploading this code to the micro:bit, there will no longer be a pause between the LED lighting up and the number being displayed.

Step 13  Upload the hex file

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

  1. Connect your computer to the micro:bit by using a microUSB cable
  2. Click on the 'Download' button on the bottom left corner of the MakeCode editor
  3. The hex file will be downloaded to your 'Downloads' folder. So open up Finder on a Mac OSX, or in Explorer on Windows and go to your Downloads folder.
  4. Drag and drop the downloaded hex file to the 'MICRO:BIT' drive

You may want to adjust the threshold level to your liking. Then, test it by covering the light-dependent resistor with your hands. The LED will light up!

Step 14  Use Multiple LEDs

In the next part of this guide, we'll use a few more LEDs in the Day 18 bag! 

We'll use five LEDs to create a light show by programming the micro:bit using 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 15  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 16  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
    }
  }
            
  • 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 17  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.