Engineers Garage

  • Electronics Projects and Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

Arduino compatible coding 21: Interfacing buzzers with Arduino

By Nikhil Agnihotri October 9, 2020

In the previous tutorials, we learned about serial communication using the SPI (serial peripheral interface) and seven-segment multiplexing using the MAX7219 IC. We’ve also covered digital I/O, analog input, PWM (pulse width modulation), and all of the serial communication protocols commonly used by embedded controllers (the UART, I2C, and SPI). 

This completes the basic coverage of the Arduino platform. With the above-mentioned skillsets, we can now learn about interfacing various sensors, modules, as well as input and output devices with Arduino. 

In this tutorial, we’ll learn how to interface a buzzer/beeper with Arduino. 

Buzzers & beepers
Buzzers are audio signaling devices that generate sound in the frequency range of 1~7 kHz and, typically, serve as an indicator. This frequency range is within the hearing threshold, which is why a buzzer can usually be heard in noisy environments. 

There are many types of buzzers, including piezoelectric, magnetic, electromagnetic, mechanical, and electromechanical buzzers. The piezoelectric and magnetic buzzers are most widely used in the electronics industry. 

Buzzers can be designed to operate as a transducer or an indicator. A transducer buzzer has no built-in driving circuit. It can be used to generate tones of various frequencies using an external driving circuit. A transducer buzzer is often actuated by a square wave input, where the sound of the buzzer can be controlled by the frequency of this wave. Several sound frequencies can be played by applying a different square wave input to the buzzer. 

The indicator buzzer has a built-in driving circuit that only lets it play a particular frequency or set of frequencies. These types of buzzers can be actuated by applying a DC voltage to their input. 

In electronic applications (~12V), the indicator buzzers can be actuated directly from: 

  • A controller or computer’s digital I/O pin 
  • A controller or computer’s digital I/O via a transistor or an amplifier circuit

Regardless of the type, a buzzer is always a two-terminal device. One of these terminals is the ground, which should be connected to the common ground of the circuit. The other is the input pin, which should be supplied by DC voltage or a square wave input, depending on its type. 

Note: even if a square wave input is applied to an indicator buzzer, it will remain actuated for TON and will remain muted for TOFF. 

Piezoelectric buzzers
Piezoelectric buzzers have a piezo-electric ceramic. The ceramic is attached to a metal plate and will shrink and stretch diametrically when an alternating current is applied to it. When the ceramic shrinks and stretches rapidly, it produces vibrations of the same frequency as the attached metal plate. The buzzer will sound the same frequency. 

The piezo buzzers have a wide operating voltage that ranges from 3 to 250V. Most of the piezo buzzers that are used in electronic circuits have an operating voltage between 3 and 12V, with high sound-pressure levels. They also have a low current consumption (~30 mA). Piezo buzzers offer a large footprint and are typically used in cost-sensitive electronic applications. 

Magnetic buzzers
Magnetic buzzers have a ferromagnetic disc that’s attached to a pole. Magnets are attached around the pole and a coil below the ferromagnetic disc act as an electromagnet. When a current is supplied to the coil, the disc pulls toward the coil. If there’s no current, the disc remains in its rest position. 

The vibrations of the disc are controlled by a weight that’s above it. When an oscillating signal is applied to the coil, the electromagnetic field generated from it also fluctuates, causing vibrations in the ferromagnetic disk. The buzzer produces the same frequency of sound like the one applied by the oscillating signal. 

Magnetic buzzers have a narrow operating voltage that ranges between 1 to 16V. These buzzers produce lower-rated frequencies, with low sound-pressure levels. However, they have a slightly greater current consumption (up to 100 mA) compared to the piezo buzzers. Magnetic buzzers offer a small footprint and are, typically, used in high-end consumer applications.       

The applications of a buzzer
Buzzers can generate various sounds, such as clicking, beeping, ringing, chiming, and that of a siren. They can be used as an alarm, a warning, or as an indicator (such as the buzzer to indicate the end of classroom time in high school or the start of a sporting event). 

Buzzers are also found in alarm devices (such as alarm clocks or fire alarms), timer devices, electronic metronomes, annunciator panels, computer peripherals (motherboards, keyboards, and a mouse), as well as in many consumer electronics applications. Learn about buzzers in detail from here.

The tone() function
Arduino provides a function tone() that can be used to produce the square wave output from a digital I/O pin. This function is declared in the Arduino.h file and defined in the Tone.cpp file. 

The tone() function is one of the basic, built-in Arduino functions such as the pinMode(), digitalWrite(), digitalRead(), analogRead(), or analogWrite(), and so on. Both the Arduino.h and Tone.cpp files are included in Arduino’s core libraries and can be inspected from the Arduino\hardware\arduino\avr\cores\arduino folder. 

The tone() function is used to generate the square wave input at the digital I/O pin. Therefore, it can be used to actuate a buzzer. It has this syntax: 

void tone(pin, frequency, duration = 0)

It also takes three parameters:

1. The digital I/O pin, where the square wave has to be generated
2. The frequency of the square wave in Hertz
3. The duration in milliseconds, where the square wave has to be generated. This third parameter is optional. 

Here’s a valid example of this method:

tone(9, 1000); // 1KHz Square Wave from pin 9

This function uses one of the built-in timers from Arduino’s AVR controller to generate the square wave signal. As such, the delay() function will have no effect on it. To stop the square wave output, the duration must be passed as a parameter — assuming the output is for a fixed period of time. Otherwise, the noTone() function should be called.  

The noTone() function
This function disables the square wave output from Arduino’s pin. It has this syntax:

