Overview
Sound adds a whole new dimension to your projects! Whether it's creating alerts, composing melodies, or simulating real-world sounds, the piezo buzzer on your Vumbua Synapse Kit opens up endless possibilities.
In this lesson, you'll learn how to generate different tones, play musical notes,
create melodies, and even produce sound effects using the tone() function.
Buzzer applications are everywhere:
- Alarm systems and security devices
- Electronic toys and games
- Musical instruments and keyboards
- Notification alerts in devices
- Medical equipment indicators
- Accessibility devices for visually impaired
Prerequisites
Before starting, make sure you have completed:
Completed Lessons
- Lesson 1: Blinking LED
- Lesson 2: Push Button Input
- Understanding of
setup()andloop() - Basic knowledge of variables and functions
Hardware Requirements
- Vumbua Synapse Kit with buzzer
- USB Type-C cable
- Computer with Arduino IDE
- Headphones or quiet environment
The piezo buzzer is pre-connected to the Vumbua board on Digital Pin 8. You can start making sounds immediately - no additional wiring needed!
Learning Objectives
By the end of this lesson, you will be able to:
- Understand how piezo buzzers generate sound
- Use
tone()andnoTone()functions - Generate different frequencies and tones
- Play musical notes and simple melodies
- Create sound effects for projects
- Understand frequency and duration in sound generation
- Control sound volume using PWM
- Combine sound with other components (LEDs, buttons)
What is a Piezo Buzzer?
The Piezoelectric Effect
Piezo buzzers use the piezoelectric effect - when certain materials (like crystals or ceramics) change shape when voltage is applied.
- Apply voltage → Material expands
- Remove voltage → Material contracts
- Rapid changes create sound waves
This vibration produces sound that we can hear!
Types of Buzzers
There are two main types:
- Active Buzzers: Have internal oscillator, produce single tone
- Passive Buzzers: Need external signal, can produce multiple tones
The Vumbua Kit uses a passive piezo buzzer, giving you full control over the sound!
How Sound is Created
When you apply a rapidly changing signal:
- Arduino sends PWM signal to buzzer
- Piezo element vibrates at same frequency
- Vibrations create pressure waves in air
- Our ears detect these as sound
Higher frequency = Higher pitch, Lower frequency = Lower pitch
Buzzer Connections
The piezo buzzer on Vumbua Synapse Kit has 2 pins:
The passive buzzer requires an oscillating signal to produce sound. It won't make
sound with just DC voltage applied. That's why we use the tone() function
which creates the necessary square wave.
Pin 8 is PWM-capable, allowing for volume control through duty cycle adjustment.
Understanding Frequency
Frequency & Pitch
Frequency = Number of vibrations per second, measured in Hertz (Hz)
Pitch = How high or low a sound is (perceived frequency)
Human Hearing Range
Most humans can hear frequencies between 20 Hz and 20,000 Hz.
- 20-250 Hz: Bass frequencies
- 250-2000 Hz: Mid-range frequencies
- 2000-20000 Hz: Treble frequencies
Buzzer Limitations
The piezo buzzer on Vumbua works best between:
However, the practical range for clear tones is 50 Hz to 5000 Hz.
Using tone() Function
The tone() function is your main tool for sound generation:
Function Syntax
tone(pin, frequency); // Play tone indefinitely tone(pin, frequency, duration); // Play tone for specific duration noTone(pin); // Stop the tone
Where pin is the buzzer pin, frequency is in Hz,
and duration is in milliseconds.
Important Notes
- Only one tone can play at a time
tone()uses Timer 2 on ATmega328P- It generates a 50% duty cycle square wave
- Always use
noTone()to stop sound - Works on most digital pins (2-13 on Vumbua)
Volume Control
For volume control, you can use PWM with analogWrite():
analogWrite(buzzerPin, 128); // 50% volume tone(buzzerPin, 440); // Play A4 note at 50% volume
However, note that combining PWM with tone() can be tricky
as they both use timers.
Basic Sound Generation
Let's start with a simple program to test different sounds:
buzzer_basic.ino
/* Vumbua Synapse Kit - Lesson 3: Basic Buzzer Sounds Test different frequencies and durations */ const int buzzerPin = 8; // Buzzer connected to digital pin 8 void setup() { Serial.begin(9600); pinMode(buzzerPin, OUTPUT); Serial.println("Buzzer Sound Test Program"); Serial.println("=========================="); Serial.println("Testing different frequencies..."); } void loop() { Serial.println("\n=== Test 1: Low Pitch (100 Hz) ==="); tone(buzzerPin, 100, 1000); // 100 Hz for 1 second delay(1500); // Wait 1.5 seconds (includes 0.5s pause) Serial.println("=== Test 2: Middle Pitch (440 Hz) ==="); tone(buzzerPin, 440, 1000); // 440 Hz (A4) for 1 second delay(1500); Serial.println("=== Test 3: High Pitch (1000 Hz) ==="); tone(buzzerPin, 1000, 1000); // 1000 Hz for 1 second delay(1500); Serial.println("=== Test 4: Very High Pitch (4000 Hz) ==="); tone(buzzerPin, 4000, 1000); // 4000 Hz for 1 second delay(1500); Serial.println("=== Test 5: Siren Effect ==="); // Rising pitch siren for (int i = 200; i <= 800; i += 50) { tone(buzzerPin, i, 100); delay(150); } // Falling pitch siren for (int i = 800; i >= 200; i -= 50) { tone(buzzerPin, i, 100); delay(150); } delay(1000); }
Testing the Program
1. Upload the Code
Upload to your Vumbua Synapse Kit. The buzzer is already connected to pin 8 on the board.
2. Listen Carefully
You should hear five different sound tests:
- Test 1: Low rumbling sound (100 Hz)
- Test 2: Standard A4 musical note (440 Hz)
- Test 3: Higher pitched tone (1000 Hz)
- Test 4: Very high pitch (4000 Hz)
- Test 5: Rising and falling siren effect
3. Open Serial Monitor
Go to Tools → Serial Monitor or press Ctrl+Shift+M.
Set the baud rate to 9600 to see the test descriptions.
Piezo buzzers produce simple square wave tones, not rich musical sounds. They're great for alerts, simple melodies, and sound effects, but don't expect high-fidelity audio!
Musical Notes & Scales
Here are the frequencies for common musical notes (equal temperament, A4 = 440 Hz):
| Octave | C | D | E | F | G | A | B |
|---|---|---|---|---|---|---|---|
| 3 | 131 Hz | 147 Hz | 165 Hz | 175 Hz | 196 Hz | 220 Hz | 247 Hz |
| 4 | 262 Hz | 294 Hz | 330 Hz | 349 Hz | 392 Hz | 440 Hz | 494 Hz |
| 5 | 523 Hz | 587 Hz | 659 Hz | 698 Hz | 784 Hz | 880 Hz | 988 Hz |
| 6 | 1047 Hz | 1175 Hz | 1319 Hz | 1397 Hz | 1568 Hz | 1760 Hz | 1976 Hz |
In music, notes have different durations. Here's a quick reference:
- Whole Note: 4 beats (e.g., 2000 ms at 120 BPM)
- Half Note: 2 beats
- Quarter Note: 1 beat (500 ms at 120 BPM)
- Eighth Note: ½ beat
- Sixteenth Note: ¼ beat
For Arduino melodies, we typically use milliseconds for duration.
Creating Melodies
Now let's create a simple melody - "Twinkle Twinkle Little Star":
twinkle_twinkle.ino
/* Vumbua Synapse Kit - Lesson 3: Simple Melody Play "Twinkle Twinkle Little Star" */ const int buzzerPin = 8; // Notes in the melody (frequencies in Hz) int melody[] = { 262, 262, 392, 392, 440, 440, 392, // C C G G A A G 349, 349, 330, 330, 294, 294, 262, // F F E E D D C 392, 392, 349, 349, 330, 330, 294, // G G F F E E D 392, 392, 349, 349, 330, 330, 294, // G G F F E E D 262, 262, 392, 392, 440, 440, 392, // C C G G A A G 349, 349, 330, 330, 294, 294, 262 // F F E E D D C }; // Note durations: 4 = quarter note, 8 = eighth note, etc. int noteDurations[] = { 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2 }; void setup() { Serial.begin(9600); pinMode(buzzerPin, OUTPUT); Serial.println("Playing: Twinkle Twinkle Little Star"); } void loop() { // Calculate the number of notes in the melody int numberOfNotes = sizeof(melody) / sizeof(melody[0]); for (int thisNote = 0; thisNote < numberOfNotes; thisNote++) { // Calculate note duration (tempo = 120 BPM) int noteDuration = 1500 / noteDurations[thisNote]; // Play the note tone(buzzerPin, melody[thisNote], noteDuration); // Display note information Serial.print("Note "); Serial.print(thisNote + 1); Serial.print(": Frequency = "); Serial.print(melody[thisNote]); Serial.print(" Hz, Duration = "); Serial.print(noteDuration); Serial.println(" ms"); // Pause between notes (adds a little space) int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // Stop the tone playing noTone(buzzerPin); } Serial.println("\nMelody complete! Restarting in 3 seconds..."); delay(3000); }
Melody Player
Click the buttons to hear different sounds:
Sound Effects
Create fun sound effects for your projects:
sound_effects.ino
/* Vumbua Synapse Kit - Lesson 3: Sound Effects Various sound effects for projects */ const int buzzerPin = 8; const int buttonPin = 2; // Optional button for trigger void setup() { Serial.begin(9600); pinMode(buzzerPin, OUTPUT); pinMode(buttonPin, INPUT_PULLUP); Serial.println("Sound Effects Demo"); Serial.println("Press button (pin 2) to play effects"); } void loop() { if (digitalRead(buttonPin) == LOW) { Serial.println("Button pressed! Playing random effect..."); // Randomly select an effect (1-6) int effect = random(1, 7); switch (effect) { case 1: playBeep(); break; case 2: playSiren(); break; case 3: playLaser(); break; case 4: playAlarm(); break; case 5: playSuccess(); break; case 6: playError(); break; } delay(500); // Debounce delay } } // Sound effect functions void playBeep() { Serial.println("Effect: Short Beep"); tone(buzzerPin, 1000, 200); } void playSiren() { Serial.println("Effect: Police Siren"); for (int i = 300; i <= 1500; i += 50) { tone(buzzerPin, i, 20); delay(20); } for (int i = 1500; i >= 300; i -= 50) { tone(buzzerPin, i, 20); delay(20); } noTone(buzzerPin); } void playLaser() { Serial.println("Effect: Laser Shot"); for (int i = 2000; i >= 100; i -= 50) { tone(buzzerPin, i, 10); } noTone(buzzerPin); } void playAlarm() { Serial.println("Effect: Alarm Clock"); for (int j = 0; j < 3; j++) { for (int i = 800; i <= 1200; i += 50) { tone(buzzerPin, i, 50); delay(50); } delay(200); } noTone(buzzerPin); } void playSuccess() { Serial.println("Effect: Success Chime"); tone(buzzerPin, 523, 150); // C5 delay(200); tone(buzzerPin, 659, 150); // E5 delay(200); tone(buzzerPin, 784, 300); // G5 noTone(buzzerPin); } void playError() { Serial.println("Effect: Error Sound"); tone(buzzerPin, 200, 300); delay(400); tone(buzzerPin, 150, 500); noTone(buzzerPin); }
Advanced Techniques
Volume Control with PWM
While tone() doesn't support volume control directly,
you can use PWM pins to adjust volume:
// Volume control example
const int buzzerPin = 3; // Must be a PWM pin
void setup() {
pinMode(buzzerPin, OUTPUT);
}
void loop() {
// Play note at different volumes
for (int volume = 0; volume <= 255; volume += 5) {
analogWrite(buzzerPin, volume); // Set volume (0-255)
tone(buzzerPin, 440); // Play A4
delay(50);
}
noTone(buzzerPin);
delay(1000);
}
Multiple Simultaneous Tones
Arduino can only play one tone at a time with tone().
For multiple simultaneous tones, you need:
- Multiple buzzers on different pins
- External sound generation hardware
- Software-based sound synthesis (complex)
A simple workaround is to rapidly switch between tones to simulate chords.
Sound Synthesis
For more complex sounds, you can create waveforms manually:
// Simple square wave generation
void playSquareWave(int pin, int frequency, int duration) {
int period = 1000000 / frequency; // Period in microseconds
int halfPeriod = period / 2;
long startTime = micros();
while (micros() - startTime < duration * 1000) {
digitalWrite(pin, HIGH);
delayMicroseconds(halfPeriod);
digitalWrite(pin, LOW);
delayMicroseconds(halfPeriod);
}
}
Using pitches.h Library
Arduino has a built-in pitches.h file that defines musical note
frequencies. Here's how to use it:
pitches_demo.ino
/* Vumbua Synapse Kit - Lesson 3: Using pitches.h Requires pitches.h file in your sketch folder */ #include "pitches.h" // Include the pitches library const int buzzerPin = 8; // Mario theme melody (first few notes) int melody[] = { NOTE_E5, NOTE_E5, 0, NOTE_E5, 0, NOTE_C5, NOTE_E5, NOTE_G5, 0, NOTE_G4, 0 }; // Note durations int noteDurations[] = { 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4 }; void setup() { Serial.begin(9600); pinMode(buzzerPin, OUTPUT); } void loop() { int numberOfNotes = sizeof(melody) / sizeof(melody[0]); for (int thisNote = 0; thisNote < numberOfNotes; thisNote++) { int noteDuration = 1000 / noteDurations[thisNote]; if (melody[thisNote] == 0) { // Rest (pause) delay(noteDuration); } else { // Play note tone(buzzerPin, melody[thisNote], noteDuration); delay(noteDuration * 1.3); noTone(buzzerPin); } } delay(2000); }
Create a new tab in Arduino IDE called "pitches.h" and add this content:
/************************************************* * Public Constants (pitches.h) *************************************************/ #define NOTE_B0 31 #define NOTE_C1 33 #define NOTE_CS1 35 #define NOTE_D1 37 // ... (complete list available in Arduino examples) #define NOTE_A6 1760 #define NOTE_AS6 1865 #define NOTE_B6 1976 #define NOTE_C7 2093 #define NOTE_CS7 2217 #define NOTE_D7 2349 #define NOTE_DS7 2489 #define NOTE_E7 2637 #define NOTE_F7 2794 #define NOTE_FS7 2960 #define NOTE_G7 3136 #define NOTE_GS7 3322 #define NOTE_A7 3520 #define NOTE_AS7 3729 #define NOTE_B7 3951 #define NOTE_C8 4186 #define NOTE_CS8 4435 #define NOTE_D8 4699 #define NOTE_DS8 4978
Troubleshooting Common Issues
- Check: Buzzer is connected to pin 8 on Vumbua
- Verify: Using
tone()notdigitalWrite() - Test: Try different frequencies (some may be outside hearing range)
- Listen: Buzzer may be very quiet - bring ear close
- Cause: Frequency too low (below 50 Hz)
- Solution: Use frequencies between 50-5000 Hz
- Check: Duration may be too short
- Try: Increase frequency to 200+ Hz
- Check: Power supply is stable (use USB power)
- Try: Different frequency ranges
- Consider: Add a small capacitor across buzzer pins
- Test: Check for loose connections
Quick Diagnostic Test
// Simple diagnostic test
void diagnosticTest() {
Serial.println("Running buzzer diagnostic...");
// Test 1: Basic beep
Serial.println("Test 1: 1000 Hz beep");
tone(8, 1000, 500);
delay(1000);
// Test 2: Frequency sweep
Serial.println("Test 2: Frequency sweep 100-1000 Hz");
for (int freq = 100; freq <= 1000; freq += 50) {
tone(8, freq, 100);
delay(120);
}
// Test 3: Musical notes
Serial.println("Test 3: C Major scale");
int scale[] = {262, 294, 330, 349, 392, 440, 494, 523};
for (int note : scale) {
tone(8, note, 300);
delay(350);
}
Serial.println("Diagnostic complete!");
noTone(8);
}
Video Demonstration
Video Tutorial Coming Soon!
We're preparing a detailed video walkthrough for this lesson.
Subscribe to our YouTube channel to be notified when it's available.
Subscribe to OLSPACEWhat's Next?
Congratulations! You've successfully mastered sound generation with buzzers. Here's what you've accomplished:
- ✓ Learned how piezo buzzers generate sound
- ✓ Used
tone()andnoTone()functions - ✓ Generated different frequencies and tones
- ✓ Played musical notes and simple melodies
- ✓ Created sound effects for projects
- ✓ Understood frequency and duration in sound generation
- ✓ Combined sound with other components
In the next lesson, you'll explore analog input:
Lesson 4: Potentiometer - Analog Input
Learn to read variable voltage using a potentiometer! Create interactive dials,
volume controls, and variable brightness LEDs with your Vumbua Synapse Kit.
Discover how to use analogRead() and map() functions.
Resources & Downloads
Download Complete Code
Get all the code files for this lesson: