Assignment #3: Piezo Cake


Download the assignment

Background

In this assignment, we will learn how to make sounds by pulsing current through a piezo circuit. We will use a lookup table built using one-dimensional arrays to find the right note to play on our Arduino. Beats will be used on the tempo so that we could make a recognizable song from our experiment.

Sound waves are vibrations in the air pressure. The speed of the vibrations (cycles per second or Hertz) is what makes the pitch of the sound. The higher the frequency of the vibration, the higher the pitch will be.

Sound with the Arduino

 

Middle C is usually defined as a frequency of 261 Hz. If you turn a digital output on and off again 261 times every second then that output will be middle C.

To hear the output, we need to attach something that will convert the electrical signal into sound waves. This can be done with a loudspeaker or, as we have used here, a piezo sounder.

Piezo sounders use a special crystal that expands and contracts as an electrical signal passes through it. This will generate a tone that we can hear.

 

Assignment

Create a melody from one of your favorite songs that uses several different notes. These notes can include flats and sharps.

EGBDF

rests notesRemember to include a proper tempo in your composition. If your song has half notes, quarter notes or even eighth notes, remember to have the proper beats for the tempo that you choose.

Here are some suggestions:

  • Star Trek theme (TOS)
  • Star Wars theme (the original trilogy)
  • Sound of Music (Julie Andrews, et al.)
  • Holiday songs
  • Children’s nursery rhymes
  • Anything with Mozart, Bach, Beethoven, Vivaldi, Schumann, Mendelssohn, Chopin, Rossini,
  • Verdi, Donizetti, Puccini, Morricone

 

Computer Program Requirements

  1. Use an array to hold the notes of your song, in frequency values (Hz)
  2. Use an array to hold the number of beats each note is to be played
  3. Use LEDs to light up as your song plays, but make sure the correct LED sequence lights up
  4. Use a push button to start and stop your Arduino project
  5. Use at least the following three functions in your code:

i.    int getToneValue( ) – Calculates the tone to generate based on the formula in “Additional Resources”

ii.    void playNote( ) – Plays the tone that you just calculated for a given duration

  1. Use loops and if-statements
  2. Use variables

 

Expectations

When you are finished your composition, you will need to hand in the following:

  • Your finished sketch with proper commenting and good programming style
  • A written report, including the sheet music that you used. This can be taken from anywhere (Internet, book, copied or even hand written). If you didn’t use any sheet music but sounded out the melody, write down the notes on a sheet of music paper
  • A circuit diagram of your Arduino project showing all the circuits you used (i.e, piezo circuit, LEDs, wires, pin assignments, etc.). Label all pins and resistor values.

 

Prerequisites

To successfully complete this assignment, you will need to have completed the prior two assignments.

 

Additional Resources

Below is a Octave Chart that shows all the frequencies of the notes (Hz). Notice the sharps and flats too. As a reference, the red box shows the Middle C octave.

Octave chart

Formulas to use:

Period, P = (1 / f) * 1000000
Time high, TH = P / 2

where, f = frequency of the note in Hz, TH = the tone value to use in your sketch

Hint: Pay attention to the use of double, float and int variables. Remember that these variables don’t like each other!

 

Code Template

int noteC4  = 261; // middle C, frequency in Hertz (Hz)
int noteCS4 = 277; // C sharp
int noteD4  = 293; 
int noteE4  = 329;
int noteF4  = 349;
int noteG4  = 391;
int noteA4  = 440;
int noteB4  = 493;
int noteRest = 0;

int notes[] = { noteC4, noteD4, noteE4, noteF4, noteG4, noteA4, noteB4 };
int beats[] = { 1, 1, 1, 1, 1, 1, 2 };
int tempo = 500; // tempo of the notes, in milliseconds

int speakerPin = 2;
int pushButtonPin = 13;

void setup() {
  // initialize your pins
  pinMode(pushButtonPin, INPUT); // Push button on pin 13
  pinMode(speakerPin, OUTPUT); // piezo speaker pin is on pin 2

 // Initialize the rest of the LED pins
}

void loop() {
  if (digitalRead(pushButtonPin) == LOW) {
    // Use a for-loop to go through the notes[] array
    // Loop for 7 times because this is how many notes are in our array
    for ( int i = 0; i < 8; i++ ) {
      if (notes[i] == noteRest) {
        delay(beats[i] * tempo);  // rest note will just be a delay for the number of beats
      } else {
        playNote(notes[i], beats[i] * tempo);
      }
    }
  }
}

// ************************* Functions are defined below *****************************

void playNote(int hertz, int duration) {
  int tone = getToneValue(hertz); // tone value is stored in an int data type

  // Turn on the LEDs
  // You can use if statements here to turn on the correct LED for each note

  // The following for-loop will pulse the piezo speaker to create
  // an approximation of the proper note
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    // The piezo speaker will vibrate at a certain frequency, depending on how fast
    // we pulse the current. The faster the pulse, the higher the frequency. The higher the
    // frequency, the higher pitch sound

    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
 
  // Turn off the LED
  // Use the same if statements as before but turn off the LEDs this time
}

double getToneValue(int hertz) {
  double P = <put your equation here, see above>
  double TH = <put your equation here, see above>
  return TH;
}

 

Grading Plan

Grades are calculated using the following rubric:

  • Finished working sketch– 50%
  • Create and use the 3 functions: void playNote(), void getToneValue() – 10%
  • Proper comments where needed – 10%
  • Good programming style – 10%
  • Written report, including sheet music to follow the melody – 5%
  • Accurate and labeled circuit diagram – 5%

Total marks possible: 100% (including bonus)