LED (Red/Green)

Overview

Age Range: First time using external devices for the micro:bit

In this lesson students will learn how to program the micro:bit to control an external bi colour LED. Bi colour means the LED has 2 colour options that be programmed. In this case red and green. 

The lesson activity will build up from turning on the LED to using both colour options available.

Aims

  • Learn how to program an external component using the micro:bit.

Download the Code

MakeCode Editor

Click the button below to download the MakeCode editor code for this lesson. The hex file is contained in a zip folder.

Kindly refer to our download the code page for instructions on how to upload your code onto the micro:bit. Please use instructions set A.

This code can also be uploaded into the MakeCode editor.

Activity 1

Activity 2

Kit Required

Micro:bit

Mini Innovation Board

LED (Red/Green)

- MicroUSB Cable
- 1 x Connector Cable

Preparation

  1. Connect the micro:bit to the Mini Innovation board via the edge connector.
  2. Use the connector cable to connect the LED to the ADC1 connector on the Mini Innovation board.
  3. Connect the micro:bit to a computer via the microUSB cable.
  4. Go to the MakeCode website on your computer and open a new project.

Time to Code

As you go through these steps please note that there are images of the completed code to the right.

Activity 1

  1. To begin, students will simply be turning one of the LED colours on.
  2. Select the ‘forever’ block from the ‘basic’ tab. On new projects this may already be on the screen.
  3. Next choose the ‘digital write pin’ from the ‘pins’ tab. The ‘pins’ can be found in the ‘advanced’ section. Insert this block into the ‘forever’ block.
  4. The ADC1 connector on the Mini Innovation Board connects to P0 and P3 on the micro:bit. These can be selected from the dropdown in the ‘digital write pin’ block. For this exercise select P0 from the dropdown. This may already be chosen.
  5. A digital signal is either on(high) or off(low). This is represented by 0 and 1. 0 being off(low) and 1 being on(high). Change the state on the ‘digital write pin’ to 1. This turn the LED on.
  6. The code is now finished! Download the code and upload it onto the micro:bit. The instructions for this can be found on the download the code pagePlease use instructions set B.

Activity 2

  1. This activity will build on activity 1 where one LED colour was turned on. This time students will alternate between the colour options having a different colour turned on for 2 seconds at a time.
  2. Begin by selecting the ‘on start’ block from the ‘basic’ tab and the ‘on button pressed’ block from the ‘input’ tab. Choose button A from the ‘on button pressed’ dropdown.
  3. Next, select the ‘led enable’ block from the ‘LED’ tab and insert it into the ‘on start’ block. This block disables the LED matrix on the micro:bit. The reason for this, is when P3 is used it interferes with the LED matrix. Students will use P3 in this activity.
  4. Select the ‘digital write pin’ block from the ‘pins’ tab and insert it into the ‘on button pressed’ block. Select P0 from the dropdown and set the state to 1. Select another ‘digital write pin’ block and attach to the bottom of the previous block. Choose P3 and set the state to 0.
  5. Select the ‘pause’ block from the ‘basic’ tab and attach to the bottom of the ‘digital write pin’ block. Set the pause time to 2000 ms (2 seconds). 
  6. So far the the code will turn on one of the LED colours when button is pressed.
  7. Next, students need to duplicate the 3 blocks within the ‘on button pressed’ block 3 more times. Attach these into the ‘on button pressed’ block as seen in the code image. 
  8. To turn on the other colour on the LED set P0 state to 0 and P3 state to 1. To mix the colours set the state of both P0 and P3 to 1.
  9. Finally students need to turn the LED off. To do this set states of P0 and P3 to 0.
  10. The code is now finished! Download the code and upload it onto the micro:bit. The instructions for this can be found on the download the code pagePlease use instructions set B.

What Will Happen

When button is pressed one of the LED colours will turn on for 2 seconds. Next, the other colour will turn on for 2 seconds. After that the colours are turned on and mix to create another colour. This will happen for 2 seconds. Finally the LED will turn off and wait for button A to be pressed again.

 

Taking it Further

  • Create a light sequence using the colour options available on the LED.
  • Use the skills learned to help complete the more advanced control lessons.

Keep in Touch

We would love to keep in touch with you and as a result aid you along your digital making journey.

Becoming a digital maker subscriber means you will receive exclusive offers and we will keep you up to date with new resources and kits, as well as upcoming events and workshops.

Let us know how your digital education journey is progressing. Send us pictures and videos of your awesome creations and use the hashtag below.

#digitaleducationjourney