Arduino basics – How to use the passive buzzer

A buzzer is a device that can generate simple tones, typically used to provide audible feedback to the user. Buzzers are used in alarm clocks, keypads, many household appliances and simple toys.

To show you how a buzzer works with the Arduino, let’s do a little experiment. If you have played with an LED following the instructions from the blog post “Arduino basics – how to use a potentiometer” (link to this post), then this experiment will look familiar.

What we’ll do here, is to control the tone of the sound emitted by a buzzer using the same potentiometer. We’ll just replace the LED with the buzzer.

Assembly

The schematic diagram below show the circuit we’ll make:

We will need:

  • An Arduino Uno,
  • A passive buzzer,
  • 7 jumper wires.

We’ll use digital pin 11 to generate Pulse Width Modulation (PWD) pulses that depend on the voltage measured at analog pin 0. This is the voltage controlled by the potentiometer.

As the potentiometer generates a higher voltage, the pulses in digital pin 11 have a longer duty cycle and trigger the buzzer to generate a louder, higher pitch sound. When the potentiometer generates a lower voltage, pin 11 generates a shorter duty cycle pulse which makes the buzzer generate a softer, lower pitch sound.

And here’s the assembled circuit:

Sketch

Copy the sketch to your Arduino IDE:

int potentiometerPin = 0;
int ledPin = 11;
int potentiometerVal = 0;
void setup()
{
	Serial.begin(9600); // setup serial
}
void loop()
{
	potentiometerVal = analogRead(potentiometerPin);
//I use the map function because PWM pins can only accept
//values from 0 to 255. Analog pins can output values from
//0 to 1023. With the map function, the range 0-1023 is
//converted to appropriate values from 0 to 255.
	int mappedVal = map(potentiometerVal,0,1023,0,255);
	Serial.print(potentiometerVal);
	Serial.print(" - ");
	Serial.println(mappedVal);
	analogWrite(ledPin,mappedVal);
	delay(10);
}