30 Days Lost in Space → Help Center

Libraries

Tone: Sound Generation

Generate square wave tones on any digital pin. The built-in tone() function is all you need to make buzzers beep, play melodies, and create sound effects.

What is tone()?

tone() is a built-in Arduino function (no library install needed) that generates a square wave at a specific frequency on a digital pin. Perfect for passive buzzers and simple speakers.

Basic Usage

tone(pin, frequency);           // Start playing
tone(pin, frequency, duration); // Play for duration (ms)
noTone(pin);                    // Stop playing

Key Functions

| Function | Description | |----------|-------------| | tone(pin, freq) | Start continuous tone at frequency (Hz) | | tone(pin, freq, ms) | Play tone for specified milliseconds | | noTone(pin) | Stop the tone on that pin |

Musical Notes

#define NOTE_C4  262
#define NOTE_D4  294
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_G4  392
#define NOTE_A4  440
#define NOTE_B4  494
#define NOTE_C5  523

Playing a Melody

int melody[] = {NOTE_C4, NOTE_E4, NOTE_G4, NOTE_C5};
int durations[] = {250, 250, 250, 500};

for (int i = 0; i < 4; i++) {
  tone(8, melody[i], durations[i]);
  delay(durations[i] + 50);  // Gap between notes
}
noTone(8);

Limitations

  • Only one tone can play at a time across all pins
  • tone() uses Timer 2, which disables PWM on pins 3 and 11
  • For polyphonic sound, you need external hardware
  • Square waves sound "buzzy" — not smooth like a speaker

Tips

  • Always call noTone() when done to free the timer
  • Add a small delay between notes for distinct sounds
  • Use tone() with millis() instead of delay() for non-blocking sound
  • Active buzzers don't need tone() — they have their own oscillator