Skip to main content
Graduate II
May 12, 2025
Solved

STM32L432 – WS2812 LEDs not working reliably (no external crystal)

  • May 12, 2025
  • 7 replies
  • 2139 views

Hi everyone,

I'm trying to drive a WS2812 LED strip (20 LEDs - Strip has 60 LEDs) using an STM32L432KCUx, but so far without success.
I've tested multiple approaches — PWM with DMA, SPI-based encoding, and bit-banging via GPIO — but none of them produce stable or correct results. Sometimes up to 22 LEDs light up instead of 20, and the colors are often wrong or appear white.

Occasionally, the first LED doesn’t light up at all, which seems to indicate timing jitter or inaccurate bit timing.


Setup:

  • Microcontroller: STM32L432KCUx

  • No external HSE crystal or LSE available

  • SYSCLK = 48 MHz, tested using both MSI via PLL and HSI via PLL

  • Output pins used:

    • PA12 for SPI and bit-banging

    • PA9 for PWM (TIM1_CH2)

  • SPI clock set to 2.4 MHz (used for encoded WS2812 bitstream) SYSCLK = 38MHz

  • 3.3 V logic level used for WS2812 data line (no level shifter)

  • LED strip powered with 5 V USB - GND connected


Additional info:

  • I confirmed the LED strip and wiring are working:
    Using an Arduino, the same strip works immediately and reliably.


Questions:

  • Has anyone successfully driven WS2812 LEDs using an STM32L4 without an external crystal?

  • Could the issue be due to clock accuracy (MSI/HSI) or the 3.3 V logic level?

  • Does anyone have a working example project (PWM, SPI, or bit-banging) for this or a similar STM32 without HSE?

I'd really appreciate any tips, example code, or experience on this topic!

    This topic has been closed for replies.
    Best answer by Martin42

    After many attempts and small mistakes with sometimes big consequences, I’ve now powered the LED strip with 4 volts and am using https://github.com/nimaltd/ws28xx library.

    Of course, it didn’t work perfectly right away, but now everything is running smoothly.

     

    Many thanks for the quick and helpful support!

    7 replies

    Super User
    May 12, 2025
    Martin42Author
    Graduate II
    May 12, 2025

    Hi, thanks for your reply. I already found it, but I thought it was an STM32F4 controller, which I believe is faster than the L4. Am I right?

    Super User
    May 12, 2025

    I don't think speed is an issue here. Adjust the timer period based on your clock settings. It's a one line change.

    The accuracy difference between HSE and HSI is not an issue for the WS2812.

    Super User
    May 12, 2025

    You haven't said what hardware you're using - see: How to write your question to maximize your chances to find a solution.

     

    Have you used a scope or logic analyser to verify your drive signal?

     


    @Martin42 wrote:
    • Could the issue be due to clock accuracy (MSI/HSI) or the 3.3 V logic level?

    Could even be both!

    Start by eliminating some unknowns - does it work if you do use a crystal HSE ?

    Martin42Author
    Graduate II
    May 12, 2025

    I don’t have an external HSE crystal available on my custom hardware, and adding one would require a fairly involved soldering modification — so I’m trying to avoid that unless it’s absolutely necessary.

    That’s why I’m particularly interested to know if anyone has successfully driven WS2812 LEDs using an STM32L4 series MCU without an external crystal. I'm currently using internal oscillators (MSI or HSI via PLL), but the timing doesn't seem reliable enough.

    Your comment actually reminded me that I do have an STM32 Nucleo-32 board (STM32L432KC), which includes an on-board 24 MHz crystal oscillator. This allows me to test the exact same WS2812 setup with a stable HSE-based clock configuration. If it works reliably on the Nucleo board, that would strongly suggest the problems on my custom board are due to clock inaccuracy or jitter caused by the internal MSI/HSI oscillators.

    I'll test this setup next to confirm whether the internal clock is indeed the root cause.

    Thank you.

    Super User
    May 12, 2025

    @Martin42 wrote:

    I don’t have an external HSE crystal available on my custom hardware, and adding one would require a fairly involved soldering modification.


    So test on a Dev Board - eg, your Nucleo board.

    A key use of dev boards is, after all, to prove this sort of unknown before committing to custom hardware ...

     


    @Martin42 wrote:

    clock inaccuracy or jitter caused by the internal MSI/HSI oscillators.


    They are certainly less accurate than a crystal, but jitter shouldn't be a problem.

    The timing tolerances on the WS LEDs are pretty wide - they are designed as cheap consumer parts - so it should be doable.

    AIUI, they regenerate their own timing on the data that they pass-through - and they certainly don't have a crystal with each LED!

    Martin42Author
    Graduate II
    May 12, 2025

    After further attempts to get the crystal working on my NUCLEO-L432KC evaluation board, I discovered that there isn't one installed on the STM32L4, only on the STM32F4. That's a bummer.

    The L4 has a 32K LSE mounted on X1. Of course, that doesn't work as a HSE.

    Do you think I really don't need an external crystal to control the SW2812?

    Super User
    May 12, 2025

    > Do you think I really don't need an external crystal to control the SW2812?

    Required timings seem pretty generous, don't they?

    TDK_0-1747088695216.png

     

    Graduate II
    May 13, 2025

    As@FKaes.1 said, I also think it is related to the voltage level of DIN on the first LED.

    @Andrew Neilwe don't know where @Martin42 probed the signal in his 2 plots. It can be at DOUT of the first LED, since I count 5 vertical divs for the signal.

    In my experience 3V3 is not high enough, I also got wrong colors, or even wrong LEDs being lit. Once I put a level converter everything was back on track.

    @Martin42in my view it is simpler to add a level converter before the first LED and see if the results are good or not.

    Super User
    May 13, 2025

    @Kraal wrote:

    @Martin42in my view it is simpler to add a level converter before the first LED and see if the results are good or not.


    Fair point: eliminate sources of doubt & uncertainty!

    Super User
    May 13, 2025

    @Martin42 wrote:
    • Using an Arduino, the same strip works immediately and reliably.


    What Arduino - 3V or 5V ?

    Martin42Author
    Graduate II
    May 13, 2025

    5V Arduino Mega 2560

    Martin42AuthorAnswer
    Graduate II
    May 13, 2025

    After many attempts and small mistakes with sometimes big consequences, I’ve now powered the LED strip with 4 volts and am using https://github.com/nimaltd/ws28xx library.

    Of course, it didn’t work perfectly right away, but now everything is running smoothly.

     

    Many thanks for the quick and helpful support!

    Super User
    May 13, 2025

    and are you using an internal RC oscillator with it?

    Martin42Author
    Graduate II
    May 13, 2025

    I’m now using PWM and DMA via Timer 1, Channel 2. HSI and PLL for a Sysclock of 80MHz.

    Martin42_0-1747133872387.png

     

    Super User
    May 13, 2025

    and are you now using a voltage translator?