Skip to Content

How can I use the interface I built to play melodies that I create?

Think About

How can I make sound with a microcontroller?

How can I program melodies in the Arduino IDE?

What do I want my instrument to do?

Get Started

Watch the following video that shows you how to understand the Arduino IDE. Directions are available below in text:

  1. Let’s learn a little more about the Arduino Sketches. There are three main parts of an Arduino Sketcharduino_sketch_sections
  2. Now, let’s examine our music_output sketch more closely to understand what it’s all about.
    1. NOTE: Return to the last XP if you do not have the music_output sketch loaded on your microcontroller

musicoutput-sketch-explanation1

 

musicoutput-sketch-explanation2

 

 

musicoutput-sketch-explanation3

 

Go Further

Watch the following video that shows you how to play with code to make your own melodies in the Arduino IDE. Directions are below in text:

You can play with the code in the void loop section of the music_output sketch.

  1. Change the numbers after the word “speaker”.
    • These numbers represent frequency in hertz.
    • You don’t have to know what that means to try new numbers.

What happens if you type in a higher number?

What happens when you type in a lower number?

  1. Remember to upload your code to your microcontroller every time you make a change to the sketch.

music_output_ss-noarrow

  1. You can also change the length of your delays.

What happens when you type in a lower number?

What happens when you type in a higher number?

What if all of your delays are different lengths (different numbers)?

How can you make your melody longer?

  1. Try adding a new line of code that says

tone(speaker,          );

delay(        );

Fill in the blanks with your own numbers.

Remember to upload your code to your microcontroller every time you make a change to the sketch.

Go Even Further

Watch this video that shows you how to program music with note names in your Arduino IDE. Directions are also available below as text:

In this sketch, I’ve pasted in some variables that are helpful to people who are used to thinking about notes using letter names: A, B, C, D, E, F, G.

  1. Download the note name sketch

The new variables are named after the letter names (A, B, C, D, E, F, G).

I looked up the frequencies measured in hertz of these notes online and typed in them in here.

  1. Now in the void loop section, try typing some letter names of the notes (you don’t need to use any numbers).

music_output_with_note_names-ss

Remember to upload your code to your microcontroller every time you make a change to the sketch.

Share What You Learned

Upload a recording or short video of your microcontroller playing the melody you coded and uploaded to the microcontroller.