top of page

STM32 TIMER PWM LED DIMMER



ree

Previous Tutorial

Tutorial 14

Next Tutorial


ree


1. Explain about Example:

In this example, we'll demonstrate how to use the TIMER peripheral of the STM32 NUCLEO L476RG microcontroller to generate PWM signals for dimming an LED. PWM (Pulse Width Modulation) is a technique used to control the brightness of LEDs by varying the duty cycle of the PWM signal. We'll configure the TIMER to generate PWM signals, and by adjusting the duty cycle, we can control the brightness of the LED.


2. Required Parts For Example:

  • STM32 NUCLEO L476RG board

  • LED

  • Appropriate resistor for the LED

  • USB cable for connecting the board to your computer

  • CUBEIDE software for STM32 development

3. CUBEIDE Configuration Steps:

  1. Open CUBEIDE and create a new project for the STM32 NUCLEO L476RG board.

  2. Configure the TIMER peripheral to generate PWM signals.

  3. Enable the GPIO peripheral for controlling the LED.

  4. Generate code and open the main.c file for editing.


-> Configure Timer2 Peripheral: Prescaler:65535, Sistem Clock=72MHz


ree

--> CODE:

#include "stm32l4xx_hal.h"

TIM_HandleTypeDef htim;
TIM_OC_InitTypeDef sConfig;

void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM_Init(void);

int main(void) {
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_TIM_Init();

  HAL_TIM_PWM_Start(&htim, TIM_CHANNEL_1);

  uint16_t duty_cycle = 50; // Initial duty cycle 

  while (1) {

    	  while(TIM2->CCR1<65535)
	  {
		  TIM2->CCR1 += 50;
		  HAL_Delay(2);
	  }
	  while(TIM2->CCR1>0)
	  {
		  TIM2->CCR1 -= 50;
		  HAL_Delay(2);
	  }
  }
}

void SystemClock_Config(void) {
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 1;
  RCC_OscInitStruct.PLL.PLLN = 10;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4);
}

static void MX_GPIO_Init(void) {
  __HAL_RCC_GPIOA_CLK_ENABLE();

  GPIO_InitTypeDef GPIO_InitStruct = {0};

  GPIO_InitStruct.Pin = GPIO_PIN_5;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.Alternate = GPIO_AF2_TIM2; // Use alternate function for TIM2 CH1
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

static void MX_TIM_Init(void) {
  __HAL_RCC_TIM2_CLK_ENABLE();

  htim.Instance = TIM2;
  htim.Init.Prescaler = 0; // Adjust as needed for PWM frequency
  htim.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim.Init.Period = 65535; // Maximum value for 10-bit resolution
  htim.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  HAL_TIM_PWM_Init(&htim);

  sConfig.OCMode = TIM_OCMODE_PWM1;
  sConfig.Pulse = 0; // Initial duty cycle
  sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfig.OCFastMode = TIM_OCFAST_DISABLE;
  HAL_TIM_PWM_ConfigChannel(&htim, &sConfig, TIM_CHANNEL_1);
}

5. Result:

The LED connected to pin PA0 will vary in brightness based on the duty cycle of the PWM signal generated by the TIMER peripheral. Adjust the duty cycle value to control the brightness of the LED.


ree



ree


ree

Previous Tutorial

Tutorial 14

Next Tutorial


ree


 
 
 

Recent Posts

See All

Comments


bottom of page