bclose

20 – Buzzers

Objectives

 

 

    • Get to know a piezoelectric buzzer.
    • The functions tone() and noTone().
    • First steps with electronic music
 

BILL OF MATERIALS

Arduino Uno

Arduino Uno or equivalent.

We can use any other Arduino model in this chapter.

 

BreadboardJumper wires A solderless Breadboard plus some jumper wires .
Buzzer A piezoelectric buzzer.

 

PIEZOELECTRICITY

 

According to Wikipedia, piezoelectricity is a phenomenon that occurs in certain crystals that, when subjected to mechanical stress, acquire electrical polarization thus appearing a difference of potential and electric charges on its surface that generate an electric voltage.

This phenomenon also occurs the other way round: the crystals get pulled out of shape under the action of internal forces when subjected to an electric field. The piezoelectric effect is normally reversible: when you stop subjecting the crystals to an external voltage or electric field, they regain their original shape.

That is, they are materials (quartz is the best known) that vibrate when subjected to a variable voltage (such as a PWM signal, with which we are already familiar).

 
  • This is a well known phenomenon and many domestic gas lighters operate under this principle. A spring hits quartz and as a result we have the spark that ignites the gas or water heater emitting that characteristic click).
  • Digital electronic circuits, usually have an internal clock that vibrates at a speed pattern based on piezoelectric quartz crystals. Arduino crystal beats 16 millions of times per second (16Mhz ). The arrow on the image below indicates its position.Arduino-clock
  • In a forthcoming chapter we will use one of these crystals to synchronize a discrete circuit with the heart of an Arduino, the ATmega 328.
 

If we apply a digital signal to a piezoelectric device, it will vibrate at a frequency that quite faithfully follows the electrical variation that excites it, and if vibrates at audible frequency, we can hear the sound it produces. The component that does it is called buzzer.

Naturally, the quality of the produced sound is far from what we might call high fidelity. But it is enough to generate audible tones (like the typical digital alarm clocks) and even recognizable musical tones that we can sequence, even musical pieces (though one would like to be somewhere else when you hear them).

Sooner or later it is useful to have an acoustic signal in your projects, let’s see how we can assemble these elements, and what kind of options are available.

In this chapter we will assemble a very simple prototype using a buzzer.

CIRCUIT SCHEMATIC DIAGRAM

 

 

Chapter 20, schematic Chapter_20_wiring_diagram

The connection is as simple as connecting the negative wire to GND and positive wire to pin 9. Anyway, be careful. The piezos have polarity so make sure to connect them properly.

 
  • If the wires are connected the other way round, it will simply not make any sound and you will have to switch them.
  • Besides, in this first assembly, we need to use a PWM pin (such as 9) because it is the variation between HIGH and LOW voltage which causes the piezoelectric effect (remember that a PWM signal sends a train of square waves of variable amplitude), so the best option is to use a PWM pin instead of programming an equivalent effect on a normal pin.
 

THE SKETCH

Let’s start by creating a function, called Beep(), which simply makes noise:

Sketch 20.1
     
void beep(unsigned char pause)
{
    analogWrite(9, 20);
    delay(pause);                 // Wait
    analogWrite(9, 0);            // Turn off
    delay(pause);                 // Wait
}

Try this now:

     void setup()
        {
            pinMode(9, OUTPUT);
            beep(50);
            beep(50);
            beep(50);
            delay(1000);
        }
     void loop()
       {    
           beep(200);
       }

The only thing that the Beep() function does is to put a PWM signal on pin 9 of 20 on 255. You can vary the value, but the audio tone will not change very much because it is controlled by the base signal. This is enough to generate one of the annoying beeps of a cheap clock.

What if we want to generate signals of variable tone to make a tune? Well Arduino provides the tone() function that generates a signal of the specified frequency and a noTone() function that stops it:

     
        void setup()
        {
            int pinOut = 8;
            int freq = 440;
            int duration = 1000;
            tone(pinOut, freq, duration);
        }

The tone() function generates an audio signal of the defined frequency and duration.

We can define an array to store the frequencies of the notes we want to use and read the correspondence with musical notes. Let’s see how to build a musical scale with a buzzer:

Sketch 20.2
     int speakerPin = 9;
  
     int numTones = 10;
     int tones[ ] = {261, 277, 294, 311, 330, 349, 370, 392, 415, 440,466, 494};
                       // mid C C# D D# E F F# G G# A

     void setup()
        { }

     void loop()
        {
             for (int i = 0; i < numTones; i++)
                 {
                     tone(speakerPin, tones[i]);
                     delay(500);
                 }
             noTone(speakerPin);
       }
 
  • Given the prevailing taste for recreational physics, I am terrified at holding forth on the relationship between musical notes and their corresponding frequency as well as the different methods of calculating them, not to speak of the temperament scales…
  • So we’ll just mention that there is a direct relationship between the frequency of a note and its position, for example, in a piano keyboard.
  • Those with musical training and interest in how to calculate the frequency of a given note, can do a search on Google and will find everything immediately.
 

Finally, to close this chapter, I must say that internet is full of people who have enough time to spend to the most unlikely extravagances. This is the page of someone who has spent an indecent number of hours in transcribing the music of Star Wars to chamber orchestra, composed for Arduino and a buzzer.

Star Wars Imperial March fragment

 

Summary

 

 

    • We have used a piezoelectric buzzer.
    • They are very easy to use although its quality is poor.
    • They are a cheap and fast system to add sound to your projects.
    • These are our first steps in electronic sound generation. If we have some time we will devote some time in further chapters to the generation of electronic and MIDI sounds.
 

 

 

No Comments

Give a Reply