noTone(pin)

It takes only one parameter, which is the digital I/O pin where the tone has to be disabled. This is a valid example:

noTone(9);

It will stop the tone at the given pin, disable the timer, and set the given pin to LOW.

Interfacing the buzzer with Arduino 
It’s quite simple to interface a buzzer/beeper with Arduino. All buzzers have two terminals and one is marked by a positive sign. This is the input terminal. 

Be sure to check the specifications of the buzzer before use to ensure it can be driven directly by a 5V digital I/O pin. It may require a higher voltage or an additional transistor circuitry. Most buzzers designed for electronics applications can be directly actuated by 5V and 3V3 pins. 

The positive terminal of the buzzer should be connected directly to Arduino’s digital I/O or through a transistor circuit. Keep the other terminal grounded. 

It’s also important to place a current-limiting resistor in series with the buzzer. This is because the buzzer sinks current from the controller pin and can potentially damage it via a leak or surge. So, a small-value resistor must be placed in series with the buzzer and the controller pin to avoid any possible damage — and even if the buzzer’s specific part number is unknown. 

Nearly all buzzer modules have a series resistor. If the part number of the buzzer transducer is known, the value of the current limiting resistance can be found from the datasheet. 

So, let’s suppose that a buzzer has an operating voltage of 5V, a maximum current rating of 40 mA, and a coil resistance of 47 Ω. To be safe in this case, we’d allow the current driving the buzzer to be maintained at 20 mA. This means that using a 5V operating voltage and a 20-mA current, the resistance across the buzzer should be 250 Ω (R = V/I). And, as the coil resistance is given 47 Ω, the current limiting resistor should be at least 200 Ω. 

Generating a beep
A buzzer’s beep sounds can be generated by periodically actuating and deactivating the buzzer. In Arduino, this can be done by calling the tone() function, with a given duration, and then calling the delay() function for a greater duration. Otherwise, the tone() and noTone() functions can be called alternatively, with some delays. 

Recipe: Interfacing a buzzer with Arduino
In this recipe, we will interface a buzzer with Arduino and generate a beep sound from it.

Components required

1. Arduino Uno x1
2. Buzzer x1
3. Resistor 100Ω x1
4. Breadboard x1
5. Jumper/connecting wires

Circuit connections

  • Connect Arduino UNO’s pin 9 to one of the terminals of the 100Ω resistor
  • Connect the other terminal of the resistor to the positive terminal of the buzzer
  • Connect the other terminal of the buzzer to the ground

Arduino sketch 

const int buzzer = 9;
void setup(){
  pinMode(buzzer, OUTPUT);
}
void loop()
{
  tone(buzzer, 1000); // 1KHz
  delay(1000);       
  noTone(buzzer);     // Stop
  delay(1000);      
}   

Alternatively, this sketch can be used:

const int buzzer = 9;
void setup(){
  pinMode(buzzer, OUTPUT);
}
void loop()
{
  tone(buzzer, 1000, 1000); // 1KHz
  delay(2000);       
}  

How the circuit works
A buzzer transducer is interfaced with the Arduino UNO. When the square wave is input to the positive terminal of the buzzer, it will generate a sound of the same frequency.

A square wave of 1 kHz is applied to the buzzer’s terminal. The square wave is applied and deactivated using the tone() and noTone() functions. This produces a beep sound from the buzzer.

Alternatively, the tone() function can be called with a given duration, and the delay() function (with a greater duration) can be called to create the same sound effect.

The higher the frequency of the input signal, the higher pitch of the buzzer sound.

Programming guide
The Arduino sketch begins by declaring a variable to store the pin number with which the buzzer is interfaced.

In the setup() function, this pin is set as the digital output by using the pinMode() function. In the loop() function, the tone() and noTone() functions are called on for the same pin so as to generate the buzzer sound.

Alternatively, it’s possible to call the tone() function with a given duration and the delay() function, with greater duration, to create the same sound effect.

As the loop() keeps iterating, the buzzer periodically beeps.

xf
In the next tutorial, we’ll learn how to interface a matrix keypad with Arduino.

 


Filed Under: Arduino Projects, Microcontroller Projects
Tagged With: Arduino
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Colpitts oscillator
  • problem identifying pin purpose on PMA5-83-2WC+ amplifier
  • Voltage Regulator Sizing Question
  • Genetic algorithm code in matlab for cost optimization
  • SDR as wideband spectrum analyzer

RSS Electro-Tech-Online.com Discussions

  • Electronic board faulty?!?
  • Can I use this charger in every country?
  • using a RTC in SF basic
  • An Update On Tarrifs
  • Wish to buy Battery, Charger and Buck converter for 12V , 2A router

Featured – Designing of Audio Amplifiers part 9 series

  • Basics of Audio Amplifier – 1/9
  • Designing 250 Milli Watt Audio Power Amplifier – 2/9
  • Designing 1 Watt Audio Power Amplifier – 3/9
  • Designing a Bass Boost Amplifier – 4/9
  • Designing a 6 Watt Car Audio Amplifier – 5/9
  • Design a low power amplifier for headphones- 6/9

Recent Articles

  • Fischer connector system adds ratchet locking system designed for 300g shock resistance
  • Littelfuse introduces tactile switch with enhanced bracket peg design for mounting strength
  • Infineon releases GaN switch with monolithic bidirectional design
  • Sienna Semiconductor data converters feature sample rates from 20 to 250 Msps
  • Delta’s 5,500 W power supplies achieve 97.5% energy efficiency for AI servers

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronics Projects and Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe