0

I am using a piezo buzzer and fiddling around with it a little bit. I currently have different tones with delays in between and I have to do a lot of trial and error for the piezo buzzer to play sounds like I want it to. Is there a better way to do this? because at this rate I will be stuck for a while.

I am trying to get the piezo buzzer to make the sound of an alarm. Any suggestion on how I can get it to sound exactly like an alarm or is trial and error the only way?

2
  • Posting your current code, might help us to better understand what it is you want to achieve (better).
    – Gerben
    Mar 3, 2017 at 12:40
  • are you using an active or passive buzzer? (they need different code)
    – dandavis
    Mar 3, 2017 at 19:43

1 Answer 1

2

It's very hard to know from your question what you are having trouble with. I have a piano and I can hit the keys, but it takes me a long time to hit them in the right order and with the right timings to create a new symphony.

Anyway, if creating the sounds is the problem, have you looked at the Tone library?

Different alarms have different sounds. I once heard an alarm which sounded like the start of Fuer Elise, so probably the notes were E and D#. That would make their frequencies about 330Hz and 311Hz:

void loop()
{
    tone(pin, 330, 250);
    delay(300);
    tone(pin, 311, 250);
    delay(300);
}

The sound you linked to sounds to me like it starts on a C, and slides up gradually one octave. One octave higher is double the frequency, so this code should do that:

void loop()
{
    const int startFreq = 523;  // C5 is 523Hz

    // Start at the given freqency, and increase gradually by
    // one octave (double the frequency)
    for (int freq=startFreq ; freq<= (startFreq*2) ; ++freq)
    {
        tone(pin, freq, 10);
        delay(3);
    }
}
13
  • Like I said I want to make the piezo buzzer implement an alarm. However, I need to have right tones. I have to find this right tone, and its hard for me to get there. maybe there is an easier way to implement the type of sound you are wanting to produce. Does this make it more clear?
    – Utsav
    Mar 3, 2017 at 12:08
  • What is the pin? because its not declared in the scope?
    – Utsav
    Mar 3, 2017 at 22:11
  • It's the pin number that you have the buzzer on. You could find that out by reading the links in my post, and you would learn much more than by copying and pasting code you don't understand.
    – Mark Smith
    Mar 3, 2017 at 22:13
  • A specific alarm I was looking for was like a fire alarm
    – Utsav
    Mar 3, 2017 at 22:23
  • youtube.com/watch?v=lo4yJbjkMWA like this one
    – Utsav
    Mar 3, 2017 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.