Make an RGB LED Blink with micro:bit


You might have already tried your hands at making a two colour LED module blink with the micro:bit, but have you tried using an RGB LED module? 

In this guide, you will learn to make an RGB LED blink and change to a variety of colours. 

Complete this guide to learn the basics of programming an RGB LED!

If you're using the Micro:bit Advent Calendar, you'll need the contents of the bags labelled 2 and 25.

Parts Used in This Guide


Step 1  The Module

Let's take a look a closer look at the RGB LED Module before we begin!

There are four pins:

  • RED (R)
  • GREEN (G)
  • BLUE (B)
  • GND: In electronics, we define a point in a circuit to be a kind of zero volts or 0V reference point, on which to base all other voltage measurements. This point is called ground or GND.
  • Note: Voltage is the difference in electric potential between two points. As it is difficult to talk about voltage without a reference point, we need another point to compare it to.

As you might guess, this module is capable of emitting red, green, blue as well as a variety of colours based on how they are combined. This module has on-board resistors, so external resistors are not required here. 

Let's start making the circuit!

Step 2  Connect module to breadboard

Step 3  Insert the Micro:bit into the Breakout board

Insert the micro:bit into the breakout board . Make sure that the micro:bit is inserted in the right orientation, with the buttons facing inward as shown.

Step 4  Connect P0 to R

Step 5  Connect P1 to G

Step 6  Connect P2 to B

Step 7  Connect GND to GND

Step 8  Start Makecode Editor

Now let's program it with the Makecode Editor

  1. Click on 'Projects' button 
  2. Click on 'New Project'

Step 9  The code

  1. Drag a "forever" block from the Basic section of the editor's panel
  2. Click on Pins button on the editor panel, drag a "digital write pin ... to ... " block
  3. Click on Basic button on editor panel and drag a "pause (ms) ... " block
  4. Change  "digital write pin ... to ..." block to "digital write pin P0 to 1"
  5. Change 'pause (ms) ... " block into "pause (ms) 500"
  6. Repeat steps 2 and 3
  7. Change  "digital write pin ... to ..." block to "digital write pin P0 to 0"
  8. Change 'pause (ms) ... " block into "pause (ms) 500"
  9. Repeat steps 2 and 3 again
  10. Change  "digital write pin ... to ..." block to "digital write pin P1 to 1"
  11. Change 'pause (ms) ... " block into "pause (ms) 500"
  12. Repeat steps 2 and 3 again
  13. Change  "digital write pin ... to ..." block to "digital write pin P1 to 0"
  14. Change 'pause (ms) ... " block into "pause (ms) 500"
  15. Repeat steps 2 and 3 once again
  16. By now you might have gotten the hang of it! Change it for P2 as shown in the diagram.

Step 10  Turn on more than one colour at a time!

Did you know? You can turn on more than one of the colours at the same time. When this happens, the overall colour of the LED will be a blend of the RGB LED colours. Let's try that out and find out what colours you get for each combination!

  1. First Pin 0 is turned on which lights up the LED in RED. 
  2. Then 500 milliseconds passes before Pin 1 is turned on, where the LED is now shining in RED+GREEN. After you upload the code to the micro:bit, you should see a bright green with a hint of red emitting from the LED.
  3. 500 milliseconds passes again. Then with Pin 0 still turned on, Pin 2 is turned on which has the LED shining RED+BLUE. After you upload the code to the micro:bit, you should see a bright purple colour emitting from the LED.

Give it a try, test out these combinations yourself:

  • RED + BLUE

Step 11  Upload the code to micro:bit

  1. Connect a microUSB cable to the micro USB port of the micro:bit.
  2. Then connect the other end of the cable to your computer or laptop's USB port.
  3. Click on the Download button on the bottom left-hand corner 
  4. Find the hex file in the Downloads folder or other folder that you might have moved it to
  5.  Open up Finder on the MacOS or Explorer on Windows, and drag the hex file into MICROBIT under 'Devices' on the macOS.
  6. Watch the micro:bit flash for a few seconds and watch the LED blink!