ESP32 KY-012 Active Buzzer Module
Overview
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.
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.
Where to Buy
Prices are subject to change. We earn from qualifying purchases as an Amazon Associate.
Technical Specifications
Pinout Configuration
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.
Troubleshooting Guide
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.
Debugging Tips
🔍 Serial Monitor
Use the Serial Monitor to check for error messages and verify the sensor's output. Add debug prints in your code to track the sensor's state.
⚡ Voltage Checks
Use a multimeter to verify voltage levels and check for continuity in your connections. Ensure the power supply is stable and within the sensor's requirements.
Additional Resources
Code Examples
Arduino Example
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.
ESP-IDF Example
#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.
ESPHome Example
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.
PlatformIO Example
platformio.ini
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino
PlatformIO Example Code
#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.
MicroPython Example
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.
Conclusion
The ESP32 KY-012 Active Buzzer Module is a powerful KY-0xx module sensor that offers excellent performance and reliability. With support for multiple development platforms including Arduino, ESP-IDF, ESPHome, PlatformIO, and MicroPython, it's a versatile choice for your IoT projects.
For optimal performance, ensure proper wiring and follow the recommended configuration for your chosen development platform.
Always verify power supply requirements and pin connections before powering up your project to avoid potential damage.