cancel
Showing results for 
Search instead for 
Did you mean: 

PWM Output on TIM3 Channel 1 Not Working for 4 kHz Buzzer on STM32L476

GR88_gregni
Associate II

Hello,

I'm writing this post because I'm facing an issue with generating a PWM signal on TIM3 for a buzzer application using the STM32L476RET microcontroller. Based on the buzzer's datasheet, it requires a 4,000 Hz square wave with a 50% duty cycle. I’ve connected the buzzer to the PC6 pin, which should work as TIM3_CH1 when configured to AF2, according to the microcontroller’s documentation.

Configuration Summary

  • Microcontroller : STM32L476RET
  • Clock Source: Internal clock at 16MHz
  • PWM Pin : PC6 (TIM3 Channel 1)

pwm1.png

In CubeMX, I configured TIM3 Channel 1 for PWM output with these settings:

  1. Prescaler: 3
  2. Auto-Reload Register (ARR): 999

These settings were calculated based on the formula:

presc.png

This results in a 4,000 Hz PWM frequency, as expected:

prec2.png

pwm2.png

 

I did this based on microcontroller datasheet:

pin_description.png 

For the 50% duty cycle, I set TIM3->CCR1 = 500 in code.

Code for sound Generation

Here is the code I use to start the PWM signal:

(Note that HAL_TIM_PWM_Start returns HAL_OK)

 

 

 

/**
 * @function start_sound_generation
 *
 * @brief Enables the square wave of PWM signal on TIM3, and channel 1.
 */
void start_sound_generation(void)
{
	TIM3->CCR1 = 500;
    printf("Starting sound generation...\n");
    HAL_StatusTypeDef res = HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
    printf("res:%d\n", res);
}

 

 

 

 

Issue

The buzzer does not generate any sound. When I check the PC6 pin with an oscilloscope, there is no waveform, indicating the PWM output is not active.

Any insights on why the PWM signal might not be generating as expected, or potential troubleshooting steps, would be greatly appreciated!

1 REPLY 1

Read out and check/post content of TIM and relevant GPIO registers.

JW