Best command to use for activating a passive buzzer with a frequency

Hi,
I am trying to find out the best command/function i can use to work with my piezo buzzer i am using. I want to be able to use PWM Features so the tone command is out of the picture which is a shame because it is the simplest one out of the options.

I am trying to get the buzzer to work with a digitalWrite function, but I cannot seem to get anywhere with it. It just makes a tiny buzzing sound. I was thinking it was because there was no frequency defined to that command but i am not sure how to do it with that command.

After this attempt i had tried the analogWrite function. This had worked but seems quite quiet, and wasnt sure how to change or find out the frequency it is running at.

analogWrite(buzzPin,100);
delay(200);
analogWrite(buzzPin,0);
delay(200);

This is the layout i had used in both digital and analogue, as i mentioned before, the analogue function only worked.

If you have any questions you need ask please do.

The buzzer is from Murata and i am pretty sure this is the part number ( PKM13EPYH4002-B0 )

Use the BlinkWithoutDelay principle with a very short period to drive the output without blocking free running of the code

There is the toneAC and toneAC2 library. They are nominally functionally identical except toneAC2 uses timer2 which may solve your PWM feature conflict.

Try this..


blinker  mrTone(3,2.5,5);  // Pin number, pulseMs, periodMs
timeObj  toneTimer(1000);  // Timer for xMs


void setup(void) {
   mrTone.setOnOff(true);  // Fire up the tone.
}

void loop(void) {

   idle();                       // Run background things, like the blinker.
   if (toneTimer.ding()) {       // If the timer has expited (**ding!**)..
      mrTone.setOnOff(false);    // Shut off the tone.
   }
}

You'll need to grab LC_baseTools from the library manager to compile it.

Good luck!

-jim lee

So toneAC uses pwm features too?

ToneAC uses a timer to generate the output signal without program involvement after it is configured. In this sense it is the same as Tone. However, there is a second version of the library, ToneAC2 that uses a different timer.

So if your issue is a conflict between the timer used by Tone, ToneAC2 is a readily available library that may resolve the issue by virtue of using a different timer.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.