822 views|3 replies

16

Posts

0

Resources
The OP
 

[Evaluation of STM32L452Nucleo-64] Use of active and passive buzzers [Copy link]

This review will give you a test of the use of the buzzer. When testing the buzzer, I also made some very basic mistakes, which caused me to be stuck for a long time before I found the problem and solved it.

First of all, there are two types of buzzers, active buzzers and passive buzzers as shown below;

Active buzzer
Passive buzzer

The buzzer module is generally composed of a pull-up resistor, a transistor responsible for amplification and a buzzer. The buzzer module used here is powered by 5V. Make sure that the current passing through the buzzer is large enough, otherwise the buzzer cannot be driven.

The main difference between an active buzzer and a passive buzzer is whether there is an oscillation circuit in the buzzer. The sound of a buzzer is generated by the constant change of frequency, which causes the vibration plate to vibrate continuously to produce sound. An active buzzer has an oscillation source inside, so it will sound as soon as it is powered on; while a passive buzzer does not have an oscillation source inside, so it cannot be made to sound if a DC signal is used. It must be driven by a 2K-5K square wave. Therefore, using a passive buzzer will be more complicated when writing code, and you need to write a frequency code yourself.

First we use an active buzzer for demonstration.

First is the configuration of cube. Here we use PA9 port.

Select GPIO output level: High

GPIO mode: Output Push Pull

GPIO Pull-up/Pull-down: Pull-up

Maximum output speed: High

After configuration, you can generate the project file

#include "beep.h"

void BEEP_Init(void)

{

GPIO_InitTypeDef GPIO_InitStruct = {0};



/* GPIO Ports Clock Enable */

__HAL_RCC_GPIOA_CLK_ENABLE();



/*Configure GPIO pin Output Level */

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET);



/*Configure GPIO pin : PD2 */

GPIO_InitStruct.Pin = GPIO_PIN_9;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_PULLUP;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

//BEEP(0); /* 关闭蜂鸣器 */

}

Write a test code in the main program:

while (1)

{



/* USER CODE END WHILE */



/* USER CODE BEGIN 3 */

BEEP_TOGGLE();

delay_ms(1000);

/* USER CODE BEGIN 3 */

/* USER CODE END 3 */

}

The output is inverted every second, and the buzzer output also sounds for one second and stops for one second.

When I was here, I confused the active and passive buzzers. I installed a passive buzzer at first, which made the sound "da-da-da". Because the board does not have an external crystal oscillator, and the clock tree of the L series has an additional MSI clock configuration compared to other models, I once thought it was a problem with the clock tree configuration. It took me a long time to find out that I had installed the wrong buzzer.

But here we also have a problem. The normal frequency band for the passive buzzer to emit sound should be as shown below:

The frequency exchange written in the experiment is too slow and does not meet the conditions for the buzzer to sound. We only need to speed up the frequency to make the passive buzzer sound.

This post is from stm32/stm8

Latest reply

Passive buzzer needs to be given an oscillation source, which can be achieved with pwm, which is simple and convenient. You can also modify the frequency duty cycle to adjust the tone and volume of the sound.   Details Published on 2023-10-31 16:20
 

6841

Posts

11

Resources
2
 
The driver is implemented using pwm, which is simple and convenient. The frequency duty cycle can also be modified to adjust the tone and volume of the sound.
This post is from stm32/stm8
 
 

6788

Posts

2

Resources
3
 

Active buzzers have their own oscillation source, while passive buzzers need to be given an oscillation source.

This post is from stm32/stm8
 
 

11

Posts

0

Resources
4
 

Passive buzzer needs to be given an oscillation source, which can be achieved with pwm, which is simple and convenient. You can also modify the frequency duty cycle to adjust the tone and volume of the sound.

This post is from stm32/stm8
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list