2025-05-12 9:01 AM - edited 2025-05-12 9:02 AM
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.
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
I confirmed the LED strip and wiring are working:
Using an Arduino, the same strip works immediately and reliably.
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!
Solved! Go to Solution.
2025-05-12 5:16 PM
Pretty sure it's not critical to use a crystal or XO. More likely a fumble at the DMA HT/TC or IRQ processing, or how the TIM is using shadow register or not (Pre/AutoLoad?)
The SW2812 will be unforgiving, but you need to look at the mechanics, the L4 and F4 should be more than capable.
2025-05-13 12:55 AM
@Martin42 wrote: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
Usually the target MCU is clocked from the ST-Link - which does have a crystal ...
2025-05-13 12:57 AM
@Martin42 wrote:I tested a single LED with red color using PWM and DMA. The signal was mostly correct, but occasionally the LED displayed white instead of red.
That's not due to your clock drifting - that's an error in your pattern generation.
2025-05-13 1:23 AM
Hi, I have it running on a L4 (STM32L431). 256 (16x16 module) WS2812B (4 Pin version). Via DMA and Timer.
2025-05-13 1:50 AM
@FKaes.1 wrote:
- 3V3 is out of spec. .
While this is true, I very much doubt that it's the issue here.
Look at the scope plots @Martin42 posted earlier - the sequence is clearly wrong in the "bad" case.
2025-05-13 2:08 AM
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.
2025-05-13 2:54 AM
@Martin42 wrote:
Using an Arduino, the same strip works immediately and reliably.
What Arduino - 3V or 5V ?
2025-05-13 2:56 AM
@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!
2025-05-13 3:37 AM
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!
2025-05-13 3:37 AM
5V Arduino Mega 2560