Assignment #9 – Let the Music Play
Assignment – Let the Music Play
1.1 Project Assignment
1. Play a song using the BasicX and an external speaker.
1.2 Assignment Goals
At the conclusion of this assignment you should be able to:
1. Use the BasicX to output the frequency necessary to vibrate a speaker and
produce a desired pitch.
2. Apply your knowledge of transistors to provide the necessary
amplification for the current that drives the speaker.
3. Relax and have a little fun.
1.3 Musical Scales
The human ear detects sound when the eardrum is vibrated. The vibration is
caused by changes in the air pressure, or what we call sound waves. We have
defined musical pitch as the vibration of the eardrums at certain frequencies that
are pleasing to the ear. Musicians have labeled these frequencies with letters of
the alphabet (A, A#, B, C, C#, D, D#, E, F, F#, G, and G#).
In western music, a frequency of 440 Hz has been assigned the letter A. Each
time the frequency is doubled, it is called an octave and the letters repeat. For
example, A is defined as 440 Hz. If you move to a frequency of 880 Hz, you
have moved one octave, and again you are at a pitch labeled A. Each octave is
divided into twelve half-tones or half-steps (A, A#, B, C, C#, etc.). (Think about
the black and white keys on a piano. Every twelve notes, the pattern of black and
white keys repeats.) Each half-step is related to the pitch below it by a factor of
the twelfth root of two. For example, A is at 440 Hz and A# is at 466 Hz
( 440 12 2 466 ). A table that includes the frequencies for four octaves of
pitches is given in figure 1.
Course Material for MECN 3200 – Mechatronics 1
Assignment #9 – Let the Music Play
Frequency Frequency Frequency Frequency
Pitch
(Hz) (Hz) (Hz) (Hz)
A 220 440 880 1760
A# 233 466 932 1864
B 247 494 988 1976
C 262 523 1046 2092
C# 277 554 1109 2216
D 294 587 1175 2348
D# 311 622 1244 2488
E 330 659 1318 2636
F 349 698 1397 2792
F# 370 740 1480 2960
G 392 784 1568 3136
G# 415 831 1661 3324
Figure 1: Pitches and Frequencies
1.4 Using the BasicX to Create Pitches
Creating different pitches using the BasicX is very simple because the “FreqOut”
command allows you to turn an output pin on and off at a specific frequency.
This frequency output can be connected to a speaker to cause it to vibrate. The
vibration creates a pressure wave that will vibrate your eardrum at the same
frequency and you will hear the pitch.
The FreqOut command can actually output two different frequencies at the same
time. We will use this fact to get a better sound quality from our speakers. For
each pitch that you wish to play, use the frequency that you want and a frequency
that is one octave higher. For example, to play the lowest A on the table in figure
1, use a frequency of 220 Hz and 440 Hz. This means the highest pitch you will
be able to play is the G# using 1661 Hz and 3324 Hz. (After playing this pitch,
you will probably not want to go any higher anyway.) To learn the syntax for the
FreqOut command, refer to the system library help document.
1.5 The Speaker Circuit
We could use the speaker provided on your BasicX Development Board to
complete this week’s project. However, the speaker included on the board is not
Course Material for MECN 3200 – Mechatronics 2
Assignment #9 – Let the Music Play
of the highest quality and requires a fairly high frequency (higher than your ears
are probably comfortable with) to operate with much volume. To get a little
better sound, we will use different speakers. The problem with using the better
quality speaker is that it requires more current to make it vibrate at lower
frequencies. To solve this problem, we will use a transistor to amplify the current
(just like we did with the motors). Construct the speaker circuit as shown in
figure 2.
+5V
Output from 1 k
BasicX Pin
Speaker
Figure 2: The Speaker Circuit
1.6 Making Music
The project for this week is to use the BasicX to play a song. We will be taking
sheet music and using it to write the program for the BasicX. Without making
everyone musicians, here is a brief description of how to read music. Each
musical pitch is represented by a note that is either on the space or line of the
staff. Figure 3 describes which pitch is assigned to which line. If there is a # or b
at the beginning of the line, the note of that letter is either sharp or flat (A# = Bb,
G# = Ab, etc). The example shown would not appear in actual music, but notice
that anytime you needed an A, you would actually use the frequency for G# (Ab)
and anytime you needed a D, you would actually use the frequency for D#. The
rest of the letters would the exactly as labeled.
E F
# B C D
b F G
A
Figure 3: Pitches on a Staff
Course Material for MECN 3200 – Mechatronics 3
Assignment #9 – Let the Music Play
The other important part of reading music is determining how long to play each
note. Some common note values are the whole note, the half note, the quarter
note, and the eighth note. As the names suggest, the values are listed in
descending order of length and each note is half as long as the previous note. It is
also possible to extend a note for one half of its value by adding a dot. (example:
a dotted half note is one and a half times as long as a half note. For our project,
we will assume a whole note should be held for 2 seconds. The symbols for the
notes can be found in figure 4.
Half Note Eighth Notes
OR
Whole Note Quarter Note
Figure 4: Different Notes
1.6.1 Example Program
To demonstrate how to take sheet music and convert it to a BasicX program, an
example is shown taking the sheet music from “Minuet in G Major” by Bach and
converting it to a BasicX program. The sheet music is given first (figure 5) and
the BasicX program follows (figure 6).
D D D B A B G A D C B A
#
3
4
D C B A G E C B A G F# E D F# G
#
3
4
Figure 5: Sheet Music for Minuet in G Major
Course Material for MECN 3200 – Mechatronics 4
Assignment #9 – Let the Music Play
Sub Main()
Call FreqOut(12,587,1175,500)
Call FreqOut(12,587,1175,500)
Call FreqOut(12,587,1175,500)
Call FreqOut(12,494,988,500)
Call FreqOut(12,440,880,250)
Call FreqOut(12,494,988,250)
Call FreqOut(12,392,784,500)
Call FreqOut(12,440,880,500)
Call FreqOut(12,587,1175,500)
Call FreqOut(12,523,1046,500)
Call FreqOut(12,494,988,1000)
Call FreqOut(12,440,880,500)
Call FreqOut(12,587,1175,500)
Call FreqOut(12,523,1046,250)
Call FreqOut(12,494,988,250)
Call FreqOut(12,440,880,250)
Call FreqOut(12,392,784,250)
Call FreqOut(12,659,1318,500)
Call FreqOut(12,523,1046,250)
Call FreqOut(12,494,988,250)
Call FreqOut(12,440,880,250)
Call FreqOut(12,392,784,250)
Call FreqOut(12,370,740,500)
Call FreqOut(12,330,659,250)
Call FreqOut(12,294,587,250)
Call FreqOut(12,370,740,500)
Call FreqOut(12,392,784,1500)
End Sub
Figure 6: BasicX Program for Minuet in G Major
1.6.2 Things to Notice
1. Each of the notes are played using two frequencies that are one octive apart.
This makes the sound a little more pleasing for the ear.
2. An eighth note is given 0.25 seconds (250 ms) and each value is calcualted
accordingly. (quarter = 500, half = 1000, dotted half = 1500).
1.7 Project Assignment
Your project is to take the sheet music from any piece of music you like and
convert it to a program for the BasicX.
Course Material for MECN 3200 – Mechatronics 5