Arduino project: use an active buzzer

In this project we’re going to use the Arduino to generate a sound using an active buzzer.

First connect the buzzer to a wire:

The buzzer has a + pole, I used the red wire for that (a good habit).

Then connect the – wire to GND on…


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

In this project we’re going to use the Arduino to generate a sound using an active buzzer.

First connect the buzzer to a wire:

The buzzer has a + pole, I used the red wire for that (a good habit).

Then connect the - wire to GND on the Arduino, and the + wire to a digital output pin, in this case I picked pin #9:

Now we switch to the Arduino program. To generate a sound we need to write a HIGH value to the buzzer + pin, delay for a tiny amount of time, for example a millisecond, then write a LOW value on the same pin:

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);
}

Load the program on the Arduino and the buzzer will emit a low sound.

Try changing the delay_ms variable value to change the sound.

You can then go fancy by making it play different sounds, with a program like this:

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);

}


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-03-25T05:00:00+00:00) Arduino project: use an active buzzer. Retrieved from https://www.scien.cx/2021/03/25/arduino-project-use-an-active-buzzer/

MLA
" » Arduino project: use an active buzzer." flaviocopes.com | Sciencx - Thursday March 25, 2021, https://www.scien.cx/2021/03/25/arduino-project-use-an-active-buzzer/
HARVARD
flaviocopes.com | Sciencx Thursday March 25, 2021 » Arduino project: use an active buzzer., viewed ,<https://www.scien.cx/2021/03/25/arduino-project-use-an-active-buzzer/>
VANCOUVER
flaviocopes.com | Sciencx - » Arduino project: use an active buzzer. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/25/arduino-project-use-an-active-buzzer/
CHICAGO
" » Arduino project: use an active buzzer." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/03/25/arduino-project-use-an-active-buzzer/
IEEE
" » Arduino project: use an active buzzer." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/03/25/arduino-project-use-an-active-buzzer/. [Accessed: ]
rf:citation
» Arduino project: use an active buzzer | flaviocopes.com | Sciencx | https://www.scien.cx/2021/03/25/arduino-project-use-an-active-buzzer/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.