Simple buzzer library for STM32 HAL
Simplest and easy-to-use library for using beeper as sound-notification or for debug-purposes.
Active or passive buzzer
Passive buzzer
This buzzer it's just a speaker with resonant frequency 1kHz-3kHz, so it's does not has a built-in oscillator and you should to generate signal manually.
Active buzzer
Active buzzer has built-in built-in oscillator, so it will beep when power is applied with own resonant frequency (also usually 1kHz-3kHz).
Different mounted types
SHT
Through-holes are widely used and I think you've seen beeper, like this, but with wires on old computers.
SMT
Surface mounted beepers does not have a built-in oscillator and can be used for automatic assembly with using pick-and-place machine.
Examples of use
Appliances
- Microwave for encoder rotation alert during the set time and end of warm-up notification
- Washing machine for notification of button presses and the end of the wash
- Multicooker for buttons notification and start and end of cooking
- Fridge for notification of a forgotten open door
Projects
I have used the beeper in many of my DIY-electronics devices.
Electrical circuit
To properly connect buzzer to the GPIO you need to use MOSFET, because buzzer current could reaches 30-40 mA.
VD1 diode should be connected close to beeper, it need to reduce the effect of EMF from back electromagnetic induction
Real example
Now you can see this schematic in hardware on real PCB:
Project in Cube
1) File -> New Project
2) Choose target, I use «Black Pill» module with STM32F401 microcontroller
3) Type any Project name
4) Enable Debugger: SYS -> Debug Serial Wire
5) Click on PC13 or another pin and choose Output Push-Pull. Enter label BUZZER:
In the GPIO configuration choose pull-down and check again Label (it is important for library).
6) Generate the code (Press on Gear icon or Alt+ K):
How to use library?
Get library from GitHub page
You can clone repository or just download it in .zip archive.
➡️ https://github.com/Egoruch/Buzzer-STM32-HAL/tree/1.0.0
Programing
1) Copy buzzer.c to Src, buzzer.h to Inc
2) Include library in main.c
/* USER CODE BEGIN Includes */ #include "buzzer.h" /* USER CODE END Includes */
2) Open stm32f4xx_it.c from Project Explorer
3) Insert HAL_SYSTICK_Callback() into SysTick_Handler:
/** * @brief This function handles System tick timer. */ void SysTick_Handler(void) { /* USER CODE BEGIN SysTick_IRQn 0 */ HAL_SYSTICK_Callback(); /* USER CODE END SysTick_IRQn 0 */ HAL_IncTick(); /* USER CODE BEGIN SysTick_IRQn 1 */ /* USER CODE END SysTick_IRQn 1 */ }
4) Add callback function in main.c
/* USER CODE BEGIN 4 */ /*** SYSTICK (T = 1ms) ***/ /** * @brief SYSTICK callback. * @retval None */ void HAL_SYSTICK_Callback(void) { BUZZER_Handler(); } /* USER CODE END 4 */
5) That's all, now you can use this library:
/* USER CODE BEGIN 2 */ Buzzer_Go(TBUZ_100, TICK_2); HAL_Delay(2000); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { Buzzer_Go(TBUZ_50, TICK_4); HAL_Delay(2000); /* USER CODE END WHILE */
6) You can highlight TBUZ_50 or TICK_4 (its enumerations) and press Open declaration or Ctrl + Left-Click on it:
To see all allowable values (you can add your own values):
|
|
Debug
To start debug session press: Run -> Debug As -> STM32 Cortex-M C/C++ Application
The first time the setup window will open with debug configurations, just click OK.
Wait some time (CubeIDE very slow) and start program executing:
Other application
Except buzzer you can use this library just for generate signal with timeout. So, the another widespread notification component is vibration motor, that installed in every smartphone, so now you can create vibration with this library too:
Work
Bright LED is connected in parallel to buzzer, so I have both light and sound notification.
Video
Conclusions
🤔
- Comments