.st0{fill:#FFFFFF;}

Arduino

Arduino basics – How to use the passive buzzer 

 March 22, 2018

By  Peter

Join Our Mailing List

We publish fresh content each week. Read how-to's on Arduino, ESP32, KiCad, Node-RED, drones and more. Listen to interviews. Learn about new tech with our comprehensive reviews. Get discount offers for our courses and books. Interact with our community.

One email per week, no spam, unsubscribe at any time.

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);
}

Tags

Arduino, Buzzer, Passive


You may also like

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

I’m excited to announce a significant update to my course, Arduino Step by Step Getting Serious. This update introduces advanced lectures on using the Wokwi simulator, a powerful tool that enhances your ability to design,

Read More
New Wokwi Simulator Lectures in Arduino Step by Step Getting Serious

I’m thrilled to share a major update to my course, Arduino Step by Step Getting Started. The course now includes a comprehensive section on using online Arduino simulators, making learning and experimenting with Arduino easier

Read More
New Simulator Tutorials in Arduino Step by Step Getting Started