Using the HD107S RGB Led with a BBC Microbit

The HD107S is a low cost RGB LED unit which features an SPI interface. I paid just over €9 for 50 of them. They are quite easy to program using a microcontroller platform such as the BBC Microbit. The short program below was written on MBed and downloaded to the Microbit. It causes the LED to cycle through a range of colours.

#include "mbed.h"

SPI spi(P0_21, P0_22, P0_23); // mosi, miso, sclk
void getRainbow(unsigned &Red, unsigned &Green, unsigned &Blue);

int main() {

    unsigned Red,Green,Blue;
    Red = Green = Blue = 0;    
    char tx_buffer[16];
    char rx_buffer[16];
    spi.format(8, 3);
    spi.frequency(1000000);
    tx_buffer[0]=0; // header
    tx_buffer[1]=0; // header
    tx_buffer[2]=0; // header
    tx_buffer[3]=0; // header
    tx_buffer[4]=0xe0 + 0x1f; // max brightness (1f = brightness figure - lower to suit)
    while(1) {
        getRainbow(Red,Green,Blue);
        tx_buffer[5]=Blue; // blue
        tx_buffer[6]=Green; // green
        tx_buffer[7]=Red; // red        
        spi.write(tx_buffer, 8, rx_buffer, 0);              
        wait(0.01);
        
        
    }
}
void getRainbow(unsigned &Red, unsigned &Green, unsigned &Blue)
{   // Cycle through the colours of the rainbow (non-uniform brightness however)
    // Inspired by : http://academe.co.uk/2012/04/arduino-cycling-through-colours-of-the-rainbow/    
    static int State = 0;
    switch (State)
    {
        case 0:{
            Green++;
            if (Green == 255)
                State = 1;
            break;
        }
        case 1:{
            Red++;
            if (Red == 255)
                State = 2;
            break;
        }
        case 2:{
            Blue++;
            if (Blue == 255)
                State = 3;          
            break;
        }
        case 3:{
            Green--;
            if (Green == 0)
                State = 4;
            break;
        }
        case 4:{
            Red--;
            if (Red == 0)
                State = 5;
            break;
        }
        case 5:{
            Blue --;
            if (Blue == 0)
                State = 0;
            break;
        }       
    }    
}

2 thoughts on “Using the HD107S RGB Led with a BBC Microbit

  1. Ken Wallace September 29, 2020 / 2:14 pm

    Just trying Mbed myself and it was easy to compile and download examples to several STM32 Nucleo boards I have. I liked the “bare metal” option they talk about but was never able to sort it out. Also, I didn’t see any intelligent completion clues (called intellisense, I think) when writing code which I use a lot with Keil uVision5. I read where the downloaded version does have it.

    Like

    • fduignan September 29, 2020 / 4:00 pm

      Hello again Ken. I’ve stuck with the online version of mbed which works well for students as there’s no need to install anything. For my own development work I still use a command line compiler, gdb and a text editor. I did try visual studio code and it was quite nice but a little brittle

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s