Controlled with PWM signals of different frequencies, the passive piezo buzzer can be used to generate different sounds.

Operating voltage 3,3 V - 5 V
Tone generation range 1,5 kHz - 2,5 kHz
Dimensions 18,5 x 15 mm

Pin assignment

Code example Arduino

Pin assignment Arduino

Arduino Sensor
Pin 8 Signal
- +V
Ground GND

This is an example program which generates an alarm signal at the buzzer using a square wave voltage.

int buzzer = 8 ; // Declaration of the buzzer output pin
 
void setup ()
{
  pinMode (buzzer, OUTPUT) ;// Initialize as output pin
}
 
 
void loop ()
{
  unsigned char i;
  while (1)
  {
    // In this program, the buzzer is controlled alternately with two different frequencies.
    // The signal consists of a square wave voltage.
    // Turning the buzzer on and off will generate a tone that roughly corresponds to the frequency.
    // The frequency is defined by the length of the on and off phase.
     
    //Frequency 1
    for (i = 0; i <80; i++) 
    {
      digitalWrite (buzzer, HIGH) ;
      delay (1) ;
      digitalWrite (buzzer, LOW) ;
      delay (1) ;
    }
    //Frequency 2
    for (i = 0; i <100; i++) 
    {
      digitalWrite (buzzer, HIGH) ;
      delay (2) ;
      digitalWrite (buzzer, LOW) ;
      delay (2) ;
    }
  }
}

Sample program download

KY006-Arduino.zip

Code example Raspberry Pi

Pin assignment Raspberry Pi

Raspberry Pi Sensor
GPIO 24 [Pin 18] Signal
3.3 V [Pin 1] * +V *
Ground [Pin 6] GND

To prevent the supply voltage from dropping, the sensor on the Raspberry Pi must also be connected to +3.3V, since the supply via the signal pin may not be sufficient.

The example program uses software PWM to create a square wave voltage with definable frequency at the output pin.

By switching on and off, a tone is generated at the buzzer that corresponds approximately to the frequency of the square wave voltage.

# Required modules are imported and set up
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
 
 
# Here the output pin is declared, to which the buzzer is connected. 
GPIO_PIN = 24
GPIO.setup(GPIO_PIN, GPIO.OUT)
 
# The software PWM module is initialized - here the frequency 500Hz is taken as start value 
Frequency = 500 #in Hertz
pwm = GPIO.PWM(GPIO_PIN, Frequency)
pwm.start(50)
 
# The program waits for the user to enter a new PWM frequency.
# Until then the buzzer is operated with the previously entered frequency (start value 500Hz)
try:
    while(True):
        print ("----------------------------------------")
        print ("Current frequency: %d" % Frequency)
        Frequency = input("Please enter new Frequency (50-5000):")
        pwm.ChangeFrequency(Frequency)
         
# Rework after the program was terminated
except KeyboardInterrupt:
    GPIO.cleanup()

Example program download

KY006-RPi.zip

To start with the command:

sudo python3 KY006-RPi.py

Code example Micro:Bit

Pinout Micro:Bit:

Micro:Bit Sensor
Pin 0 Signal
3 V +V
Ground GND

Since this sensor is controlled by PWM it has to be connected to pin 0 of the Micro:Bit since that is the PWM pin of the Micro:Bit.

Sample program download

microbit-KY-006.zip