Why Buzzer doesn't work with ESP32-S3?

// Define the pin where the buzzer is connected
const int buzzerPin = 12;

// Define the notes for Happy Birthday
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_D5  587
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_G5  784
#define NOTE_A5  880

// Define the melody for Happy Birthday
int melody[] = {
  NOTE_C5, NOTE_C5, NOTE_D5, NOTE_C5, NOTE_F5, NOTE_E5, // Happy Birthday to You
  NOTE_C5, NOTE_C5, NOTE_D5, NOTE_C5, NOTE_G5, NOTE_F5, // Happy Birthday to You
  NOTE_C5, NOTE_C5, NOTE_C5, NOTE_A5, NOTE_F5, NOTE_E5, NOTE_D5, // Happy Birthday Dear [Name]
  NOTE_C5, NOTE_C5, NOTE_D5, NOTE_C5, NOTE_F5, NOTE_E5, // Happy Birthday to You
};

int noteDurations[] = {
  4, 4, 4, 4, 4, 4, // Happy Birthday to You
  4, 4, 4, 4, 4, 4, // Happy Birthday to You
  4, 4, 8, 8, 4, 4, 4, // Happy Birthday Dear [Name]
  4, 4, 4, 4, 4, 4, // Happy Birthday to You
};

void setup() {
  // Set the buzzer pin as output
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  // Play the melody
  for (int thisNote = 0; thisNote < 24; thisNote++) {
    // Calculate the note duration
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(buzzerPin, melody[thisNote], noteDuration);

    // Pause between notes
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);

    // Stop the tone
    noTone(buzzerPin);
  }

  // Add a delay before repeating the song
  delay(2000); // Delay for 2 seconds before repeating
}

I can hear any sound only if I put "digitalWrite(12, HIGH);", and when I was checking, I also tried to write another programs, but they do not work also

It worked correctly here.

This has me curious about the exact part number buzzer you are using, how you have it connected, and the range of tones your buzzer will output. Alarm buzzers function on DC power. Buzzers capable of audio output require non-DC (AC or simulated AC) input.

More information is needed. It could be a 5V buzzer, or it might be an annunciator that requires pulsing. Please post links to all the parts for clarity.

A buzzer is like the horn of a car. It produces a single tone when powered with a DC voltage.
To play different tones (music) you need a piezo transducer (loudspeaker).

Test your buzzer by connecting it to a 9volt battery.
If it makes a sound on it's own, then you can't use it for your project.

A piezo disk from a musical greeting card will work.
Leo..

PaulRBKarma: 2500+

2hpost #11

@kirilllanskoy you have posted almost 80 times and created almost 20 new topics. You can no longer claim to be new to the forum or have an excuse for not having read the forum guide.

Why did you post this, when you must know by now that it is a bad question because it lacks any of the detailed information other forum members need in order to be able to help you.

I was just sleeping :rage:, If it’s daytime for you, it doesn’t mean that it’s daytime for me and not night. @Wawa, it has made a sound when I have connected it to 9v battery

I've deleted some inappropriate comments.

From now on replies will either be to help the OP or they will be from the OP providing information and answering questions raised by those trying to help. Deviation from this will likely result in short bans from posting.

I thank you.

So if it work from 9v and PWM it is annunciator, isn't it? If yes, then what should I do?

Since your buzzer is an annunciator and your code will not function with your current buzzer, you will need to replace your buzzer with the correct type for your sketch.

1 Like