Difference between revisions of "STM32 WS2812B"
(Created page with "Category:STM32Category:STM32 DevelopmentCategory:STM32CubeMXCategory:STM32CubeIde{{metadesc|How to use a WS2812 LED with STM32}} When developing the STM32Dev...") |
|||
(12 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | [[Category:STM32]][[Category:STM32 Development]][[Category:STM32CubeMX]][[Category:STM32CubeIde]]{{metadesc|How to use a WS2812 LED with STM32}} | + | [[Category:STM32]][[Category:STM32 Development]][[Category:STM32CubeMX]][[Category:STM32CubeIde]]{{metadesc|How to use a WS2812 LED with STM32}}{{metadesc|STM32 WS2812b library using PWM and DMA}} |
− | When developing the [[STM32Dev]] board | + | [[File:STM32Dev WS2812B RGB LED.jpg|thumb]] |
+ | When developing the [[STM32Dev]] board a [[WS2812B]] RGB [[LED]] was including connected to the PC6 [[GPIO]]. | ||
+ | |||
+ | The [[WS2812B]] LED contains 3 LEDS: red, green and blue and each of these LEDS can be set at 256 different brightness levels. These levels are controlled with a bit stream running at 800 kHz and multiple LEDS (up to 1000-ish) can be chained after each other. | ||
+ | |||
+ | Searching the Internet there are a '''lot''' of suggestions and implementations dealing with the ws2812 LEDs, so obviously I decided to take a different approach. This approach is described on this page. | ||
+ | |||
+ | == Example == | ||
+ | |||
+ | Before digging into the details, here's an example of a [[STM32F103]] driving an 8 x 8 matrix of WS2812B LEDs: | ||
+ | |||
+ | {{#ev:youtube|-rt49mIliGs}} | ||
+ | |||
+ | {{#ev:youtube|qH-1c5IOwco}} | ||
+ | |||
+ | == Protocol Analysis == | ||
+ | |||
+ | WS2812 LEDs are daisy chained together like this: | ||
+ | |||
+ | [[File:WS2812B Chain.png|800px]] | ||
+ | |||
+ | When sending data each LED need 24 bit - 8 bit for each color. The first LED in the chain will "use" the first 24 bit and then pass any following bit to the next LED in the chain and so forth. After a reset (a pause with no data) this cycle repeats. | ||
+ | |||
+ | [[File:WS2812B Data Transmission Method.png|600px]] | ||
+ | |||
+ | For each individual LED the 24 bits are expected like this: | ||
+ | |||
+ | [[File:WS2812 bits.png|800px]] | ||
+ | |||
+ | Notice that for the WS2812B the order is GRB (Green, Red, Blue). | ||
+ | |||
+ | Each bit is send as one pulse. The difference between high and low time indicates if the bit is set or reset: | ||
+ | |||
+ | [[File:WS2812 sequence chart.png|400px]] | ||
+ | |||
+ | The bits are send at a frequency of 800 kHz (800000 bits per second) so each bit in total takes 1.25 μs. The early WS2812s were really timing critical but the later models are a lot less critical. Official timing for the WS2812B is: | ||
+ | |||
+ | [[File:WS2812B Timing.png|800px]] | ||
+ | |||
+ | == Implementation == | ||
+ | |||
+ | There are a number of ways, driving a string of [[WS2812]]'s could potentially be implemented on [[STM32]] [[MCU]]'s. One of the better approaches I have seen involve using the MOSI signal of SPI to drive the LEDs (while ignoring the SCK). | ||
+ | |||
+ | == Library on Github == | ||
+ | |||
+ | The library is available on Github: | ||
+ | |||
+ | [https://github.com/lbthomsen/stm32-ws2812b https://github.com/lbthomsen/stm32-ws2812b] | ||
+ | |||
+ | == Links == | ||
+ | |||
+ | * [https://ipfs-pin.com/ipfs/Qmc1osaeY9SUg8wkktzkSach2ANuuSSJe6Ejd1kibvmPSD/Worldsemi-WS2812B-B_C114586.pdf WS2812B Datasheet V5] |
Latest revision as of 02:22, 11 February 2024
When developing the STM32Dev board a WS2812B RGB LED was including connected to the PC6 GPIO.
The WS2812B LED contains 3 LEDS: red, green and blue and each of these LEDS can be set at 256 different brightness levels. These levels are controlled with a bit stream running at 800 kHz and multiple LEDS (up to 1000-ish) can be chained after each other.
Searching the Internet there are a lot of suggestions and implementations dealing with the ws2812 LEDs, so obviously I decided to take a different approach. This approach is described on this page.
Example
Before digging into the details, here's an example of a STM32F103 driving an 8 x 8 matrix of WS2812B LEDs:
Protocol Analysis
WS2812 LEDs are daisy chained together like this:
When sending data each LED need 24 bit - 8 bit for each color. The first LED in the chain will "use" the first 24 bit and then pass any following bit to the next LED in the chain and so forth. After a reset (a pause with no data) this cycle repeats.
For each individual LED the 24 bits are expected like this:
Notice that for the WS2812B the order is GRB (Green, Red, Blue).
Each bit is send as one pulse. The difference between high and low time indicates if the bit is set or reset:
The bits are send at a frequency of 800 kHz (800000 bits per second) so each bit in total takes 1.25 μs. The early WS2812s were really timing critical but the later models are a lot less critical. Official timing for the WS2812B is:
Implementation
There are a number of ways, driving a string of WS2812's could potentially be implemented on STM32 MCU's. One of the better approaches I have seen involve using the MOSI signal of SPI to drive the LEDs (while ignoring the SCK).
Library on Github
The library is available on Github:
https://github.com/lbthomsen/stm32-ws2812b