How to use an Arduino Buzzer

In this guide you can learn what an Arduino buzzer is, what is piezzo electricity, and how to use such a device with your Arudino board. Arudino buzzers can be used to alert a user of an event corresponding to a switching action, counter signal or sensor input. They are also used in alarm circuits. In this guide, you will also learn how to operate an Arduino buzzer from a Windows or Linux PC or from a Raspberrry Pi using Ozeki. You can find easy to follow videos and some great diagrams. First lets start with the basic definitions.

What is an Arudino buzzer?

An arduino buzzer is also called a piezo buzzer. It is basically a tiny speaker that you can connect directly to an Arduino. You can make it sound a tone at a frequency you set. The buzzer produces sound based on reverse of the piezoelectric effect.

What is the piezzoelectric effect?

Piezo ectricity is an effect where certain crystals will change shape when you apply electricity to them. By applying an electric signal at the right frequency, the crystal can make sound.

How does an Arduino buzzer work?

The buzzer produces the same noisy sound irrespective of the voltage variation applied to it. It consists of piezo crystals between two conductors. When a potential is applied across these crystals, they push on one conductor and pull on the other. This, push and pull action, results in a sound wave. Most buzzers produce sound in the range of 2 to 4 kHz.

How to use an Arudino buzzer?

If your buzzer has a sticker on top of it, pull the sticker off. Connect one pin (it doesn't matter which one) to the Arduino's ground (Gnd) and the other end to digital pin 10. From the Arduino, you can make sounds with a buzzer by using tone. You have to tell it which pin the buzzer is on, what frequency (in Hertz, Hz) you want, and how long (in milliseconds) you want it to keep making the tone. We will use the arduino buzzer tone generation command:

tone(8, 1200, 500); // tone on pin 8 at 1200 Hz for 1/2 second

How do you know what frequency to use? Young people can generally hear frequencies between about 20 Hz and 20,000 Hz; older people lose the high frequencies and may not be able to hear all the way up to 20,000. Of course it varies from person to person. Also, your buzzer may not be able to reproduce the whole range, especially the very high and low notes.

If you like, you can leave off the duration. In that case, it will keep making a tone until you tell it to stop by calling noTone(pin) or by calling tone() with a different frequency.

Arduino buzzer wiring diagram

arduino buzzer wiring diagram
Figure 1 - Arduino buzzer wiring diagram

Example code to test your buzzer on your Arduino

int SPEAKER = 10;    // The speaker is on pin 8
int freq = 50;      // Starting frequency

void setup()
{
    pinMode(SPEAKER, OUTPUT);
}

void loop()
{
    freq += 100;      // add 100 to freq

    if (freq > 8000)
    {
        noTone(SPEAKER);
        freq = 50;
    }

    tone(SPEAKER, freq);

    delay(100);
}

How to use the Arudino buzzer in Ozeki

To use an Arudino buzzer in Ozeki 10, you need to wire the buzzer to your Arudino, then you need to connect your Arudino to your PC using a USB port. You can use a Windows or Linux PC or you can use a Raspberry Pi. Once the Arudino is connected to your PC, you need to setup Ozeki 10, which gives you a webbrowser based user interface to interact with the buzzer. To use the Buzzer in Ozeki, you first need to download Ozeki Robot Developer. Ozeki Robot Developer will install the Arduino libraries needed to use this sensor efficiently.

Download Ozeki Robot Developer

Download Ozeki Robot Developer

After Ozeki Robot developer is installed, you need to upload the Buzzer control code to your Arduino. You can find the code and download instructions on the following pages. The upload process includes two steps: first you need to format the EEPROM of the arduino, then you need to upload the control code. The procedure is very simple, it only takes a few seconds.

Upload the Buzzer code to Arduino Uno
Upload the Buzzer code to Arduino Mega 2560
Upload the Buzzer code to Arduino Nano
Upload the Buzzer code to Raspberry Pi

The arduino sensors and Ozeki will communicate over the USB port using the Ozeki Buzzer protocol. This protocol makes it possible for you to use the sensor directly on your PC. You will be able to control this sensor through the web based user interface or you will be able to communicate with it using Ozeki Chat. You can read more about chat control on the following page.

How to communicate with the Buzzer using chat

It is important to understand chat control, because when you build a robot, the way you wish to control this sensor is by sending and receiving messages. If you open the Ozeki Robot Developer app, you will see, who you can write a C#.Net program to work with this sensor.

setup buzzer to pc using arduino
Figure 2 - System configuration of buzzer connecting to PC using Arduino

Connection steps

  1. Wire the Buzzer to your Arduino
  2. Plug the Arduino board to your PC using an USB data cable
  3. Check the COM ports to see which port the Arduino is connected to
  4. Upload example code to your Arduino microcontroller
  5. Open Ozeki 10 by typing https://localhost:9515 in your browser
  6. Select the Buzzer from the connection list
  7. Set the Frequency and Duration
  8. Click 'Play' to test the Buzzer

Prerequisites

  • Buzzer (e.g. 5V buzzer with an added 100Ω resistor)
  • Ozeki 10 installed on your computer
  • Programmable board (Arduino Mega/Nano/Uno or Raspberry Pi)
  • USB cable is needed between Arduino Mega/Nano/Uno and your computer

Step 1 - Wire the buzzer to your Arduino

You can see how to wire the buzzer to any of the following boards:

After wiring, you should connect your Arudino board to your PC using an USB data cable!

Step 2 - Upload the control code to the Arduino microcontroller

(Here is the code to upload)

Step 3 - Start Ozeki 10 and test the buzzer

Step 4 - Configure your buzzer

To configure your buzzer (attached to your Arduino) in Ozeki 10, that is installed on your PC (or Raspberry Pi), you need to open the graphical user interface (GUI). You can open the GUI by entering the following URL into your webbrowser: http://127.0.0.1:9513. If you wish to connect to Ozeki from a remote computer, you need to find out the IP address of your PC, and you need to use it instead of 127.0.0.1. If you connect from a remote computer, you must make sure, that your firewall allows incoming communication on TCP port 9513 and 9514.

Step 5 - Understand the buzzer protocol

The Buzzer Controller can communicate with Ozeki through the following protocol.

Step 6 - Troubleshooting an Arduino buzzer

If your buzzer has an error, meaning it does not a make a sound, you should take into consideration, that there are two kinds of buzzers - active and passive. Passive buzzer means they can be operated by applying just logic high to + and low to ground. The examples above use such an active buzzer. Active buzzers needs pulses to make sound. Sound produced depends on the frequency. Your buzzer might be a passive one. You can make a symphony by applying different frequencies. 'tone' function will help you for this

Step 7 - How to check and improve an Arudino buzzer

Due to the capactive nature of a piezo buzzer, there needs to be a way for it to discharge when the power is turned off. To improve it's perfomance, the easy solution is to put a resistor across the piezo leads to help it discharge. For example 150 ohm resistor can do the job.

References:
https://www.shallowsky.com/
https://hackaday.io/

More information