Making Music

In this project, you'll learn how to generate sound with the Arduino.

Parts needed: Software stuff you'll learn: Arduino      Buzzer      Breadboard     
1 Making the connections
  • Connect the buzzer with the two pins to columns 40 and 43 on the breadboard. There'll be two holes in between them. You might have to very gently squeeze the two pins slightly apart in order to fit.
    The pins break very easily so be very gentle when you squeeze them apart.
  • Connect one terminal on the buzzer to pin 8 on the Arduino. To do this, plug one end of a wire into a hole below column 40 on the breadboard, and plug the other end of the wire to pin 8 on the Arduino.
  • Connect the other terminal on the buzzer to GND on the Arduino. To do this, plug one end of a wire into a hole below column 43 on the breadboard, and plug the other end of the wire to GND on the Arduino.
Tone connections
2 Create a new program by selecting File from the menu, and then select New.

You will see this new template.
BareMinimum template
3 Type in this program.









The pinMode command tells the Arduino that our buzzer is connected to pin 8.

The tone command generates a music note in the given frequency, in this case 440 Hz, through the specified pin number, in this case it is pin 8. The frequence 440 Hz corresponds to the A note on the piano.

The delay command causes the note to play for 1 second before turning it off with the noTone command.

The noTone command stops the sound from the buzzer that is connected to pin 8.
Tone Melody
4 Upload and run the program. You should hear a note for one second.
5 Change the number 440 in the tone command to 523.
Upload and run the program again.
What do you notice? Experiment with other numbers.
6 Modify the program to make it play two notes, one after the other.
7 This next program will play several notes of a song. Create a new program by selecting File from the menu, then select New. Type in this program.






The frequencies for the four notes (NOTE_G3, NOTE_A3, NOTE_B3, and NOTE_C4) are assigned to a variable name using the int command. For example, the frequency 196 is assigned to the name NOTE_G3. Later in the code, we can use the name instead of the number, which is easier to remember.

A variable name must start with a letter and cannot have any spaces.
void setup() {
  int NOTE_G3 = 196;
  int NOTE_A3 = 220;
  int NOTE_B3 = 247;
  int NOTE_C4 = 262;

  pinMode(8, OUTPUT);
  tone(8, NOTE_C4);
  delay(250);
  tone(8, NOTE_G3);
  delay(125);
  noTone(8);
  delay(75);
  tone(8, NOTE_G3);
  delay(125);
  tone(8, NOTE_A3);
  delay(250);
  tone(8, NOTE_G3);
  delay(250);
  noTone(8);
  delay(250);
  tone(8, NOTE_B3);
  delay(250);
  tone(8, NOTE_C4);
  delay(400);
  noTone(8);
}

void loop() {
}
8 Upload and run the program. You should hear a song.