Skip to main content
ESPBoards

ESP32 KY-012 Active Buzzer Module

The KY-012 is an active piezoelectric buzzer module that emits a 2.5 kHz tone when powered. It operates within a voltage range of 3.3V to 5V and is ideal for applications requiring audible alerts.

⬇️ Jump to Code Examples

Arduino Core Image
ESP-IDF Image
ESPHome Image
PlatformIO Image
MicroPython Image

🔗 Quick Links

KY-012 Active Buzzer Module Datasheet ButtonKY-012 Active Buzzer Module Specs ButtonKY-012 Active Buzzer Module Specs ButtonKY-012 Active Buzzer Module Specs Button

🛒 KY-012 Price

KY-012 Active Buzzer Module
Normally, the KY-012 Active Buzzer Module costs around 1$ per Psc.
The prices are subject to change. Check current price:

Amazon com

Amazon de logo

Aliexpress logo

ℹ️ About KY-012 Active Buzzer Module

The KY-012 Active Buzzer Module is equipped with an active piezoelectric buzzer that emits a tone at approximately 2.5 kHz when powered. Unlike passive buzzers, this module does not require an external square wave signal; it generates sound as soon as a voltage of at least 3.3V is applied to its signal pin. Operating within a voltage range of 3.3V to 5V and drawing a maximum current of 30 mA, it produces a minimum sound output of 85 dB. This makes it suitable for applications requiring audible alerts, such as alarm systems and user notifications.

⚙️ KY-012 Sensor Technical Specifications

Below you can see the KY-012 Active Buzzer Module Technical Specifications. The sensor is compatible with the ESP32, operating within a voltage range suitable for microcontrollers. For precise details about its features, specifications, and usage, refer to the sensor’s datasheet.

  • Type: module
  • Protocol: Digital
  • Operating Voltage: 3.3V - 5V
  • Maximum Current: 30 mA
  • Tone Frequency: 2.5 kHz ± 300 Hz
  • Minimum Sound Output: 85 dB
  • Operating Temperature: -20°C to 70°C
  • Storage Temperature: -30°C to 105°C
  • Dimensions: 19 x 15.5 x 11 mm

🔌 KY-012 Sensor Pinout

Below you can see the pinout for the KY-012 Active Buzzer Module. The VCC pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND pin is the ground connection and must be connected to the ground of your ESP32!

  • Pin (-): Ground (GND).
  • Pin (middle): Not connected.
  • Pin (S): Signal input.

🧵 KY-012 Wiring with ESP32

Below you can see the wiring for the KY-012 Active Buzzer Module with the ESP32. Connect the VCC pin of the sensor to the 3.3V pin on the ESP32 or external power supply for power and the GND pin of the sensor to the GND pin of the ESP32. Depending on the communication protocol of the sensor (e.g., I2C, SPI, UART, or analog), connect the appropriate data and clock or signal pins to compatible GPIO pins on the ESP32, as shown below in the wiring diagram.

  • KY-012 Pin (-): Connect to ESP32 GND.
  • KY-012 Pin (S): Connect to ESP32 GPIO (e.g., GPIO15).

🛠️ KY-012 Active Buzzer Module Troubleshooting

This guide outlines a systematic approach to troubleshoot and resolve common problems with the . Start by confirming that the hardware connections are correct, as wiring mistakes are the most frequent cause of issues. If you are sure the connections are correct, follow the below steps to debug common issues.

❌ No Sound Emitted

Issue: The buzzer does not produce any sound when expected.

Solutions:

  • Ensure the module is properly connected to the microcontroller, with the correct pins assigned.
  • Verify that the signal pin is receiving the appropriate voltage (at least 3.3V).
  • Check for any soldering issues or loose connections on the module.

🔊 Continuous Sound

Issue: The buzzer emits a continuous sound when it shouldn't.

Solutions:

  • Ensure that the signal pin is not inadvertently set to HIGH in the code.
  • Check for any short circuits or unintended connections that might cause the signal pin to remain HIGH.

💻 Code Examples

Below you can find code examples of KY-012 Active Buzzer Module with ESP32 in several frameworks:

If you encounter issues while using the KY-012 Active Buzzer Module, check the Common Issues Troubleshooting Guide.

Arduino Core Image

ESP32 KY-012 Arduino IDE Code Example

Example in Arduino IDE

Fill in your main Arduino IDE sketch file with the following code to use the KY-012 Active Buzzer Module:

int buzzer = 13;

void setup() {
pinMode(buzzer, OUTPUT); // Initialize the buzzer pin as an output
}

void loop() {
digitalWrite(buzzer, HIGH); // Turn the buzzer on
delay(4000); // Wait for 4 seconds
digitalWrite(buzzer, LOW); // Turn the buzzer off
delay(2000); // Wait for 2 seconds
}

