Coding Ireland STEM Report 2024 Have Your Say
Microbit Sensors & Circuits
Advanced
45 mins
145 points
What you need:
  • Computer/laptop
  • Microbit
  • RGB LED
  • Crocodile clips

Microbit RGB LED

Using the RGB LED Microbit accessory, make different colours and sequences.

1 - Create a new Microbit project

The RGB LED is a coluorful add-on to your Microbit . Connect it up with alligator clips and then use the three outputs of your Microbit  to control the red, green and blue channels to mix up any color of light you want.

To get started create a new Microbit project.

makecode.microbit.org


2 - Create three functions

The RGB LED board has three channels, a red, a green and a blue channel. We can control the LED by setting a value for each of these channels and 'mix' a colour. We do this by using the analog write pin block.

  • Pin 0 (P0) is the red channel
  • Pin 1 (P1) is the green channel
  • Pin 2 (P2) is the blue channel

To start off let's code the LED to show 3 different colours. Create the following three functions (in the Advanced toolbox).

  1. Red
  2. Orange
  3. Green
function red () {
    pins.analogWritePin(AnalogPin.P0, 1023)
    pins.analogWritePin(AnalogPin.P1, 0)
    pins.analogWritePin(AnalogPin.P2, 0)
}
function orange () {
    pins.analogWritePin(AnalogPin.P0, 800)
    pins.analogWritePin(AnalogPin.P1, 100)
    pins.analogWritePin(AnalogPin.P2, 0)
}
function green () {
    pins.analogWritePin(AnalogPin.P0, 0)
    pins.analogWritePin(AnalogPin.P1, 1023)
    pins.analogWritePin(AnalogPin.P2, 0)
}

3 - Call the functions

Now call the functions by putting them in the 'forever' block and space them out using 'pause' blocks. This sequence imitates a traffic light.

basic.forever(function () {
    red()
    basic.pause(2000)
    orange()
    basic.pause(2000)
    green()
    basic.pause(5000)
})

function red () {
    pins.analogWritePin(AnalogPin.P0, 1023)
    pins.analogWritePin(AnalogPin.P1, 0)
    pins.analogWritePin(AnalogPin.P2, 0)
}
function orange () {
    pins.analogWritePin(AnalogPin.P0, 800)
    pins.analogWritePin(AnalogPin.P1, 100)
    pins.analogWritePin(AnalogPin.P2, 0)
}
function green () {
    pins.analogWritePin(AnalogPin.P0, 0)
    pins.analogWritePin(AnalogPin.P1, 1023)
    pins.analogWritePin(AnalogPin.P2, 0)
}


4 - Download the project and send it to your microbit

5 - Connect the RGB light

Using 4 crocodile clips, connect your microbit to the RGB light as shown in this diagram. It doesn't matter which colour crocodile clip you use for each connection as they are the same apart from the colours.

MicrobitRGB LED Board
0Red
1Green
2Blue
GNDGND

When you power your Microbit, the RGB light should light up and follow your sequence.



Join our club 😃

To view the remaining 1 steps and access hundreds of other coding projects please login or create an account.

Copyright Notice
This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.
🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more