Buzzer
A buzzer is a tiny loudspeaker. Switch its pin on and off very fast, say 1000 times a second, and you hear a note. The Arduino does that for you with tone: a higher number, a higher note. noTone makes it quiet.
The leg under the + mark to a pin, the other to GND. tone(pin, 1000) sounds until noTone(pin).
More: beeps, notes and the two kinds of buzzer
const int buzzerPin = 8; void setup() { pinMode(buzzerPin, OUTPUT); } void loop() { tone(buzzerPin, 1000); // on, 1000 Hz delay(60); noTone(buzzerPin); // off delay(400); // the gap: shorter gap, faster beeping }
A beep is a short tone and a gap. Change the gap and the beeping gets faster or slower; change the number in tone and the note gets higher or lower. The notes of a song are just numbers: 262 is a C, 294 a D, 330 an E, 392 a G, 440 an A.
Two kinds: a passive buzzer (ours) needs tone to make the swings; an active buzzer has the swings built in and sounds as soon as its pin is high, always at the same note. If yours beeps with a plain digitalWrite HIGH, it is an active one; tone works with both kinds.