8 Bit Music Conductor

Start 

This project only uses a Piezo Buzzer to create a musical master piece.

Modules 

Gather the following parts to complete this project.

Parts

All Parts
All Parts
Piezo Buzzer
Cable
PartQuantity
Piezo Buzzer 1
Cable 1

Basic Music Player 

Take a cable and unwrap it. Plug one side into the buzzer socket and the other into Digital socket D6 to start making music.

All the parts you'll need
All the parts you'll need
Take a cable...
... and unwrap it
Plug one side into the buzzer socket
... and the other into Digital socket D6
All finished

Upload

Upload the code below. This tutorial uses Digital socket 6. If you are using a different socket update the code after copying it.

Download file Copy to clipboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//Change here if you're using a different socket
#define buzzerSocket 6

int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;

void setup() {
  pinMode(buzzerSocket, OUTPUT);

  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo); // rest
    } else {
      playNote(notes[i], beats[i] * tempo);
    }

    // pause between notes
    delay(tempo / 2);
  }
}

void loop() {

}

void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(buzzerSocket, HIGH);
    delayMicroseconds(tone);
    digitalWrite(buzzerSocket, LOW);
    delayMicroseconds(tone);
  }
}

void playNote(char note, int duration) {
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

  // play the tone corresponding to the note name
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}

Observe

It plays a simple melody on start up.

Modify

You can change the notes and duration to make your own songs.

Experiment

Checkout the code below for some examples of how to put together a melody.

Some NOTEable tunes 

Below are some pretty well known melodies to get you started. Replace the block of code that contains these variables in the example above.

Ode to Joy

Download file Copy to clipboard
1
2
3
4
5
//All the code to make a song is in this block
int length = 15; // the number of notes
char notes[] = "eefggfedccdeedd"; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1 };
int tempo = 300;

Super Mario Brothers

Download file Copy to clipboard
1
2
3
4
5
//All the code to make a song is in this block
int length = 8; // the number of notes
char notes[] = "eeecegc"; // a space represents a rest
int beats[] = { 1, 1, 2, 1, 1, 4, 1};
int tempo = 120;