Intro to Arduino – Part 8 – Buzzers

In this section we will chew through two chapters from the Arduino starter kit’s tutorial, using two different kinds of buzzers.

Download the pages of chapter 7 from the tutorial here:

Code for chapter 7:



int buzzer = 12;//the pin of the active buzzer
void setup()
{
 pinMode(buzzer,OUTPUT);//initialize the buzzer pin as an output
}
void loop()
{
 unsigned char i;
 while(1)
 {
   //output an frequency
   for(i=0;i<80;i++)
   {
    digitalWrite(buzzer,HIGH);
    delay(1);//wait for 1ms
    digitalWrite(buzzer,LOW);
    delay(1);//wait for 1ms
    }
    //output another frequency
     for(i=0;i<100;i++)
      {
        digitalWrite(buzzer,HIGH);
        delay(2);//wait for 2ms
        digitalWrite(buzzer,LOW);
        delay(2);//wait for 2ms
      }
  }
} 

Too easy? Next up we will use a passive buzzer, which has a bit of a curve-ball to it…

Libraries

It should come as no surprise that some components are trickier to control than others. Turning a single LED on and off is relatively straightforward, controlling an LCD display or sending a picture to some kind of TV or monitor is significantly more complex.

Many times there are libraries out there that can make it easier for us to use more complicated parts.

To use the passive version of the buzzer, we will use a library somebody else was nice enough to put together for us. Libraries are typically referenced at the start of your Arduino sketch/code, as shown at the top of the passive buzzer code for chapter 8 below:



#include "pitches.h"
 
// notes in the melody:
int melody[] = {
  NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_B5, NOTE_C6};
int duration = 500;  // 500 miliseconds
 
void setup() {
 
}
 
void loop() {  
  for (int thisNote = 0; thisNote < 8; thisNote++) {
    // pin8 output the voice, every scale is 0.5 sencond
    tone(8, melody[thisNote], duration);
     
    // Output the voice after several minutes
    delay(1000);
  }
   
  // restart after two seconds 
  delay(2000);
}

pitches.h” is the library we are pulling in to help us control the passive buzzer. When programming for robotics you will use many libraries from external sources to help you use common components such as servos and sensors. It would be unrealistic to write the code to control something like a color sensor from scratch, at least at this point in your programming career.

codebender is really doing a lot of swell work for us here, because “pitches.h” is already in its internal collection of libraries and it already knows where to get it just by referencing it at the top of our program. Don’t get too used to this, most of the time you have to hunt down the library yourself and pull it into your code project in a much more manual fashion, but we’ll enjoy this while it lasts.

So you have the code for chapter 8 above, here are the pages for chapter 8 from the tutorial:

As usual, follow the steps to set up the component on your breadboard and load up the code to make it run.

We are relying on codebender keeping a compatible version of the “pitches” library on hand so let us know if you run into any problems.

If your musical buzzer is now working, try playing with the code a little to change the tune and see what you can come up with.

Once you’ve had your fun with that, in the next section we are going to play with the temperature sensor in the kit, and learn about the serial monitor.