Tuesday, June 11, 2019

How to interface BUZZER with STM32

In this tutorial we are going to interface BUZZER with STM32 microcontroller. The buzzer, which I am using, is an Active Buzzer Low Level Trigger Alarm Module along with STM32F446RE microcontroller.


These modules generally have 3 pins i.e.
Vcc  —->  around 3 to 5 V
Gnd —–> Ground
I/O  —–> input to the buzzer inorder to control it’s sound

 HOW TO 

In order to interface the buzzer, we have to provide a variable input to the buzzer and for that purpose, I will be using PWM to create a square wave of low frequency and also I will vary the duty cycle to observe the changes in the sound of the buzzer.
Open CubeMx and setup PWM channel output

Next we need to set the clock. I am using TIM2, which is connected to the APB1 running at 45 MHz

Now this part is important just like any other PWM application. We need to set theprescalar and ARR values. As the timer in my case is running at 45 MHz and also I want the duty cycle to vary between 0 to 255, the following is the calculation for the prescalar value
(45 MHz / (255*255)) = 692
ARR = 255
The reason for the values being ‘-1’ is because cubemx will add ‘1’ to these values by it’s own.
This setup will provide a frequency of (45000000/255*692) = 255 Hz and we can vary the duty cycle to observe the changes in the sound output.
Keep in mind that if the duty cycle is always 100% (i.e. 255), the output from the microcontroller will behave like digital high output and the buzzer won’t work.

Some Insight into the Code 
uint8_t value = 0;  // the value for the duty cycle

while (value<255)
   {
    htim2.Instance->CCR1 = value;  // vary the duty cycle
    value += 20;  // increase the duty cycle by 20
    HAL_Delay (500);  // wait for 500 ms
   }

   value = 0;   // reset the value

The duty cycle will increase every 500 ms and you can observe the changes in the sound of the buzzer. Watch the video in the last tab to see the changes.

CODE

#include "main.h"
#include "stm32f4xx_hal.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim2;

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM2_Init(void);                                    
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
                                

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

 uint8_t value = 0;  // the value for the duty cycle

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  *
  * @retval None
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_TIM2_Init();
  /* USER CODE BEGIN 2 */

  // turn on the PWM

  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */
   while (value<255)
   {
    htim2.Instance->CCR1 = value;  // vary the duty cycle
    value += 20;  // increase the duty cycle by 20
    HAL_Delay (500);  // wait for 500 ms
   }

   value = 0;   // reset the value
  }
  /* USER CODE END 3 */

}
static void MX_TIM2_Init(void)
{

  TIM_ClockConfigTypeDef sClockSourceConfig;
  TIM_MasterConfigTypeDef sMasterConfig;
  TIM_OC_InitTypeDef sConfigOC;

  htim2.Instance = TIM2;
  htim2.Init.Prescaler = 692-1;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 255-1;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 0;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  HAL_TIM_MspPostInit(&htim2);

}

/** Configure pins as 
        * Analog 
        * Input 
        * Output
        * EVENT_OUT
        * EXTI
*/
static void MX_GPIO_Init(void)
{

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();

}

RESULT



No comments:

Post a Comment

Back to Top