Zed-King Institute

    Arduino Buzzer Code Using tone() and noTone()

    easySoundArduino (C++)

    tone() makes a piezo buzzer play a note of any frequency — used in alarms and simple music.

    The question

    A buzzer is on pin 10. Make it play a 262 Hz tone for 500 ms, stay silent for 500 ms, and repeat.

    The sketch

    sketch.ino

    void setup() {
      pinMode(10, OUTPUT);
    }
    
    void loop() {
      tone(10, 262);
      delay(500);
      noTone(10);
      delay(500);
    }

    Wiring

    Buzzer + → pin 10; buzzer − → GND.

    How it works

    1. 1tone(pin, frequency) starts a square wave at that frequency in hertz (440 Hz is the musical note A).
    2. 2noTone(pin) stops the sound.
    3. 3The delays set how long the beep and the silence last.

    Common mistakes

    • Using digitalWrite on a passive buzzer — it only clicks, it does not play a tone.
    • Forgetting noTone(), so the buzzer never stops.

    Now solve a similar question yourself

    A new Arduino question every time, checked instantly.

    Practice questions