Skip to main content

3. Making Sounds

Learning how to use a simple buzzer!

Materials

ComponentImage
Breadboard
Jumper wires
Arduino Uno R4 Minima
Buzzer

Instructions

  1. Place the buzzer in the middle of the breadboard.

  2. Connect the negative terminal to the ground and the postive terminal to pin 12.

  1. Paste the following code into your Arduino IDE:

Code

int buzzer = 12; // the pin of the active buzzer

void setup() {
pinMode(buzzer, OUTPUT); // initialize the buzzer pin as an output
}

void loop() {
int sound_duration = 500;
for (int i = 0; i < 20; i++) {
// use the if function to gradually shorten the interval of the sound
if (i < 5) {
sound_duration = 500;
} else if (i < 10) {
sound_duration = 300;
} else if (i < 20) {
sound_duration = 100;
}
// activate the active buzzer
digitalWrite(buzzer, HIGH);
delay(sound_duration); // wait for sound_duration ms
// deactivate the active buzzer
digitalWrite(buzzer, LOW);
delay(sound_duration); // wait for sound_duration ms
}
// activate the active buzzer
digitalWrite(buzzer, HIGH);
delay(5000); // keep playing sound for 5 seconds
}
  1. Upload the code and watch the buzzer sound!
PrevNext
2. Digital Input4. Passive Buzzer