Introduction
In this experiment, you will learn how to generate tones with a piezo buzzer using an Arduino. By changing the frequency, you can play different pitches and create simple tunes.
Components Needed
Circuit Setup
- Place the piezo buzzer on the breadboard.
- Connect the positive pin of the piezo buzzer (marked with a "+" symbol) to digital pin 8 on the Arduino R4 Minima.
- Connect the negative pin of the buzzer to the ground (GND) on the Arduino.
Code for Piezo Buzzer Tone Generator
Upload the following code to your Arduino to generate different tones with the piezo buzzer:
// Define the pin for the piezo buzzer
const int buzzerPin = 8;
// Note frequencies in Hz
const int NOTE_C = 262;
const int NOTE_D = 294;
const int NOTE_E = 330;
const int NOTE_F = 349;
const int NOTE_G = 392;
const int NOTE_A = 440;
const int NOTE_B = 494;
void setup() {
// No setup needed for this experiment
}
void loop() {
// Play each note for 500 ms
tone(buzzerPin, NOTE_C);
delay(500);
tone(buzzerPin, NOTE_D);
delay(500);
tone(buzzerPin, NOTE_E);
delay(500);
tone(buzzerPin, NOTE_F);
delay(500);
tone(buzzerPin, NOTE_G);
delay(500);
tone(buzzerPin, NOTE_A);
delay(500);
tone(buzzerPin, NOTE_B);
delay(500);
// Pause for 1 second
noTone(buzzerPin);
delay(1000);
}
Explanation
This code uses the tone()
function to send specific frequencies to the piezo buzzer, generating musical notes. Each note is played for 500 milliseconds, with a 1-second pause between cycles.
The noTone()
function stops the sound, creating a brief silence before the sequence repeats.
Troubleshooting
- If the buzzer does not produce sound, double-check the connections to the Arduino and ensure that the correct pin is assigned.
- If the tones are too soft, try using a different piezo buzzer or adjusting the delay time.
- Experiment with different note frequencies to create custom sounds or tunes.