The PL9823 is a smart LED much like the WS2812B. There are some slight timing differences and they can sometimes be found cheaper than an equivalent WS2812B. I got hold of a few from Aliexpress in a frosted 8mm package with 4 pins that allow them to be used in a breadboard.
In a previous post I outlined how SPI could be used with an STM32F042 to drive a WS2182b. Things were a little different in this case as the STM32L031 has fewer options with the SPI clock and the slightly different timing requirements of the PL9823.
The image above shows the control from the MOSI signal for a nearly white colour on the LED. The signal is shows a 12 byte sequence which is interpreted as a 3 byte RGB sequence by the PL9823. A PL9823 logic ‘1’ is sent by sending 3 SPI ‘1’s followed by a zero. This takes 2 microseconds. A logic ‘0’ is sent by sending a single SPI ‘1’ and 3 SPI ‘0’s. The sequence is generated using the following function
void writePL9823(uint32_t Colour) { // each colour bit should map to 4 SPI bits. // Format of Colour (bytes) 00RRGGBB uint8_t SPI_Output[12]; int SrcIndex = 0; int DestIndex = 0; for (DestIndex = 0; DestIndex < 12; DestIndex++) { if (Colour & (1 << 23)) { SPI_Output[DestIndex] = 0xe0; } else { SPI_Output[DestIndex] = 0x80; } Colour = Colour << 1; if (Colour & (1 << 23)) { SPI_Output[DestIndex] |= 0xe; } else { SPI_Output[DestIndex] |= 0x8; } Colour = Colour << 1; } for (int i=0;i<12;i++) { transferSPI(SPI_Output[i]); } }
The code expands a 3 byte colour sequence into a 12 byte one and sends it over an SPI link running at 2MHz. This is sufficient to match the timing requirements of the PL9823.
A demo program which cycles through various colours can be found over on github.
One thought on “STM32L031 controlling a PL9823 LED using SPI”