Back to Trending
RepositoryCFRFrance
MA

MaJerle/stm32-ws2811-ws2812-ws2812b-ws281x-tim-pwm-dma-timer

WS2811, WS2812, WS2812B or compatible leds driver with STM32, TIM, PWM and DMA with minimum RAM required

291stars
56forks
1issues
3views

Featured achievements

The project's latest awards

1
TINIX TRENDING·Topic: ws2811

#1 Repository of the month

1
TINIX TRENDING·Topic: ws2812b

#1 Repository of the month

1
TINIX TRENDING·Topic: leds

#1 Repository of the month

WS2811 and WS2812B driver for STM32 with TIM, PWM and DMA

This application note aims to explain architecture and understanding how to develop driver for addressable LEDs, such as WS2811, WS2812, WS2812B or any other matching protocol specifications.

Table of Contents

Abbreviations

  • DMA: Direct Memory Access controller in STM32
  • TIM: Timer peripheral, general or advanced, w/ or w/o external channel (output compare or input capture)
  • PWM: Pulse-Width-Modulation generated by TIM peripheral
  • HT: Half-Transfer Complete DMA event/flag
  • TC: Transfer Complete DMA event/flag
  • IRQ: Interrupt

Understanding LED protocol

WS2811 and WS2812 protocol is specific one and has defined values:

  • Transfer rate is 800 kHz, or 1.25us pulse length for each bit
  • Transfer length is 24 pulses for each led, that's 30us for one LED
  • Each logical bit (1 or 0) consists of high and low part, with different length
  • Reset pulse is needed prior updating led strip, to synchronize sequence

WS2811 & WS2812 & WS2812B LED protocol

Minimum reset pulse length depends on WS281x device. Check datasheet for your particular unit. WS2812B says > 50us, while WS2811 says > 280us.

STM32 TIM and PWM

Several STM32 timers (TIM) have support for Capture/Compare channels, connect to output pins, and being able to generate PWM pulses. According to protocol (also explained above), timer should be able to generate 3 type of pulses:

  • All-time zero to emulate reset pulse
  • Logical bit 1 with ~3/4 of time being high and low for the rest of the time
  • Logical bit 0 with ~1/4 of time being high and low for the rest of the time

Furthemore, timer should be configured to generate update event for 800 kHz, with auto-reload register (TIMx->ARR) set to TIMx->ARR = timer_kernel_clock / 800000 - 1 and prescaler = 0 for maximum resolution.

Timer does not support a look-up table to know if logical bit 1 or 0 is next, hence we will utilize DMA feature to implement this task. Timer channel allows DMA requests to load next value to channel compare register, and effectively implement look-up table with data transfer from memory to timer peripheral.

STM32 Timer Cookbook is a great starting point to understand how timers work in STM32s.

STM32 DMA

DMA controllers in STM32s support various operations, one of them being super handy for our WS LED driver, called circular operation mode. Circular mode will continuously transmit data from memory to peripheral (or, in general, can also go opposite direction) and periodically send transfer-complete or half-transfer-complete interrupts to the application.

STM32 DMA circular mode

We will use HT and TC events extensively, as they will be use to prepare data for next operations to transfer all bits for all leds. More explained in the later sections.

List of some useful STM32 DMA application notes

Memory requirement

Memory requirement for one LED strip is split to:

  • 3 * leds_count to store read, green & blue colors for each led
  • 1 working buffer with size of 2 leds (48 elements), each element of 16/32 bits, depending on used TIM peripheral

Putting it all together

To put all together, application developer must:

  • Decide to use one timer with PWM generation support with one channel
  • Select one DMA channel/stream/dmamux (depends on STM32) for selected timer and channel
  • Put DMA in circular mode and enable HT and TC interrupts
  • Use HT and TC events to prepare data for next operation
    • HT event is used to prepare data at the beginning of memory (part of memory just completed with transmision)
    • TC event is used to prepare data for second part of memory

Examples are extensively commented and should provide necessary understanding for application development

Examples

Examples can be used as reference code to implement your own LED driver with your own STM32.

  • Developed in STM32CubeIDE for easier evaluation on STM32 boards
  • Supports CMake and VSCode development
  • Uses LL or HAL drivers
  • Demos for various STM32
STM32 familyBoard nameTIM & CHGPIODMA settings
STM32G0xxNUCLEO-G0B1RETIM2 CH4PA3DMA2, Channel 5

Similar Projects

Discover other similar projects and AI models.

RepositoryC
Crazy-Geeks/STM32-ARGB-DMA

STM32 Library for Addressable LEDs: WS2812(b), WS2811, SK6812, etc. Supports RGB & RGBW LEDs.

128
Jun 8, 2023
RepositoryC
nimaltd/ws28xx

ws28xx (ws2811, ws2812, ws2812b, ws2815, ...) Library for STM32 (PWM+DMA)

108
Oct 2, 2026
RepositoryC
MaJerle/stm32-usart-uart-dma-rx-tx

STM32 examples for USART using DMA for efficient RX and TX transmission

1.8k
Sep 12, 2026
RepositoryC
libdriver/ws2812b

WS2812B full-featured driver library for general-purpose MCU and Linux.

207
Jun 30, 2026
RepositoryC
tom-2015/rpi-ws2812-server

Raspberry Pi WS2812 (web) server tool

172
Dec 31, 2022
RepositoryC
mafredri/asustor-platform-driver

Linux kernel platform driver for ASUSTOR NAS hardware (leds, buttons)

109
Nov 14, 2026
Modeltext-classification
cis-lmu/glotlid

GlotLID !GlotLID(https://img.shields.io/badge/🤗-Open%20In%20Spaces-blue.svg)(https://huggingface.co/spaces/cis-lmu/glotlid-space) Description GlotLID is a Fasttext language identification (LID) model that supports more than 2000 labels. Latest: Glo...

♥0
504.7k
Nov 5, 2026
RepositoryC#
todbot/blink1

Official software for blink(1) USB RGB LED by ThingM

977
Sep 26, 2026
RepositoryC#
nanoframework/nanoFramework.IoT.Device

📦 This repo includes .NET nanoFramework implementations for various sensors, chips, displays, hats and drivers

321
Nov 12, 2026