Buzzer Program

Lets make some sound

In the previous program, we made LED blink every one second. In this program, we will only modify the LED pin to create an audible beep every second.

/*
* Buzzer Program: The program below beeps a buzzer every one second, 
* then off for one second, and continuous endlessly. Buzzer is connected to Pin 9
*
* At VEEROBOT, we invest time and resources providing this open source code,
* Please support VEEROBOT and open-source hardware by purchasing products
* from us @ http://veerobot.com
* -----------------------------------------------------------------------------
* You are free to redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as  published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This Code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* See <http://www.gnu.org/licenses/>
*/

int BUZZ = 9;  // Buzzer is connected to Pin 9

void setup() 
{
  // Initialize digital pin as output
  pinMode(BUZZ, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(BUZZ, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(BUZZ, LOW);   // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

The above program might be boring at its best. All it does is beep every one second. We will upgrade our program to create some interesting music.

We will have to map the notes to create our micro:Xbot monophonic music. For that we will create a pitches.h file.

Now that we have the pitches for our buzzer, we will modify our buzzer program and create some music.

Compile, Upload and listen to music. The above program uses a built-in tone() library to generate tones.

Change melody[] to below code and guess the music. You may also need to change tempo to 200 to make the music run slightly faster.

Last updated