
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;
}
}
}