Simple buzzer library for STM32 HAL

Image

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.

Image

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).

Image

Different mounted types

SHT

Through-holes are widely used and I think you've seen beeper, like this, but with wires on old computers.

Image

SMT

Surface mounted beepers does not have a built-in oscillator and can be used for automatic assembly with using pick-and-place machine.

Image

Examples of use

Appliances

Image

  • 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. 

Image

Electrical circuit

To properly connect buzzer to the GPIO you need to use MOSFET, because buzzer current could reaches 30-40 mA.

Image

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:

Image

Project in Cube

1) File -> New Project

Image

2) Choose target, I use «Black Pill» module with STM32F401 microcontroller

Image

3) Type any Project name

Image

4) Enable Debugger: SYS -> Debug Serial Wire

Image

 5) Click on PC13 or another pin and choose Output Push-Pull. Enter label BUZZER:

Image

In the GPIO configuration choose pull-down and check again Label (it is important for library).

Image

6) Generate the code (Press on Gear icon or Alt+ K):

Image

How to use library?

Get library from GitHub page

Image

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

Image

2) Include library in main.c

  1.  
  2. /* USER CODE BEGIN Includes */
  3. #include "buzzer.h"
  4. /* USER CODE END Includes */
  5.  


2) Open  stm32f4xx_it.c from Project Explorer

Image

3) Insert HAL_SYSTICK_Callback() into SysTick_Handler:

  1.  
  2. /**
  3.   * @brief This function handles System tick timer.
  4.   */
  5. void SysTick_Handler(void)
  6. {
  7. /* USER CODE BEGIN SysTick_IRQn 0 */
  8. HAL_SYSTICK_Callback();
  9. /* USER CODE END SysTick_IRQn 0 */
  10. HAL_IncTick();
  11. /* USER CODE BEGIN SysTick_IRQn 1 */
  12. /* USER CODE END SysTick_IRQn 1 */
  13. }
  14.  

4) Add callback function in main.c

  1.  
  2. /* USER CODE BEGIN 4 */
  3. /*** SYSTICK (T = 1ms) ***/
  4. /**
  5.   * @brief SYSTICK callback.
  6.   * @retval None
  7.   */
  8. void HAL_SYSTICK_Callback(void)
  9. {
  10. BUZZER_Handler();
  11. }
  12. /* USER CODE END 4 */
  13.  

5) That's all, now you can use this library:

  1.  
  2. /* USER CODE BEGIN 2 */
  3. Buzzer_Go(TBUZ_100, TICK_2);
  4. HAL_Delay(2000);
  5. /* USER CODE END 2 */
  6. /* Infinite loop */
  7. /* USER CODE BEGIN WHILE */
  8. while (1)
  9. {
  10. Buzzer_Go(TBUZ_50, TICK_4);
  11. HAL_Delay(2000);
  12. /* USER CODE END WHILE */
  13.  

6) You can highlight TBUZ_50 or TICK_4 (its enumerations) and press Open declaration or Ctrl + Left-Click on it:

Image

To see all allowable values (you can add your own values):

  1.  
  2. typedef enum BuzzerTime
  3. {
  4. TBUZ_20 = 20,
  5. TBUZ_50 = 50,
  6. TBUZ_100 = 100,
  7. TBUZ_200 = 200,
  8. TBUZ_300 = 300,
  9. TBUZ_400 = 400,
  10. TBUZ_500 = 500,
  11. TBUZ_600 = 600,
  12. TBUZ_800 = 800,
  13. TBUZ_900 = 900,
  14. TBUZ_1000 = 1000,
  15. }buztime_t;
  16.  
  1.  
  2. typedef enum BuzzerTicks
  3. {
  4. TICK_1 = 2-1,
  5. TICK_2 = 4-1,
  6. TICK_4 = 8-1,
  7. TICK_6 = 12-1,
  8. TICK_8 = 16-1,
  9. TICK_10 = 20-1,
  10. TICK_12 = 24-1,
  11. }buztick_t;
  12.  

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.

Image

Wait some time (CubeIDE very slow) and start program executing:

Image

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:

Image

Work

Bright LED is connected in parallel to buzzer, so I have both light and sound notification.

Image

Video

Conclusions

🤔

788
No comments yet. Be the first to add a comment!
Cookies?