Jump to content

Buzzer: Difference between revisions

From cognifront
CogWikiEditor (talk | contribs)
CogWikiEditor (talk | contribs)
Line 269: Line 269:


=== Melody Playing ===
=== Melody Playing ===
<pre>
Download the melody playing buzzer exam, it contains the .ino file and the pitches.h header file with tones frequency set
#include "pitches.h"


// Define the buzzer pin
<div style="margin-top:15px;">
int buzzerPin = 8;
[[Media:Buzzer-tone-melody-playing.zip|<span style="display:inline-block; padding:10px 20px; background:#3366cc; color:white; border-radius:5px; font-weight:bold; text-decoration:none;">⬇ Download Manual (ZIP)</span>]]
 
</div>
// Define the "Happy Birthday" melody
int melody[] = {
  NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_F4, NOTE_E4, // "Happy Birthday to You"
  NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_G4, NOTE_F4, // "Happy Birthday to You"
  NOTE_C4, NOTE_C4, NOTE_C5, NOTE_A4, NOTE_F4, NOTE_E4, NOTE_D4, // "Happy Birthday dear [Name]"
  NOTE_AS4, NOTE_AS4, NOTE_A4, NOTE_F4, NOTE_G4, NOTE_F4 // "Happy Birthday to You"
};
int noteDurations[] = {
  4, 4, 4, 4, 4, 2,
  4, 4, 4, 4, 4, 2,
  4, 4, 4, 4, 4, 4, 2,
  4, 4, 4, 4, 4, 2
};
 
// JINGLE BELL
// int melody[] = {
//  NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_G4, NOTE_C4, NOTE_D4, NOTE_E4,
//  NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_G4
// };
// int noteDurations[] = {
//  8, 8, 4, 8, 8, 4, 8, 8, 8, 8, 2, 8, 8, 8, 8, 4, 8, 8, 8, 8, 4, 8, 8, 8, 2
// };
 
// // Twinkle
// int melody[] = {
//  NOTE_C4, NOTE_C4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_C4
// };
 
// int noteDurations[] = {
//  4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2
// };
 
