Project 3: Music Box

Posted by Rhiannon L on

Table of Contents


Introduction

When I was younger, I had a music box that would play a short song after a handle on the outside was wound. Today, you’ll learn to program something similar: an Arduino that will play a tune when you press the button! This project uses a buzzer, which is a small piece of technology that makes a little "click" when you apply voltage to it (try it!). By itself that isn't terribly exciting, but if you turn the voltage on and off hundreds of times a second, the buzzer will produce a tone, and if you string together a bunch of tones, you've got music!

This project also combines and applies your understanding of the last project, Project 2: Rocket Launch Initiator. If you haven’t tried it yet, it is recommended you do so before starting on this project.

Many modern megaphones have settings that use a loud amplified buzzer. They are usually very loud and quite good at getting people’s attention.


 Project 3 Circuit Diagram



03 Music Box


Parts List

This series of posts will guide you through your journey with Arduino with simple, easy-to-follow projects to help you get the basics down. Today’s post is about helping you understand your Arduino and get set up.

For this project, you’ll need the following items:


For a full list of all the parts you'll need for this series of projects, visit our introductory blog, An Introduction to Arduino.


5V warning

5V Current. Your Arduino runs on five volts. 5V can’t hurt you, so don’t be afraid to touch anything in your circuit. This is the power that will be supplied from your computer via USB and will be the driving force behind any components you use in your circuits. By plugging your Arduino board into your computer, you are supplying it with just the right voltage it needs to thrive!



There are three main steps to any Arduino project:
ASSEMBLE
the circuit
WRITE
the code
UPLOAD & RUN
See it work!


Assemble the Board

Set up your Arduino and breadboard. Refer to An Introduction to Arduino for help with this.

Before assembling, unplug your Arduino from the power source so that there's no power in the circuit. While 5V is not enough energy to hurt you, it can damage your Arduino if you wire it incorrectly while there's power in the circuit.

When assembling, make sure the pieces are in the right orientation, so that power is flowing in the right direction. This is important for the LED and the buzzer.

Wiring Diagram 2

Write the Code

Copy the code below carefully; make sure that you haven’t made any syntax (coding punctuation) errors that would cause the program to not work.


int numOfNotes = 8;

int melody[] = {262, 196, 196, 220, 196, 0, 247, 26};

int durations[] = {4, 8, 8, 4, 4, 4, 4, 4};

void setup() {

  pinMode(11, OUTPUT);

  pinMode(8, OUTPUT);

  pinMode(2, INPUT);

}

void loop() {

  if (digitalRead(2) == HIGH) {

    digitalWrite(11, HIGH);

    for (int i = 0; i < numOfNotes; i++) {

      int duration = 1000 / durations[i];

      tone(8, melody[i], duration);

      int pause = duration * 1.30;

      delay(pause);

      noTone(8);

    }

  } else {

    digitalWrite(11, LOW);

  }

}


Explanation of the Code

int numOfNotes = 8; - this line creates an integer variable (using the keyword “int”) called numOfNotes. numOfNotes is used to remember the number of notes in the “song,” 8 in this case.


int melody[] = {...} - creates an integer (keyword “int”) array (a list) that stores all the integers inside of the { } brackets, in this case, these are the notes to be played.


int noteDurations[] = {...} - creates an integer array that holds each of the lengths of the notes to be played, written inside the { } brackets. A length of 1 plays a whole note, 2 – half note, 4 – quarter note, and 8 – eighth note.

For this code to work, the number of elements or items in both arrays must match.


for (...) {

} - this is a for() loop. As you may have noticed with void loop(), loops are essential to almost any code that you will write. We won’t go into the details of the for loop function here, but we are using it to loop through and play each note in our tune.


tone(8, melody[i], duration); - the tone function is a built-in function (meaning it was created by Arduino for Arduino users to use). It is used to play a tone with a buzzer. The first value is the pin in which the buzzer is attached to, the second value is the frequency, and the last value is the length/duration of the tone.


Musical Tones

Note G3 A3 B3 C4 D4 E4 F4 G4 A4 B4 C5
Freq. 196 220 247 262 294 330 349 392 440 494 523


Upload & Run

Once you’ve written the code, and assuming you’ve assembled and written everything correctly, your project should be ready to run! Press the following buttons:

1 Verify This compiles your code. The IDE changes it from text into instructions the computer can understand.
2 Upload This sends the instructions via the USB cable to the computer chip on the Arduino board. The Arduino will then begin running your code automatically


What You Should See

You should see... well, nothing less than what you saw in Project 2! In addition, however, you should be able to hear a song. If it isn't working, make sure you have assembled the circuit correctly and securely, and that you pressed the “Verify” and “Upload” buttons. If it’s still not working, try the troubleshooting tips below.


Troubleshooting

No sound?

Given the size and shape of the piezo buzzer it is easy to put it in the wrong holes in the breadboard. Try double checking its placement.


Other components

Check Project 1 for LED troubleshooting tips.

Check Project 2 for button troubleshooting tips.


Still no success?

Remove everything from the breadboard and build the circuit again. If you are still having problems, send us an e-mail at support@carobot.ca and we will get back to you as soon as we can.


Exercises to Try

  1. Refer to the "Musical Tones" table under "Write the Code" to modify the code to play a tune using these parameters:

Tone: E4, D4, C4, D4, E4, E4, E4, D4, D4, D4, E4, G4, G4

Duration: 4, 4, 4, 4, 4, 4, 2, 4, 4, 2, 4, 4, 2

What is the tune?

Hint: you’ll need to change the value of the numOfNotes variable.

  1. Play the tune faster by a factor of 2.
  2. Modify the code so the LED blinks with the tone instead of always ON when the tone is playing.

Were you able to recognize the music playing? As stated in the introduction, there’s sound all around us, and buzzers are used in many of the things we see in our day-to-day life, like megaphones. This project provided you with an introduction to buzzers and playing different notes on a buzzer, while combining your knowledge from previous projects. If you have any questions, leave a comment below.

For more simple projects to get familiar with the Arduino basics, check back on our Learn page frequently.


Share this post



← Older Post Newer Post →


Leave a comment

Please note, comments must be approved before they are published.