Piezo Buzzer
A Piezo Buzzer is a device that makes sound. Buzzers can be used for alarms, music makers, or some other device that needs to make sound. On the buzzer, the positive end is the one with the ‘+’ sign over it. over it.
How to Wire a Piezo Buzzer
Wiring a piezo buzzer is wired just like an LED is. Put the buzzer on the breadboard, connect the positive end to a resistor and then to a digital pin on the Arduino, then connect the negative end to the GND pin on the Arduino.
How to Code a Piezo Buzzer
To code a buzzer, you will need to use the tone function. The tone function is structured like this: tone(pin #, frequency, time); The pin # is the pin on the Arduino that the buzzer is connected to. The frequency is the tone (frequency) that the buzzer will vibrate at in hertz(vibrations per second). The time is the amount of time that you want the tone to play for before stopping in milliseconds. For example, if you want to play a tone that is 500 hertz for 1 second, you would write: tone(8, 500, 1000);
Sample Code 1
The following code will make the buzzer vibrate at 550 hz for half a second then wait half a second and repeat:
Sample Code 2
The following code will play the happy birthday song, then wait 10 seconds, then repeat:
Challenges
- In sample code #1, try changing the frequency number to higher and higher values. What happens?
- Make the circuit for a button in addition to the buzzer circuit. Now look up the notes to your favorite song and play those notes if the button has been pressed.