I'm writing a program where I want to use the intensity/brightness of one of the onboard LEDs to indicate a specific state change. Is it possible to use PWM or some other means to modify the brightness of one of the onboard LEDs?

I have set a timer into PWM mode that ticks via interrupt every 50 ms and provides the PWM value range that I want. However, I'm unsure of how to make the GPIO pin (LD pins) respond to the ticks of the timer/PWM. I've tried configuring the LD/onboard-LED pins in the GPIO.c with the following code:

GPIO_InitStruct.Pin = LD4_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

From the information I have gathered from the HAL manual and other online information, I hoped that these changes (setting GPIO_InitStruct.Mode = GPIO_MODE_AF_PP and GPIO_InitStruct.Alternate = GPIO_AF1_TIM2) would allow the timer to control the LED output however, with this code the LED doesn't activate at all.

Is there something I'm missing with this method or is this the completely wrong method to be trying to control the brightness of the onboard LED?

Any assistance on this would be appreciated.


1 Answer 1


The alternate functions cannot be freely mapped.

LD4 is green led on PD12, and PD12 does not have an alternative feature that maps to TIM2.

However, it can be mapped to Timer 4.

  • \$\begingroup\$ Thanks for your reply. Are all the onboard LEDs the same in this regard? And are there any other timers I can map to them? I'm actually intending to map LD6, does this change anything? \$\endgroup\$
    – Saturn
    Commented Jun 2, 2020 at 11:46
  • \$\begingroup\$ I don't know and I don't have the datasheet open now, but you can check yourself. The datasheet contains tables that list which alternate functions are available for each pin. \$\endgroup\$
    – Justme
    Commented Jun 2, 2020 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.