.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"}

For innovators and hobbyists looking to unleash the full potential of their Arduino projects, delving into the realm of wireless technology is essential. Arduino, the compact microcontroller that has revolutionized DIY inventions, can truly flourish

Read More
Exploring Wireless Options for Arduino Projects

I’m excited to introduce you to the latest and greatest version of KiCad – version 8 (stable release). This article will overview the new features and capabilities added to this popular open-source electronic design automation

Read More
KiCad 8: The new and updated features, a full review