The Piezo Buzzer project

In this section, we will learn how to use a Piezo Buzzer and make sounds using some in-built C functions. A Piezo Buzzer is like a small speaker capable of emitting sounds at various frequencies. A typical Piezo Buzzer is shown in the following figure:

Figure 3: A Piezo Buzzer

A Piezo Buzzer has two legs, one longer positive leg and one shorter negative leg. The negative leg is connected to the ground while the longer positive leg is usually connected to an Arduino pin via a 100 Ohms resistor.

Sometimes, you may find a small sticker pasted to the top of a brand new Piezo Buzzer that says, "Remove after washing". Do not get confused by this, it is just a leftover sticker from the Buzzer manufacturing process, which was not removed. Just ignore and remove the sticker. Do not wash the Buzzer!

For building the Piezo Buzzer project, we will use the following hardware components in this example:

  • Arduino Uno R3
  • USB connector
  • 1 breadboard
  • 1 Piezo Buzzer
  • 1 piece 100 ohms resistors (higher resistor values will lower the sound intensity)
  • Some male-to-male jumper wires

You may substitute the 100 ohms resistor with a higher value resistor. However, doing so will result in the reduction of sound intensity. Once all the required parts are available, go ahead and assemble them as shown in the following breadboard diagram:

Figure 4: Wiring of the Buzzer project

Notice how the longer positive leg of the Piezo Buzzer has been connected to the digital pin number 6. This has been done so that the Buzzer can receive power from Arduino's pin number 6.

It is worthwhile to note that Arduino Uno's I/O pins can safely provide upto 40 mA of current. Beyond this level, damage might start happening to the Arduino board. So be careful about what you connect to the pins.

While connecting a component like an LED, a Buzzer, or a small sensor directly to the Arduino I/O pins may be fine, one must avoid connecting heavier components directly. Heavier components are like motors that can draw more than 40 mA.

The connections between the Arduino and the Piezo Buzzer are listed in the following table for reference:

Arduino Uno pin

Buzzer pin(s)

Digital pin 6

+ve (longer) leg

GND

-ve (shorter) legs of the 3 LEDs

Table 2: Arduino to Buzzer pin mapping

While making the connections, you will notice that one leg of the LED is longer than the other. The longer leg of the LED is the positive terminal of the LED. The resistor should be placed between the Arduino pin and the positive terminal of the LED.

After assembling the circuit, load the following sketch into the Arduino Uno board. As a reminder, it is advisable not to connect any wires to the Rx and Tx pins of the Arduino while loading the sketch. In the next section, we will learn how to use in-built C functions to interface with the Piezo Buzzer:

//**********************************************************/ 
// Step-1: Variables used in this sketch
//**********************************************************/
//The following frequency values for musical notes have been
//collected from the Arduino Foundation website
//address mentioned below
//https://www.arduino.cc/en/Tutorial/ToneKeyboard?from=Tutorial.Tone3
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494

int pin = 6; // the pin used to send signals to the buzzer


//**********************************************************/
// Step-2: INITIALIZE I/O PARAMETERS
//**********************************************************/
void setup()
{
// no special setup related code in this example
}


//**********************************************************/
// Step-3: MAIN PROGRAM
//**********************************************************/
// the loop function runs over and over again forever
void loop()
{
tone(pin, NOTE_C4, 500);
delay(1000);
tone(pin, NOTE_D4, 500);
delay(1000);
tone(pin, NOTE_E4, 500);
delay(1000);
tone(pin, NOTE_F4, 500);
delay(1000);
tone(pin, NOTE_G4, 500);
delay(1000);
tone(pin, NOTE_A4, 500);
delay(1000);
tone(pin, NOTE_B4, 500);
delay(1000);
}

In a nutshell, the preceding C sketch plays the seven basic fundamental notes in a musical octave. This sketch utilizes the frequency definitions that are available on the official Arduino foundation website. Let us analyze the sketch and see how exactly the musical notes are getting played through the Buzzer.

To start with, the sketch starts with the top section where the variables used in the program are declared. For sounding the Piezo Buzzer, we have utilized the following function:

tone(pin, frequency, duration) 

This function takes the pin number through which a sound with the specified frequency will be played on the Buzzer for the specified duration.

Therefore, it is very important that we know to what frequency the seven fundamental musical notes (that is, C, D, E, F, G, A, and B) corresponds to. This information can be readily found on the official Arduino foundation website. A reference URL is mentioned at the top of the sketch. You may visit this location and search for frequency data for more musical notes.

The setup() function in this case is empty, as we do not need to explicitly configure any pins. This is because the pin configuration is taken care of by the tone() function.

Once in the loop() function, the program executes the following line of code to sound a musical note on the Piezo Buzzer. The variable NOTE_C4 corresponds to the frequency 262, as defined at the top of the sketch. Hence, the following line of code results in a sound of frequency 262 Hertz being played for a duration of 500 milliseconds:

tone(pin, NOTE_C4, 500); 

Thereafter, the program flow pauses for a second by using the following line of code. This pause has been given intentionally, in order to keep a small silence between two corresponding notes. It is up to you to increase or decrease the gap as well as duration of the sounds, depending upon how you want the sequence of notes to play out:

delay(1000); 

The preceding two lines of code are repeated seven times, once for each musical note.

With this information in mind can you now imagine how we can build a simple electronic keyboard? You thought right! We can use seven buttons and wire them up with the Arduino, and on pressing each button, the sketch should sound a musical note of an appropriate frequency. Similarly, you may apply this knowledge in so many other projects that require sounds to be emitted. The possibilities are limitless.

In the following sections of this chapter, two other commonly used components have been described in a very clear manner. These components (transistors and diodes) are commonly used while building Arduino prototype circuits. It is very important that you grasp the fundamentals in this chapter, so that you can understand the rationale behind their usage in the remaining chapter in this book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.82.23