This Arduino code alternates the KY-012 buzzer on and off. The buzzer is connected to pin 13, which is set as an output. The buzzer is turned on for 4 seconds and then off for 2 seconds in a continuous loop.

Connect your ESP32 to your computer via a USB cable, Ensure the correct Board and Port are selected under Tools, Click the "Upload" button in the Arduino IDE to compile and upload the code to your ESP32.

ESP-IDF Image

ESP32 KY-012 ESP-IDF Code Example
Example in Espressif IoT Framework (ESP-IDF)

If you're using ESP-IDF to work with the KY-012 Active Buzzer Module, here's how you can set it up and read data from the sensor. Fill in this code in the main ESP-IDF file:

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"

#define BUZZER_PIN GPIO_NUM_15

void app_main(void) {
gpio_config_t io_conf = {
.pin_bit_mask = (1ULL << BUZZER_PIN),
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE
};
gpio_config(&io_conf);

while (1) {
gpio_set_level(BUZZER_PIN, 1); // Turn the buzzer on
vTaskDelay(pdMS_TO_TICKS(4000)); // Wait for 4 seconds
gpio_set_level(BUZZER_PIN, 0); // Turn the buzzer off
vTaskDelay(pdMS_TO_TICKS(2000)); // Wait for 2 seconds
}
}

This ESP-IDF code configures GPIO15 as an output to control the KY-012 active buzzer. The buzzer is turned on for 4 seconds and then off for 2 seconds in a loop.

Update the I2C pins (I2C_MASTER_SDA_IO and I2C_MASTER_SCL_IO) to match your ESP32 hardware setup, Use idf.py build to compile the project, Use idf.py flash to upload the code to your ESP32.

ESPHome Image

ESP32 KY-012 ESPHome Code Example

Example in ESPHome (Home Assistant)

Fill in this configuration in your ESPHome YAML configuration file (example.yml) to integrate the KY-012 Active Buzzer Module

output:
- platform: gpio
pin: GPIO15
id: buzzer_output

switch:
- platform: output
name: "KY-012 Buzzer"
output: buzzer_output
turn_on_action:
- output.turn_on: buzzer_output
- delay: 4s
- output.turn_off: buzzer_output
turn_off_action:
- output.turn_off: buzzer_output

This ESPHome configuration sets up the KY-012 active buzzer on GPIO15 using a simple GPIO switch. The buzzer can be toggled on and off through the ESPHome interface.

Upload this code to your ESP32 using the ESPHome dashboard or the esphome run command.

PlatformIO Image

ESP32 KY-012 PlatformIO Code Example

Example in PlatformIO Framework

For PlatformIO, make sure to configure the platformio.ini file with the appropriate environment and libraries, and then proceed with the code.

Configure platformio.ini

First, your platformio.ini should look like below. You might need to include some libraries as shown. Make sure to change the board to your ESP32:

[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino

ESP32 KY-012 PlatformIO Example Code

Write this code in your PlatformIO project under the src/main.cpp file to use the KY-012 Active Buzzer Module:

#include <Arduino.h>

#define BUZZER_PIN 15

void setup() {
pinMode(BUZZER_PIN, OUTPUT);
}

void loop() {
digitalWrite(BUZZER_PIN, HIGH); // Turn buzzer on
delay(4000);
digitalWrite(BUZZER_PIN, LOW); // Turn buzzer off
delay(2000);
}

This PlatformIO code configures GPIO15 as an output to control the KY-012 active buzzer. The buzzer is turned on for 4 seconds and off for 2 seconds in an alternating cycle.

Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload command.

MicroPython Image

ESP32 KY-012 MicroPython Code Example

Example in Micro Python Framework

Fill in this script in your MicroPython main.py file (main.py) to integrate the KY-012 Active Buzzer Module with your ESP32.

import machine
import time

BUZZER_PIN = machine.Pin(15, machine.Pin.OUT)

while True:
BUZZER_PIN.value(1) # Turn buzzer on
time.sleep(4) # Wait for 4 seconds
BUZZER_PIN.value(0) # Turn buzzer off
time.sleep(2) # Wait for 2 seconds

This MicroPython script configures GPIO15 as an output for the KY-012 active buzzer. It alternates the buzzer on and off in a cycle of 4 seconds on and 2 seconds off.

Upload this code to your ESP32 using a MicroPython-compatible IDE, such as Thonny, uPyCraft, or tools like ampy.

Conclusion

We went through technical specifications of KY-012 Active Buzzer Module, its pinout, connection with ESP32 and KY-012 Active Buzzer Module code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.