• Arduino
  • Raspberry Pi
  • Raspberry Pi Pico
  • Micro:Bit

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
GND 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

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 Raspberry Pi

Pin assignment Raspberry Pi

Raspberry Pi Sensor
GPIO 24 [Pin 18] Signal
3.3 V [Pin 1] * +V *
GND [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

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 Micro:Bit

Pinout Micro:Bit:

Micro:Bit Sensor
Pin 0 Signal
3 V +V
GND 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

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 Raspberry Pi Pico

Pin assignment Raspberry Pi Pico

Raspberry Pi Pico Sensor
GPIO15 Signal
- +V
GND GND

This is a sample program that plays a song at the buzzer.

# Load libraries
from machine import Pin, PWM
from utime import sleep

# Initialization of GPIO15 as PWM pin
buzzer = PWM(Pin(15))

# Defining the different pitches
tones = {
"B0": 31,
"C1": 33,
"CS1": 35,
"D1": 37,
"DS1": 39,
"E1": 41,
"F1": 44,
"FS1": 46,
"G1": 49,
"GS1": 52,
"A1": 55,
"AS1": 58,
"B1": 62,
"C2": 65,
"CS2": 69,
"D2": 73,
"DS2": 78,
"E2": 82,
"F2": 87,
"FS2": 93,
"G2": 98,
"GS2": 104,
"A2": 110,
"AS2": 117,
"B2": 123,
"C3": 131,
"CS3": 139,
"D3": 147,
"DS3": 156,
"E3": 165,
"F3": 175,
"FS3": 185,
"G3": 196,
"GS3": 208,
"A3": 220,
"AS3": 233,
"B3": 247,
"C4": 262,
"CS4": 277,
"D4": 294,
"DS4": 311,
"E4": 330,
"F4": 349,
"FS4": 370,
"G4": 392,
"GS4": 415,
"A4": 440,
"AS4": 466,
"B4": 494,
"C5": 523,
"CS5": 554,
"D5": 587,
"DS5": 622,
"E5": 659,
"F5": 698,
"FS5": 740,
"G5": 784,
"GS5": 831,
"A5": 880,
"AS5": 932,
"B5": 988,
"C6": 1047,
"CS6": 1109,
"D6": 1175,
"DS6": 1245,
"E6": 1319,
"F6": 1397,
"FS6": 1480,
"G6": 1568,
"GS6": 1661,
"A6": 1760,
"AS6": 1865,
"B6": 1976,
"C7": 2093,
"CS7": 2217,
"D7": 2349,
"DS7": 2489,
"E7": 2637,
"F7": 2794,
"FS7": 2960,
"G7": 3136,
"GS7": 3322,
"A7": 3520,
"AS7": 3729,
"B7": 3951,
"C8": 4186,
"CS8": 4435,
"D8": 4699,
"DS8": 4978
}

# Defining the song to be played in a list
song = ["E5","G5","A5","P","E5","G5","B5","A5","P","E5","G5","A5","P","G5","E5"]

# Function: Setting the frequency to be output
def playtone(frequency):
    buzzer.duty_u16(1000)
    buzzer.freq(frequency)

# Function: Switch off the buzzer
def bequiet():
    buzzer.duty_u16(0)

# Function: Output of the song to be played
def playsong(mysong):
    for i in range(len(mysong)):
        if (mysong[i] == "P"):
            bequiet()
        else:
            playtone(tones[mysong[i]])
        sleep(0.3)
    bequiet()

# Endless output of the defined song
playsong(song)

Example program download

KY006-Pico.zip