In this guide, we will demonstrate how to interface a buzzer with an ESP32 microcontroller using the ESP-IDF and the VS Code extension. The buzzer will produce a beeping sound for 1 second and then turn off for 1 second in a continuous loop.
The objective is to control the beep of the buzzer using ESP-IDF on your ESP32.
To successfully complete this project, you will need the following components:
ESP32 development board
Buzzer
Jumper wires
Breadboard
Follow the circuit diagram below to connect the Buzzer to the ESP32 development board:
Now let's understand and write the C code to control the LED. Follow the steps below:
Open VS Code and go to View > Command Palette (Ctrl + Shift + P). Type "ESP-IDF: New Project" in the search bar and press Enter.
2. Specify the project name and directory. Choose a meaningful name for your project, such as "Buzzer". Choose the ESP-IDF board by selecting the "ESP-WROVER-KIT 3.3V" option. Click the "Choose Template" button to proceed.
3. In the Extensions view, select the ESP-IDF option. You will see a list of available templates. Under the "get-started" tab, click on the "sample_project" template.
4. Then click "Create project using template sample_project".
5. A notification will appear, indicating that the project has been created. You can choose to open the project in a new window by clicking "Yes".
6. Now navigate to the "Main" folder and then open the Main.c file.
7. You might also see other files like CMakeLists.txt this is the config file for building the project the SRCS "main.c" part tells the CMake compiler about source files of the project, similarly INCLUDE_DIRS "." part is for any header files that would be used.
8. Copy and paste the following code into Main.c or download it from here:
#include <stdio.h>
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#define BUZZER_PIN GPIO_NUM_4
void app_main(void)
{
// Code for buzzer
gpio_set_direction(BUZZER_PIN, GPIO_MODE_OUTPUT);
gpio_set_level(BUZZER_PIN, 1);
while (1)
{
// Beep the buzzer for 1 second
gpio_set_level(BUZZER_PIN, 0);
vTaskDelay(1000 / portTICK_PERIOD_MS);
// Turn off the buzzer for 1 second
gpio_set_level(BUZZER_PIN, 1);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
9. Now at the bottom navbar of VS Code select the port of your ESP32 on the left side.
10. After selecting port select the target device besides port, now select the flash method by clicking on star icon and select the UART option and the workspace.
11. Finally, it's time to compile and flash the code. Click on the fire like icon in the bottom navbar. Your project will start compiling
Note: During the flashing process of your board, you may encounter an error. If this happens, there's no need to worry. Simply click the Build, Flash, and Monitor icon again. When you see the message "connecting..........." appear, press and hold the boot button on your board for 1 or 2 seconds. Keep holding it until the dots on the screen start disappearing or the further flashing process begins. This action will help resolve any issues and ensure a successful flashing of your board.
After flashing the code to the ESP32, the buzzer will produce a sound for 1 second, followed by a 1-second silent interval. This pattern will repeat continuously.
By following this guide, you have successfully interfaced a buzzer with an ESP32 using the ESP-IDF and the VS Code extension. This project can be useful for various applications such as alarm systems, notifications, and user feedback.