cancel
Showing results for 
Search instead for 
Did you mean: 

I am wondering how to use PWM to adjust colours for an RGB LED if the LED is already set as a GPIO_Output? (so I cannot also set them as a timer(s)).

cl.1_99
Associate II

Hi all, I am currently writing a program that uses the STM32L432KC board (nucleol432kc) and a thermistor to obtain a temperature and then display that temperature on an LCD, a terminal screen, as well as turn an RGB LED on with different temperature ranges corresponding to different colours (ex. temp lower than 20 degrees C, blue led on...etc) Each component is currently working perfectly, the only extra thing I cannot figure out is how I can use PWM (and how to set up the configuration for it) to change the colours to different mixes of RGB. I have each leg of the RGB LED set as GPIO_Outputs, but I do not know what pin I should be setting as a timer if I want to use the PWM. I am using STM32CubeIde.

I have attached the current configuration for my board, which does work perfectly fine (so ideally I wouldn't want to make any huge changes, mostly because whenever I do it gets rid of all of my code and even when I copy and paste it back there's usually some errors). All I want to be able to do is adjust the colour mixing of the RGB LED in a way that looks like (255, 0, 0) for red, (0, 255, 0) for green... etc.

Note* the only configuration requirements are:

PA8 is red

PA9 is green

PA10 is blue

PA1 is for ADC of thermistor voltage

9 REPLIES 9

Use whatever timer channels that are on PA8 to PA10.

If there are none, you can still bit-bang the PWM e.g. in a timer interrupt if you don't mind the processor load this implies. A better solution is a timer-triggered DMA transferring the required pattern from memory to GPIOA_BSRR.

JW

Thanks for the reply! I am not sure how to do the latter of your suggestions, but for using the timer channels on PA8-PA10, the only one that is available is for PA8 and it is PWM Generation CH1N (all other options have conflicts), PA9 and PA10 only have PWM Generation No Output. Do you have any suggestions as to what they should be set to and/or how to physically code or what functions to use to actually set the colour mixes?

I got it to work with PWM, however, whenever I try to make new colours out of combos of RGB, there is a ton of flickering that happens. Basically, whenever the value is not 255, there is flickering. This is some of the code:

void set_rgb (int red, int green, int blue)

{

htim1.Instance->CCR1 = red;

htim1.Instance->CCR2 = green;

htim1.Instance->CCR3 = blue;

}

  HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);

 HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);

 HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);

 HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2);

 HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);

 HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_3);

  while (1)

 {

  /* USER CODE END WHILE */

 set_rgb(255, 100, 0);

 HAL_Delay(1000);

 set_rgb(0, 255, 0);

 HAL_Delay(1000);

 set_rgb(0, 0, 255);

 HAL_Delay(1000);

  /* USER CODE BEGIN 3 */

 }

Any suggestions?

Make your timers go faster. They should update at at least 100Hz or more to avoid noticeable flicker. No harm making them go even faster if you're driving LEDs.

If you feel a post has answered your question, please click "Accept as Solution".

Where do I do that? The only freq I can see/change is in the clock configuration, and there it says 80MHz is the max value.

In your timer settings. The prescaler and auto-reload values will dictate the speed that the timer updates.
If you feel a post has answered your question, please click "Accept as Solution".

I have prescaler as 984-1 and auto-reload as 255-1 (based on this tutorial and the fact that the clock is set to 64 MHz https://www.youtube.com/watch?v=KRhctlCOBOg). Also thank you for your replies!

So your update frequency is 32MHz / 984 / 256 = 127 Hz. Plus or minus a factor of 2, I'm guessing at the timer clock.
Changing the prescaler to 0 should solve the problem. Or 7 or 15 or 31.
If you feel a post has answered your question, please click "Accept as Solution".

So I have it working with no flickering with the prescaler at 100 (just from trial and error, I might still lower it) and with the auto-reload at 255 still. Now to find a colour combo that looks correct on this RGB LED. Thanks a bunch!!!