
Using a Piezo Buzzer — SIK Circuit 11
Play a song with a piezo buzzer! Learn about the tone() function, musical frequencies, arrays for storing melodies, and how to create your own tunes.
안내
Parts & Introduction
Parts & Introduction
A piezo buzzer converts electrical signals into sound. Using Arduino's built-in tone() function, you can play specific musical frequencies. This sketch plays a simple melody using character arrays for notes and integer arrays for timing.
Parts Needed
- 1x Arduino Uno + USB cable
- 1x Breadboard
- 1x Piezo Buzzer
- 3x Jumper Wires
Note Frequency Reference
| Note | Freq |
|---|---|
| c | 262 Hz |
| d | 294 Hz |
| e | 330 Hz |
| f | 349 Hz |
| g | 392 Hz |
| a | 440 Hz |
| b | 494 Hz |
| C | 523 Hz |
Hardware Hookup
Hardware Hookup
Wiring Instructions
- Place the piezo buzzer in the breadboard. It has two pins — one marked with "+".
- Connect the positive pin (+) to Arduino Digital Pin 9 (PWM).
- Connect the negative pin to GND.
Tip: If the buzzer doesn't fit easily, try rotating it slightly to align with diagonal breadboard holes.
Arduino Code
Arduino Code
Open the Arduino IDE and upload the following sketch to your Arduino board.
/*
SparkFun Inventor's Kit
Example sketch 11 — BUZZER
Use the buzzer to play a song!
Hardware connections:
Buzzer positive pin (+) to digital pin 9 (PWM)
Buzzer negative pin to GND
This code is completely free for any use.
*/
const int buzzerPin = 9;
const int songLength = 18;
char notes[] = "cdfda ag cdfdg gf "; // space = rest
int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};
int tempo = 113;
void setup()
{
pinMode(buzzerPin, OUTPUT);
}
void loop()
{
int i, duration;
for (i = 0; i < songLength; i++)
{
duration = beats[i] * tempo;
if (notes[i] == ' ')
{
delay(duration);
}
else
{
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
}
delay(tempo/10);
}
while(true){} // Play once, then stop. Remove this line to loop.
}
int frequency(char note)
{
int i;
const int numNotes = 8;
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
for (i = 0; i < numNotes; i++)
{
if (names[i] == note)
{
return(frequencies[i]);
}
}
return(0);
}Test & Experiment
Test & Experiment
What You Should See
You won't see anything — but you'll hear a melody playing from the buzzer! The song plays once and stops.
Troubleshooting
- No sound: The buzzer pins are easy to misplace on the breadboard. Double-check placement and make sure both pins have good contact.
- Want it to stop: Pull the buzzer out while working on other things, plug it back in to test.
Experiments to Try
- Remove the
while(true){}line to make the song loop continuously. - Write your own melody by changing the
notes[]andbeats[]arrays. - Add more notes by expanding the
frequency()function with sharps and flats. - Use a button (from Circuit 5) to trigger the melody on demand.
재료
- •SparkFun Inventor's Kit - V3.2 - 1 kitNOK 999.20
- •Arduino Uno R3 - 1 piece플레이스홀더
- •Breadboard - 1 piece플레이스홀더
- •Piezo Buzzer - 1 piece
- •Jumper Wires - 3 piecessNOK 39.20
필요 도구
- Computer with Arduino IDE
CC0 퍼블릭 도메인
이 블루프린트는 CC0로 공개되었습니다. 어떤 목적으로든 자유롭게 복사, 수정, 배포 및 사용할 수 있습니다.
제품 구매를 통해 메이커를 지원하세요. 판매자가 설정한 메이커 커미션 을 받거나, 이 블루프린트의 새로운 반복을 만들어 연결로 포함시킬 수 있습니다.