In this Arduino project, we will be utilizing an active buzzer to generate sound. This tutorial will guide you through the process of connecting the buzzer to your Arduino and writing the necessary code to produce different sounds.

Connecting the Buzzer

To begin, connect the buzzer to your Arduino using the following steps:

  1. Attach the red wire to the + pole of the buzzer.
  2. Connect the - wire to the GND (ground) pin on the Arduino.
  3. Connect the + wire to a digital output pin on the Arduino, such as pin #9.

Refer to the provided images for visual guidance on the wiring process.

Writing the Arduino Code

Now let’s switch to the Arduino program and write the code to generate sound. Below is an example program that you can use:

int delay_ms = 5;
int buzzer_pin = 9;

void setup() {
 pinMode(buzzer_pin, OUTPUT);
}

void loop() {
 digitalWrite(buzzer_pin, HIGH);
 delay(delay_ms);
 digitalWrite(buzzer_pin, LOW);
 delay(delay_ms);
}

This code will cause the buzzer to emit a low sound. You can experiment with different sound frequencies by adjusting the value of the delay_ms variable.

For a more advanced approach, you can create a program that plays different sounds. Here is an example:

int buzzer_pin = 9;

void setup() {
 pinMode(buzzer_pin, OUTPUT);
}

void play(int ms, int delay1, int delay2) {
 for (int i = 1; i <= ms; i++) {
 digitalWrite(buzzer_pin, HIGH);
 delay(delay1);
 digitalWrite(buzzer_pin, LOW);
 delay(delay2);
 }
}

void loop() {
 play(100, 1, 1);
 play(100, 2, 2);
 play(100, 1, 1);
 play(100, 2, 2);
 play(100, 1, 1);
 play(50, 2, 1);
 play(100, 3, 2);
 play(100, 4, 4);
}

In this program, you can modify the play() function to adjust the duration and delays between sound tones. Experiment with different values to create unique sounds.

Conclusion

By following this tutorial, you have successfully learned how to use an active buzzer with your Arduino to generate various sounds. Have fun exploring and experimenting with different sound patterns by modifying the code provided.

Tags: Arduino project, active buzzer, sound generation, Arduino programming, buzzer wiring