How To Set Up Buzzer With Arduino: 3 Examples

Explore the basics of a buzzer with Arduino to generate different sounds with the help of the tone function. Learn how to use it effectively in your projects.

Buzzer with Arduino

A buzzer or piezo speaker is an audio signaling device commonly used to produce sound. A buzzer is a tiny component in electronics but can be used very smartly in multiple applications. Piezo buzzer produces sound based on the reverse principle of the piezoelectric effect. The buzzer is a less costly and lightweight electronic device which is why it is used in computers, alarm devices, refrigerators, microwave ovens, security devices, and so on. 

There are two conductors available inside the buzzer along with a piezo crystal between them. Whenever the potential is applied across the crystal then the conductor’s position gets changed due to which a 2 to 4 kHz sound wave is produced by the buzzer.

In this article, I will explain to you how a buzzer can be operated with the help of Arduino Uno differently to generate different toned sounds. This is a very basic tutorial if you want to learn more about how they can buzzer be used in any security device then you can check my advanced-level project named Upgrade Your Locker Security with Arduino Uno.

Required components

Connection diagram

Buzzer with Arduino

Circuit description

A buzzer is a two-terminal (one is longer and the other one is shorter) device. The longer leg is used to provide the input and the shorter leg is used to provide the ground connection. According to the circuit diagram, I have connected pin 7 of Arduino with the longer leg of the buzzer. The shorter leg of the buzzer is connected to the GND terminal of the Arduino Uno. 

Codes (Buzzer with Arduino)

Here I am sharing three codes along with their output video which will help you understand the code’s working efficiently.

Parameters:

  • pin = pin number which is used to generate a tone.
  • Frequency = frequency of the tone in hertz (unsigned integer can take values up to 65,535 but we will use values between 2000 and 5000 to make tone hearable for human ears).
  • Duration = the time limit of tone in the millisecond.

I used different values to generate different tones. tone() function used to generate a square wave of the specified frequency. The duration of the tone can be specified with the frequency of the tone. If the duration is mentioned along with the tone() function then the tone will be generated only for the specified duration. If the duration is not mentioned in the tone() function then the noTone() function is needed to stop the tone.

Example Code-1

In this first example, I used the “digitalwrite(pin, value)” command to operate the buzzer. In this syntax, the pin indicates the pin number and the value indicates the LOW or HIGH. To turn on the buzzer HIGH value is used and on the other side to turn off the buzzer LOW value is preferred in the command.

/*
*How to operate buzzer by Arduino uno
*This is a very basic example which will help you to understand basic operation of buzzer using Arduino Uno.
*
*for more detail about this project please visit:https://arduinounomagic.com/2019/02/buzzer-with-arduino.html
*
*Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>
*
*for more projects based on Arduino uno please visit: https://arduinounomagic.com/
*/

int BUZZER=7; // connect buzzer at pin 7

void setup() {

pinMode(BUZZER, OUTPUT); // set BUZZER as output
  
}

void loop() {
 digitalWrite(BUZZER, HIGH); //turn on buzzer
 
  delay(500); //delay
 
 digitalWrite(BUZZER, LOW); //turn off buzzer
 
  delay(1000); //delay
 }

Example Code-2

In this example, I used the tone() function to operate the buzzer. There are two types of syntax used for the tone() function.

Syntax of the tone() function

/*
*How to operate buzzer with Arduino uno
*This is a very basic example which will help you to understand the working of the tone function for buzzer.
*
*for more detail about this project please visit:https://arduinounomagic.com/2019/02/buzzer-with-arduino.html
*
*Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>
*for more projects based on Arduino uno please visit: https://arduinounomagic.com/
*/ 

int BUZZER=7; // connect buzzer at pin 7

void setup() {

pinMode(BUZZER, OUTPUT); // set BUZZER as output
  
}

void loop() {
 
 tone(BUZZER, 1000); //generate 1KHz sound signal
   
   delay(500); //delay
  
   noTone(BUZZER); //stops sound
   
  delay(2000); //delay
}
/*
*How to operate buzzer with Arduino uno
*This is a very basic example which will help you to understand the working of the tone function for buzzer.
*
*for more detail about this project please visit:https://arduinounomagic.com/2019/02/buzzer-with-arduino.html
*
*Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>
*
*for more projects based on Arduino uno please visit: https://arduinounomagic.com/
*/

int BUZZER=7; // connect buzzer at pin 7 

void setup() {

pinMode(BUZZER, OUTPUT); // set BUZZER as output
  
}

void loop() {
  tone(BUZZER, 2000, 100); //generate sound signal

   delay(1000); //delay
}

Syntax of the noTone() function

  • noTone(pin)

Parameter: 

  •  pin = pin on which tone generation should get stopped.

Limitation of Tone() function

  • If you are using analogWrite() on pin 3 or 11 then you cannot use tone() because tone() uses the same built-in timer for pin 3 and 11 as analogWrite(). If you will use them together then you will find an unexpected tone as output.
  • If you are using two buzzers on different pins then you can’t play both of them at the same time. You need to turn off the tone on any one of the pins using noTone() before using the tone() on another pin.
  • The buzzer is not able to generate a tone below 31 HZ.

Output video

Here I am attaching the output video for all codes which will help you to understand the working of the codes.


The tone function can also be used to generate piano notes of any song with the help of additional pitches.h file. Hence buzzer is a small, efficient, less costly, less bulky device that can generate different kinds of tones with very little effort. You need to modify your code to change the frequency of the buzzer sound which helps you to create different patterns of the sound.

Hope you like the article.

If you like the article, let me know in the comments.

If you have any questions, feel free to ask.

Leave a Reply

Your email address will not be published. Required fields are marked *