🔊 How to connect buzzer to Arduino

You can generate sounds using Arduino in different ways. The simplest of them is to connect a piezo emitter to the board (or, as it is also called, a “piezo squeaker”).
1_2-1

You can generate sounds using Arduino in different ways. The simplest of them is to connect a piezo emitter to the board (or, as it is also called, a “piezo squeaker”). But as always, there are some nuances here. In general, let’s connect a piezo squeaker to the Arduino and we will understand.

We will need:

  • Arduino (or compatible board);
  • piezo emitter (piezo squeaker), can be purchased here or here;
  • connecting wires;
  • breadboard.
  • Instructions for connecting the piezo to the Arduino

Connection diagram of the piezo emitter to the Arduino

A piezo emitter, or piezoelectric transducer, or “piezo squeaker” is an electroacoustic sound reproduction device using the inverse piezoelectric effect. The principle of its operation is based on the fact that under the action of an electric field a mechanical movement of the membrane occurs, which causes the sound waves that we hear. Typically, such sound emitters are installed in household electronic equipment as sound alarms, in desktop PC cases, in telephones, in toys, in loudspeakers, and much more.

A piezo emitter has 2 outputs and polarity matters. Therefore, we connect the black pin to ground (GND), and the red pin to any digital pin with the PWM function. In this example, the positive output of the radiator is connected to the output “D3”.

Extract sound from a piezo emitter using the analogWrite () function

The piezo can be activated in various ways. The simplest of these is to use the analogWrite () function. An example of a sketch is in a sidebar. This sketch alternately turns on and off the sound with a frequency of 1 time in 2 seconds.

int soundPin = 3; 

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

void loop() {
    analogWrite(soundPin, 50); 
    delay(1000);
    analogWrite(soundPin, 0);
    delay(1000);
}

Set the pin number, define it as a way out. The analogWrite () function takes as output an output number and level, which can be from 0 to 255 since Arduino’s PWM outputs have an 8-bit DAC. This value will change the volume of the piezo squeaker within small limits. To turn off the piezo squeezer, you need to send the value “0” to the port.

Using the analogWrite () function, you cannot change the tone of the sound, unfortunately. A piezo emitter will always sound at a frequency of approximately 980 Hz, which corresponds to the frequency of the output of pulse-width modulated signal (PWM) on Arduino UNO boards and the like.

Extract sound from the piezo using the tone () function

But the frequency of the sound can be changed differently. To do this, extract the sound from the piezoelectric transducer using the built-in function tone (). An example of the simplest sketch is shown in the sidebar…

int soundPin = 3; 
void setup() {
    pinMode(soundPin, OUTPUT); 
    Serial.begin(9600); 
}

void loop() {
    for (int i=20; i<10000; i+=50) {
      tone(soundPin, i);
      delay(100); 
      Serial.println(i);  
    }
    noTone(soundPin);
    delay(1000);
}

The tone () function takes as arguments the number of the Arduino output and the sound frequency. The lower limit of the frequency is 31 Hz, the upper limit is limited by the parameters of the piezo emitter and human hearing. To turn off the sound, send the noTone () command to the port.

And this is what the signal timing diagram that the tone () function generates will look like. It is seen that every 100 ms the frequency increases, which we hear:

As you can see, with the help of a piezo-emitter from Arduino you can extract sounds. You can even write a simple musical composition, setting the notes with appropriate frequencies, as well as determining the duration of the sound of each note using the delay () function.

Please note that if several piezo emitters are connected to the Arduino, only one will work at a time. To turn on the emitter on another output, you need to interrupt the sound on the current one by calling the function noTone ().

An important point: the tone () function is superimposed on the PWM signal on the “3” and “11” pins of the Arduino. That is, called, for example, for pin “5”, the tone () function can interfere with the work of pins “3” and “11”. Keep this in mind when designing your devices.

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest