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

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

Code example Raspberry Pi

Pin assignment Raspberry Pi

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

Programming example in the programming language Python.

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

Turning it on and off produces a tone at the buzzer that roughly corresponds to the frequency of the square wave voltage.

# Required modules are imported and set up
import RPi.GPIO as GPIO
# Here the output pin is declared, to which the buzzer is connected. 
# 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)
# 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)
        print "----------------------------------------"
        print "Current frequency: %d" % Frequency
        Frequency = input("Please enter new Frequency (50-5000):")
# Rework after the program was terminated
except KeyboardInterrupt:

Example program download

To start with the command:

sudo python3

Code example Micro:Bit

Pinout Micro:Bit:

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

This is a MakeCode example for Micro:Bit which essentially does the same as the examples of the other two variants. However, this example is closer to the Raspberry Pi example than the Arduino example.

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.

It should be noted that the sensor may only be connected to pin 0 of the Micro:Bit for PWM controll.

Sample program download