The Tone Library in Arduino allows you to generate sound waves on a digital pin, making it easy to create melodies, sound effects, or audible alerts using a piezo buzzer or speaker.
This tutorial explains the tone library, its functions, and how to use it with examples.
Table of Contents
1. What Is the Tone Library?
The Tone Library is a built-in feature of Arduino that allows you to generate square waves of varying frequencies on a digital pin. These square waves can produce sounds when connected to a piezo buzzer or speaker.
2. Functions in the Tone Library
2.1 tone()
- Generates a square wave on a specific pin at a given frequency.
- Syntax:
tone(pin, frequency, duration);
- pin: Digital pin number.
- frequency: Frequency of the tone in Hz.
- duration: (Optional) Duration of the tone in milliseconds.
Example:
tone(8, 440, 500); // Play a 440 Hz tone on pin 8 for 500 ms
2.2 noTone()
- Stops the tone being generated on a pin.
- Syntax:
noTone(pin);
Example:
noTone(8); // Stop tone on pin 8
3. Connecting a Piezo Buzzer or Speaker
Circuit Diagram
- Connect the positive pin of the piezo buzzer or speaker to a digital pin (e.g., pin 8).
- Connect the negative pin to GND.
- Optionally, use a resistor (e.g., 100-1k ohms) in series with the buzzer to limit current.
4. Practical Examples
4.1 Play a Single Note
Play a simple tone for a specific duration.
const int buzzerPin = 8; void setup() { tone(buzzerPin, 440, 1000); // Play 440 Hz for 1 second } void loop() { // Empty }
4.2 Create a Melody
Generate a sequence of tones to create a melody.
const int buzzerPin = 8; // Notes and their frequencies (in Hz) const int melody[] = {262, 294, 330, 349, 392, 440, 494, 523}; // C4 to C5 const int noteDuration = 500; // Duration of each note (ms) void setup() { for (int i = 0; i < 8; i++) { tone(buzzerPin, melody[i], noteDuration); // Play each note delay(noteDuration + 100); // Pause between notes } } void loop() { // Empty }
4.3 Generate Alarms or Alerts
Create a repeating alert sound with alternating tones.
const int buzzerPin = 8; void setup() { // Empty } void loop() { tone(buzzerPin, 1000, 200); // Play 1000 Hz for 200 ms delay(300); tone(buzzerPin, 500, 200); // Play 500 Hz for 200 ms delay(300); }
4.4 Play a Scale
Play a musical scale by gradually increasing the frequency.
const int buzzerPin = 8; void setup() { int startFrequency = 262; // Start frequency (C4) for (int i = 0; i < 8; i++) { tone(buzzerPin, startFrequency); // Play the current frequency delay(500); startFrequency += 50; // Increase frequency for the next note } noTone(buzzerPin); // Stop the tone } void loop() { // Empty }
4.5 Play a Custom Song
Play “Twinkle Twinkle Little Star” using predefined notes and durations.
const int buzzerPin = 8; // Notes for "Twinkle Twinkle Little Star" const int melody[] = { 262, 262, 392, 392, 440, 440, 392, 349, 349, 330, 330, 294, 294, 262 }; // Note durations (4 = quarter note, 8 = eighth note) const int noteDurations[] = { 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2 }; void setup() { for (int i = 0; i < 14; i++) { int noteDuration = 1000 / noteDurations[i]; // Calculate note duration tone(buzzerPin, melody[i], noteDuration); delay(noteDuration + 50); // Pause between notes } } void loop() { // Empty }
5. Best Practices for Using the Tone Library
- Limit Duration:
- Use the duration parameter to stop tones automatically after a certain time.
- Avoid Conflicts:
- The tone() function blocks PWM output on pins 3 and 11 on some boards (e.g., Arduino Uno).
- Use noTone():
- Always call noTone() to stop the tone when it’s no longer needed.
- Combine with Delays:
- Use delay() between tones to create distinct notes in melodies.
- Test with Resistors:
- Use resistors with speakers to limit current and prevent damage to the Arduino pin.
- Optimize for Memory:
- For complex songs, use arrays for notes and durations instead of hardcoding values.
Conclusion
The Arduino Tone Library is a simple yet powerful tool for generating sounds and melodies.
Whether you’re creating alarms, musical projects, or audio feedback for devices, this library makes it easy to generate tones of varying frequencies and durations.
This tutorial provided a comprehensive overview of tone functions and included practical examples to get you started. For more details, visit the official Arduino reference.