void setup() {
  for (int thisNote = 0; thisNote < sizeof(melody)/sizeof(int); thisNote++) {
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(buzzerPin, melody[thisNote], noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
 
    noTone(buzzerPin);
  }
}
 
void loop() {
 
}
 
</pre>


== Observation ==
== Observation ==

Revision as of 17:09, 3 September 2026

Interfacing Piezo Buzzer with ESP32 for Generating Sound and Music

Interfacing Piezo Buzzer with ESP32 for Generating Sound and Music demonstrates how a piezoelectric buzzer can be connected to an ESP32 board to generate sound and musical tones.

The piezoelectric buzzer is an electronic device commonly used for producing sound and music.

The buzzer can generate different sounds by applying electrical signals of different frequencies.

ESP32 → Digital Pin → Piezo Buzzer → Sound

Theory

A piezoelectric buzzer is an electronic device commonly used to produce sound and music.

The piezoelectric buzzer works on the inverse principle of piezoelectricity.

The buzzer contains a piezoelectric crystal and a diaphragm.

When voltage is applied to the piezoelectric element, the diaphragm gets stretched or compressed. These mechanical movements produce vibrations, which generate sound.

By applying signals of different frequencies, different tones can be generated.

Electrical Signal → Piezoelectric Element → Diaphragm Vibration → Sound

Using high-performance piezoelectric elements, loud, clear and pleasant electronic tones can be generated.

An appropriate frequency can be used to generate a particular musical tone.

Piezo Buzzer Pins

The piezo buzzer has two pins:

Pin Connection Function
+ ESP32 digital pin Data / signal input
− GND Ground

The pin marked with + is the data or signal pin and is connected to a digital output pin of the ESP32.

The pin marked with − is the ground pin and is connected to the GND pin of the ESP32.

Sound Generation

Sound is generated by applying an electrical signal to the piezoelectric element.

The frequency of the signal determines the frequency of the sound produced by the buzzer.

A higher frequency produces a higher-pitched sound, while a lower frequency produces a lower-pitched sound.

The ESP32 can generate different tones by changing the frequency of the signal supplied to the buzzer.

Applications

Piezoelectric buzzers are commonly used in:

  • Refrigerators
  • Typewriters
  • Automotive instruments
  • Security equipment
  • Electronic instruments
  • Sound and alert systems

Connections

Device Pin ESP32 Function
Piezo Buzzer + Digital GPIO Sound signal
Piezo Buzzer − GND Ground

Note: The exact ESP32 GPIO number depends on the GPIO selected in the program.

Algorithm

For Controlling Buzzer ON/OFF

The following algorithm is used for controlling the buzzer using digital output:

  1. Start the Arduino IDE.
  2. Declare a variable to assign a pin number of the ESP32 board.
  3. In the void setup() function, configure the selected pin as OUTPUT.
  4. In the void loop() function, make the selected pin HIGH.
  5. Give an appropriate delay.
  6. Make the selected pin LOW.
  7. Give an appropriate delay.
  8. Repeat the process continuously.

The overall operation is:

GPIO HIGH → Buzzer ON → Delay → GPIO LOW → Buzzer OFF → Repeat

Generating Different Frequencies Using Tone Function

The buzzer can also be used to generate different frequencies using the tone() function.

The tone function is used in the following format:

tone(pin no, frequency, duration);

Where:

Parameter Description
pin no Digital pin connected to the buzzer
frequency Frequency of the generated tone in Hz
duration Duration of the tone in milliseconds

Different frequencies can be supplied to the buzzer to generate different musical tones.

Musical Tone Generation

The buzzer can be used to generate a sequence of musical tones such as:

Sa → Re → Ga → Ma → Pa → Dha → Ni → Sa

The frequencies specified in the practical are:

Musical Tone Frequency Duration
Sa 3000 Hz 500 ms
Re 3100 Hz 500 ms
Ga 3200 Hz 500 ms
Ma 3300 Hz 500 ms
Pa 3400 Hz 500 ms
Dha 3500 Hz 500 ms
Ni 3600 Hz 500 ms
Sa 3700 Hz 500 ms

A delay of 1000 ms is provided between the tones.

Working

The ESP32 generates an electrical signal on the digital pin connected to the piezo buzzer.

For simple ON/OFF control, the digital pin is alternately set to HIGH and LOW with a suitable delay.

For generating musical tones, the tone() function is used to specify the frequency and duration of the sound.

The working sequence is:

  1. The ESP32 initializes the buzzer pin as an output.
  2. A frequency is applied to the buzzer.
  3. The piezoelectric element vibrates according to the applied signal.
  4. The vibration of the diaphragm produces sound.
  5. The tone continues for the specified duration.
  6. A delay is provided before the next tone.
  7. The next frequency is applied.
  8. The process continues for the complete sequence.

ESP32 → Frequency Signal → Piezoelectric Element → Vibration → Sound

Tone Sequence

The example tone sequence is:

tone(2, 3000, 500);
delay(1000);

tone(2, 3100, 500);
delay(1000);

tone(2, 3200, 500);
delay(1000);

tone(2, 3300, 500);
delay(1000);

tone(2, 3400, 500);
delay(1000);

tone(2, 3500, 500);
delay(1000);

tone(2, 3600, 500);
delay(1000);

tone(2, 3700, 500);
delay(1000);

Observation

Simple Buzzer

#define buz D4

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

void loop()
{
  digitalWrite(buz, HIGH);
  delay(1000);
  digitalWrite(buz, LOW);
  delay(1000);
}

Melody Playing

Download the melody playing buzzer exam, it contains the .ino file and the pitches.h header file with tones frequency set

Observation

When the buzzer is controlled using digital HIGH and LOW signals, the buzzer produces sound according to the applied signal.

When different frequencies are supplied using the tone() function, the buzzer generates different audible tones.

The sequence of frequencies:

3000 Hz → 3100 Hz → 3200 Hz → 3300 Hz → 3400 Hz → 3500 Hz → 3600 Hz → 3700 Hz

produces a sequence of different musical tones.

Result

The ESP32 successfully interfaces with the piezoelectric buzzer to generate sound.

The buzzer can be controlled using digital HIGH and LOW signals for ON/OFF operation and different frequencies can be generated using the tone() function to produce musical tones and sequences.