Using A Passive Buzzer

Hi,
In my project i want to be able to use a passive buzzer using the digitalWrite command. However im getting a little confused how i can actually control the buzzer how i want it. I simply just want the buzzer to beep on and off with around a half a second delay on it. i have created one small program that does this however it isnt the best. So if anyone has any advice or pointers to where to go next it would be appreciated
Here is the code:

while(1)
  {
    for(int i = 0; i <200; i++)
    {
      digitalWrite(buzzPin,HIGH);
      delay(1);
      digitalWrite(buzzPin,LOW);
      delay(1);
    }
      digitalWrite(buzzPin,LOW);
      delay(500);
  }
     delay(1);

A delay of 1 millisecond. Why ?

What is the purpose of the while loop when the loop() function will do it for you ?

What is the point of the for loop ?

In loop() make buzzPin HIGH, delay() for 500 milliseconds, make buzzPin LOW, delay() for 500 milliseconds,

Why not use tone() function?
https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/

Because it is a passive buzzer with a fixed tone

Sorry

1 Like

This does not work on passive buzzers. It will just make a small noise. For a passive buzzer your delay inbetween has to be extremely short for it to work.

Why ?

The passive buzzer will make a tone for as long as it is powered and that can be as long or short a period as you want

For the Passive buzzer to work it needs a very short delay. I understand your confusion, because i was in the same situation a few days ago.

This code below, will compile and run. However, it will just create a small noise that sounds like a tick from a clock every second.

  digitalWrite(buzzPin,HIGH);
  delay(1000);
  digitalWrite(buzzPin,LOW);
  delay(1000);

For a passive buzzer to work, the delay needs to be very low. I usually use microseconds because it creates a better tone for the buzzer.

For example;

digitalWrite(buzzPin,HIGH);
delayMicroseconds(500);
digitalWrite(buzzPin,LOW);
delayMicroseconds(500);

The code above will constantly create a sound without stopping.

For a passive buzzer to create a sound, you will have to send a rapid series of on and off pulses for it to generate a tone. This is because a passive buzzer does not have a built in Oscillator.

My apologies.

I have confused passive and active buzzers in my replies and you are quite right in saying that a passive buzzer needs a series of short HIGH/LOW in order to make a sensible noise

No worries.