Interface WS2812 with STM32
In this tutorial, we will interface the WS2812 LED with STM32. The same process can be used for WS2812B also. I am going to use PWM with DMA to send the data to the LED. I will explain the steps along the way, and how they are related to the datasheet of the device.
Let’s start with Setup
First of all Let’s see the clock setup. Notice that the APB2 Timer Clock is running at 72 MHz
Now we will enable the Timer 1 in the PWM output mode. Also note that the Prescalar is set to 0, and ARR is 72-1
- Since the Timer 1 is connected to the APB2 clock, it was initially running at 72 MHz
- Now we use prescalar of 0, that means we are diving this Frequency by 1, So the timer is still running at same 72 MHz frequency
- The ARR of 90 will bring down this timer frequency to 72 MHz/80 = 800 KHz
- Also this 90 in the ARR is the maximum duty cycle i.e 100% Duty = 90 (in the CCR)
- Now if we want to change the duty cycle, we have to calculate the percentage from 90
- For example, if we want the duty cycle of 30%, we will use the value 27, and for 70% Duty, the value will be 63
Now let’s setup the DMA
- DMA is set for TIM1 channel 1
- Memory to peripheral direction is needed, since we are sending the data to the peripheral
- Use the normal mode, since we only want the DMA to transfer the data, when we command it to do so
- I am using the data width of half word here, for no particular reason
This completes the setup process. Now let’s see some important points from the datasheet of the device
Below is the picture from the datasheet, explaining how to send a 0 and a 1 to the device
- As you can see in the picture above, in order to send a 0, the Pulse should be HIGH for 0.4 us, and then LOW for 0.85 us
- This makes up about 1/3rd of the Pulse should be HIGH
- Similarly, to send a 1, 2/3rd of the Pulse must be HIGH
- Reset code consists of the LOW Pulse for more than 50 us
Next we have the bit arrangement
- You can see that each data consists of 24 bits, G R B
- And we must send the Green bits (MSB) first
- Also the data for all the LEDs should be sent together. For eg- If you have 12 LEDs, then you need to send 12×24 = 288 bits one after other
- The driver will assign the first 24 bits to the first LED, 2nd set of 24 bits to second LED, and so on
- After sending the data for all the LEDs, we must keep the signal LOW for more than 50 us, or else, the driver will consider that this data is for the NEXT LED.
This is all the infomation we needed from the datasheet. Now Let’s go through the CODE
- Define the Maximum LEDs Connected in cascade
- USE_BRIGHTNESS can be set to 1, if you want to use the brightness control. Or set it to 0
- LED_Data and LED_Mode are the matrices to store the LED related data.
Storing the LED data
- LED_Data is used to store the color data for individual LED. Here First Column Represents the LED number, 2nd column for Green, then Red and Last column for blue color
- LED_Mod is also used to store LED related data, but the scaled values, as per the brightness settings
- Controlling the Brightness is fairly simple. All we need to so is divide the actual value by some number. For eg- Red 255 will be brightest, 127 will be 50% bright and 63 will be 25% bright
- I am using the Tangent function to bring some linearity in the scaling.
- The brightness values can vary from 0 to 45
Convert and send the data to DMA
- Here we first convert the individual color values to a single 24 bit data
- Now we will check each bit of this color data
- if the bit is 1, we will store 60 in the buffer.
- This 60 comes from the fact that, in order to store a 1, we need to keep the Pulse High for 2/3rd of the time for one period.
- Since the ARR value is 90 (100% Duty), 2/3rd of this will be 60.
- Also note that I am storing the MSB value to the LSB position, because as per the device requirement, we need to send the MSB first
- Similarly, if the value is 0, we will store 30 (1/3rd of 90) in that position
- After storing all the values for the LEDs, we need to also store the values, to keep the Pulse LOW for more than 50 us
- To achieve this, we can store 50 zeroes. Since our period is 1.25 us, 50 zeroes will cover more than 50 us of the LOW time
- And now we send this data to the DMA
Stopping the DMA
- Once the data transfer has been finished, the Pulse Finish callback function will be called
- Here we will just STOP the DMA, so that another transfer doesn’t take place automatically
- Here first of all I am setting the different colors to the LEDs
- Inside the while loop, I am increasing the brightness every 50 ms
- and then decreasing the brightness. You can see the Result below
- Note that after setting the LED, you must call WS2812_Send function, to send the data to the LED
- I am calling that function inside the while loop
Below is the output of the above program. To see the entire process, check the the video also