Using buzzers can be fun. In this tutorial, you will learn how to use them, from simply controlling the pitch and loudness to playing musical notes. Lets get started!
Step 1: Collecting the parts
Obviously, we need to start by understanding and gathering all the required components. Here is a list of them, along with explanations for all of them-
- Buzzer- Piezo buzzers come in many shapes and sizes, from small high pitch ones to large ones loud enough for doorbells or other industrial purposes. For our purposes, most buzzers will do, except for those ones which are obnoxiously large and consume too much power. Here are images of some of the buzzers which will work.
- Arduino Uno(Or any similar microntroller)- This is the brain of our project, While initially, it will only be used for switching it on or off, we will also use it to play notes and music.
- Battery(optional)- Once you have completed the tutorial, you can use the battery to power it away from your computer. This is an optional component and you can choose not to use it. Just make sure that the battery/battery pack can provide over 6V.
Step 2: Connecting everything
In this, step, we will connect the buzzer to the Arduino. Let’s start by quickly understanding how the buzzer works. The Buzzer works on the principle of piezoelectric oscillation. When an electric current flows across the 2 electrodes(pins) of the buzzer, an electric field is created which makes a thin disk of a piezoelectric material rapidly change shape and size. This hits a a small piece of metal which makes the buzzing sound we want. The Buzzer has 2 pins, one of which is positive and the other is negative.
You should be able to identify the positive and negative pins from the above image. All that is left now is to connect it to the Arduino. The negative pin from the buzzer can be connected to any of the available Ground(GND) pins while the positive pin can be connected to any of the digital output pins(from 0 to 13). For this tutorial, I am connecting it to pin 13 but as I have said, you can connect it to any pin by changing just one line in the code. Here is an image of how this will look-
Step 3: Writing code
In this step, we will be writing the code that controls our buzzer. To start, we will just switch it on and off every half a second. If you are not familiar with Arduino programming, click here to learn how to get started with Arduino.
Let’s understand this code line by line. In the first line, I create a variable buzzpin and give it a value of 13. This is the pin which controls the buzzer, make sure you assign it the same value as the pin your buzzer is connected to. I then set the pin to output by saying pinMode(buzzpin, OUTPUT) and then change its state over and over again from on to off and back every half a second. You can upload this to the board and play with the delay a bit to get different beeps. If your buzzer does not work then make sure your code is alright and it is firmly connected the the board. If the problem continues then try replacing the buzzer with an led(keeping the polarity right) and if the led works, then it is your buzzer which is faulty.
Now that we have learned how to switch the buzzer on and off, let’s play musical notes!
Step 4: Making music
Before we play musical notes, let us quickly understand what they are. Musical notes are just sounds of certain frequencies which are played together in succession to create an illusion of what we call music. Woah! Don’t leave! Musical notes are basically sounds of different pitches. When they are played for varying amounts of time in a certain order, they make . . . MUSIC!
How do we do this using a microcontroller you ask? That’s easy, just switch the pin on and off rapidly. (THIS IS NOT PWM! In Pulse width Modulation the pin is held in the On state and Off state for different time periods while the frequency remains the same. In Tone, we change the frequency while the pin remains ON and OFF for the same amount of time always.) To do make this easy, we will make use of the Arduino’s tone function. Just paste this code into your IDE, compile and upload.
The Tone function has 3 parameters(which you might have already realized). We first need to tell it which pin is being used. In our case we are using buzzPin. We then tell it the frequency we need to play in Hz(Hertz) and then finally for how long(in milliseconds). That’s it! Super easy!
Final Step: Making it Portable
Right now you might want to show it to your friends or take it to different places, but how can you do that? Don’t worry, in this step we will make the entire device portable. You may choose to skip this step.