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 one terminal on the buzzer to pin 8 on the Arduino.
  • Connect the other terminal on the buzzer to GND on the Arduino.
Tone connections
2 Create a new program by selecting File from the menu,
then select New.
3 Type in this program.









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.
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 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 (G, A, B, and C) 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.
Tone Melody
7 Upload and run the program. You should hear a song.