Skip to content

Interfacing Buzzer with the STM32-F103RB Nucleo Board

Introduction

In this guide, we will explore how to interface a buzzer with the STM32-F103RB Nucleo Board, allowing you to generate audible alerts or tones in your projects. The STM32-F103RB Nucleo Board is a powerful microcontroller development board that offers a wide range of capabilities. One such capability is the ability to interface with external components like buzzers.

Problem Statement

The objective of this project is to interface a buzzer with the STM32-F103RB Nucleo Board and generate sound output. By implementing this functionality, you will gain practical experience in connecting external components to the board and controlling them using GPIO pins.

Requirements

To successfully complete this project, you will need the following components:

  1. Nucleo-F103RB board
  2. Buzzer
  3. Jumper wires
  4. Breadboard

Circuit Diagram

Follow the circuit diagram below to properly connect the buzzer to the STM32-F103RB :

  1. Connect the signal pin of the buzzer to PC8 of the Nucleo board.
  2. Connect the other power pins of buzzer to the Nucleo board.
 STM 32

Project Configuration

Follow these steps to configure the .IOC file and set up GPIOs and Clock :

  1. Open the STM32 Cube IDE and start by creating a new STM32 project. Click on the board selector tab in the target selection window.
  2. Enter "NUCLEO-F103RB" as the commercial part number and click on "Next" after selecting the board.
 STM 32
  1. Enter the Project Name "buzzer" and click on "Finish". After that, click on "Yes" to initialize all the peripherals in default mode.
 STM 32
  1. Pinout & Configuration: To configure the buzzer (PC8) on the STM32-F103RB Nucleo Board, navigate to the "Pinout & Configuration" tab. Select the "GPIO" tab in the System Core section.

  2. To configure the GPIO pin PC8 for output, click on the GPIO pin in the Pinout & Configuration tab. Select the "GPIO_Output" option from the available options. To add a user label, right-click on the GPIO pin PC8 and select the "User Label" option and enter the desired name for the label, in this case "BUZZ".

  3. Configure the PC8 pin as follows:

  • Select "Output level" as Low (This will keep the GPIO Low initially).
  • Choose push-pull GPIO mode.
  • Keep the GPIO Pull-Up/Pull-Down as No Pull-up or Pull-down.
  • Set the maximum output speed as low.
 STM 32
  1. After finishing the Pinout Configuration, go to the Clock Configuration menu. In the HCLK field, enter "72 MHz" as the desired frequency for the HCLK (system clock). The Integrated Development Environment (IDE) will automatically search for the necessary clock sources and prescalers based on this configuration.

  2. Next, locate the gear-like icon on the toolbar and click on it. This will add the configuration settings we did in the GUI to the code.

 STM 32

Code

Now, let's proceed to understand and write the C code to control the beep of the Buzzer. Follow the steps below:

  1. The Device Configuration Tool will automatically open the main.c file for you. In the project, you will find all the necessary pin and port names defined in the Core ⇾ Inc ⇾ main.h file. These definitions will help us easily reference the pins and ports in our code.

  2. Navigate to the main function in the main.c file and paste the following code:

Note: You can download the full project code from the provided link.

c
int main(void)
{

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

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

    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_USART2_UART_Init();

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
        /*USER CODE BEGIN 3 */
        HAL_GPIO_TogglePin(BUZZ_GPIO_Port, BUZZ_Pin); // Toggle the Buzzer state
        // Delay for a short period
        HAL_Delay(500);
    }
    /* USER CODE END 3 */
}
  1. After pasting the above code, ensure that you have connected your STM32-F103RB Nucleo Board to your PC/Laptop using a USB cable.

  2. Next, click on the "Run and Debug" icon in the toolbar menu of the IDE (Refer to image below ). This will initiate the build process for your project. The IDE will compile the code. Once the build process is successful, the IDE will start flashing the compiled project to the STM32-F103RB Nucleo Board.

 STM 32

Output

Upon successful implementation of the project, the buzzer connected to pin PC8 of the STM32-F103RB Nucleo Board will produce a continuous sound. The buzzer will alternate between being on and off every 500 milliseconds, creating an audible tone.

Conclusion

By following this guide, you have successfully interfaced a buzzer with the STM32-F103RB Nucleo Board. The buzzer generates a continuous sound with alternating on and off states, allowing you to incorporate audible alerts or tones in your projects. You now have the knowledge and skills to integrate buzzers for sound output and explore various audio effects using the GPIO functionality of the STM32-F103RB Nucleo Board.