Controlling a PL9823 LED over Bluetooth LE

I received an ESP32-Camera board from Aliexpress recently but unfortunately it’s camera was broken (the supplier is sending a free replacement). Not wanting to waste the otherwise good ESP32 I decided to see if it could be used to control the brightness and colour of an LED such as the PL9823. These LED’s are controlled using a serial data string which is documented elsewhere in this blog (https://ioprog.com/2016/04/09/stm32f042-driving-a-ws2812b-using-spi/ and https://ioprog.com/2018/11/08/stm32l031-controlling-a-pl9823-led-using-spi/)

It turned out to be pretty straightforward.

The ESP32 board was connected to the PC over a USB-Serial converter. Two buttons were added to control boot mode and the PL9823’s Data In pin was connected to IO2. The code to control all of this was developed in the Arduino environment (based off an example) and is as follows:

#include <SPI.h>

/*
 *  Controlling a PL9823 LED over bluetooth on an ESP32
    Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleWrite.cpp
    Ported to Arduino ESP32 by Evandro Copercini
*/

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

#define SERVICE_UUID        "22389e17-7cee-41ce-8aa0-28a4482f7020"
#define CHARACTERISTIC_UUID "a575e1bf-e15f-4534-a80c-1837348360ad"

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;
    }    
    SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0)); 
    SPI.transfer(SPI_Output, 12);
    delay(10);
    SPI.endTransaction();
}

class MyCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pCharacteristic) {
      std::string value = pCharacteristic->getValue();
    
      if (value.length() > 0) {
        // Write debug messages out to serial port
        Serial.println("*********");
        Serial.print("New value: ");
        for (int i = 0; i < value.length(); i++)
          Serial.print(value[i]);

        Serial.println();
        Serial.println("*********");
        // update the PL9823 LED
        uint32_t intvalue;
        intvalue = strtoul(pCharacteristic->getValue().c_str(),NULL,16);
        writePL9823(intvalue);
      }
    }
};

void setup() {
  
  SPI.begin(1,4,2,3); // (int8_t sck, int8_t miso, int8_t mosi, int8_t ss)
  Serial.begin(115200);  
  Serial.println("Connect to the device over BLE and change the colour of the LED");

  BLEDevice::init("BLE_PL9823");
  BLEServer *pServer = BLEDevice::createServer();

  BLEService *pService = pServer->createService(SERVICE_UUID);

  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setCallbacks(new MyCallbacks());

  pCharacteristic->setValue("ffffff");
  
  pService->start();

  BLEAdvertising *pAdvertising = pServer->getAdvertising();
  pAdvertising->start();

  // Write the initial value out to the PL9823
  uint32_t intvalue;
  intvalue = strtoul(pCharacteristic->getValue().c_str(),NULL,16); 
  writePL9823(intvalue);
}
void loop() {
  // put your main code here, to run repeatedly:
  
  delay(1000);
}

Using an Andoid app like BLE Scanner, it is possible to control the the LED by sending a hex string such as ff0000 for maximum red; 00ff00 for max green and 0000ff for max blue. These colours can be mixed to form arbitrary colours and brightness.

STM32L031 controlling a PL9823 LED using SPI

stm32l031_pl9823
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. pl9823_mosi
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.