Assignment #3: Piezo Cake
Computer Science: 7th and 8th Grade
7-CS / 8-CS: Introduction to Computer Science I
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.
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.
th
th
Assignment #3: Piezo Cake – Computer Science: 7 and 8 Grade
7-CS / 8-CS: Introduction to Computer Programming I
Page 1
Assignment
Create a melody from one of your favorite songs that uses several different notes. These notes can
include flats and sharps.
Remember 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.
2.
3.
4.
5.
Use an array to hold the notes of your song, in frequency values (Hz)
Use an array to hold the number of beats each note is to be played
Use LEDs to light up as your song plays, but make sure the correct LED sequence lights up
Use a push button to start and stop your Arduino project
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
iii. int getLedPin( ) – Gets the LED to light up according to the note that is played
6. Use loops and if-statements
7. 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.
th
th
Assignment #3: Piezo Cake – Computer Science: 7 and 8 Grade
7-CS / 8-CS: Introduction to Computer Programming I
Page 2
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.
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!
th
th
Assignment #3: Piezo Cake – Computer Science: 7 and 8 Grade
7-CS / 8-CS: Introduction to Computer Programming I
Page 3
Code Template
int
int
int
int
int
int
int
int
int
noteC4
noteCS4
noteD4
noteE4
noteF4
noteG4
noteA4
noteB4
noteC5
=
=
=
=
=
=
=
=
=
261;
277;
293;
329;
349;
391;
440;
493;
523;
// middle C, frequency in Hertz (Hz)
// C sharp
int notes[] = { noteC4, noteD4, noteE4, noteF4, noteG4, noteA4, noteB4, noteC5 };
int beats[] = { 1, 1, 1, 1, 1, 1, 1, 2 };
int temp = 500; // tempo of the notes, in milliseconds
void setup() {
// initialize your pins
pinMode(13, INPUT); // Push button on pin 13
pinMode(2, OUTPUT); // piezo speaker pin is on pin 2
// initialize LED pins
…
}
void loop() {
if (digitalRead(13) == 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++ ) {
playNote(notes[i], beats[i] * tempo);
}
}
}
// ************************* Functions are defined below *****************************
/*
* playNote
* -------*
* Plays the tone for a given duration
*
* Arguments
* int note: The note value to play (Hertz)
* int duration: The total amount of time to play the note (milliseconds)
*
* Returns
* none
*/
void playNote(int hertz, int duration) {
int tone = getToneValue(hertz);
// Turn on the LED
digitalWrite(getLedPin(note), HIGH);
// ---------------------------------------------------------------------------------------// 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. The delayMicroseconds()function will allow
// us to control how fast to pulse the current to make the correct tone.
th
th
Assignment #3: Piezo Cake – Computer Science: 7 and 8 Grade
7-CS / 8-CS: Introduction to Computer Programming I
Page 4
// Play the note
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
// ---------------------------------------------------------------------------------------// Turn off the LED
digitalWrite(getLedPin(note), LOW);
}
Grading Plan
Grades are calculated using the following rubric:
Finished working sketch– 50%
Create and use the 3 functions: void playNote(), void getToneValue() and void getLedPin() – 15%
Proper comments where needed – 10%
Good programming style – 10%
Written report, including sheet music to follow the melody – 10%
Accurate and labeled circuit diagram – 5%
Bonus marks if you work with a friend to create a musical composition that includes both the bass
and treble clefs of a song and also make a video of the composition with commentary – 10%
Total marks possible: 110% (including bonus)
th
th
Assignment #3: Piezo Cake – Computer Science: 7 and 8 Grade
7-CS / 8-CS: Introduction to Computer Programming I
Page 5
© Copyright 2026 Paperzz