06 |
.:Piezo Elements:. |
What We're Doing
To this point we have controlled light, motion, and electrons. Let's tackle sound next. But sound is an analog phenomena, how will our digital Netduino cope? We will once again rely on its incredible speed which will let it mimic analog behavior. To do this, we will attach a piezo element to one of the Netduino's digital pins. A piezo element makes a clicking sound each time it is pulsed with current. If we pulse it at the right frequency (for example 440 times a second to make the note middle A) these clicks will run together to produce notes. Let's get to experimenting with it and get your Netduino playing "Twinkle Twinkle Little Star".
To this point we have controlled light, motion, and electrons. Let's tackle sound next. But sound is an analog phenomena, how will our digital Netduino cope? We will once again rely on its incredible speed which will let it mimic analog behavior. To do this, we will attach a piezo element to one of the Netduino's digital pins. A piezo element makes a clicking sound each time it is pulsed with current. If we pulse it at the right frequency (for example 440 times a second to make the note middle A) these clicks will run together to produce notes. Let's get to experimenting with it and get your Netduino playing "Twinkle Twinkle Little Star".
The Circuit
The Parts
NCIR-06 Breadboard Sheet x1 |
2 Pin Header x4 |
Piezo Element x1 |
Wire | ||||
Code (no need to type everything in just)
Download the Code from ( http://nedx.org/CODE06 )
(copy the text and paste it into an empty Netduino Sketch)
//Libraries that our program uses
using System; //use base classes
using System.Threading; //use threading classes
using Microsoft.SPOT; //use smart personal objects technology classes
using Microsoft.SPOT.Hardware; //use hardware related SPOT classes
using SecretLabs.NETMF.Hardware; //use Secret Labs hardware framework
using SecretLabs.NETMF.Hardware.Netduino; //use the Netduino specific classes
namespace NCIR06 /// Define the namespace we are in ///
{
public class Program /// Define any variables used in the program and subrout
//ines after Main()///
{
static PWM speaker = new PWM(Pins.GPIO_PIN_D9); //Sets pin 9 as connecte
//d to our Piezo (PWM mode)
static char[] notes = "ccggaagffeeddc ".ToCharArray(); //The notes of our song
static int[] beats = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; //The number
// of beats each note is played
static int tempo = 300; //The speed
//the song is played
public static void Main() /// The Main loop (run at power up) ///
{
while (true) /// Do Forever ///
{
for (int i = 0; i < notes.Length; i++) //Run through each note
{
playNote(notes[i], beats[i] * tempo); //Plays the current note
playNote(' ', tempo / 2); //pauses before going on to nex
//t note
}
}
}
///
/// Plays a particular tone for a particular duration
///
/// The tone to play
/// How long to play the tone
static void playTone(int tone, int duration)
{
speaker.SetPulse((uint)(tone * 2), (uint)tone); //sets the pin to the right
//PWM settings
Thread.Sleep(duration); //waits the appropriate amou
//nt of time
}
///
/// Looks up the tone for a note based on it's letter and plays
/// it for a specific amount of time
///
/// The letter of the note to play
/// How long to play the tone
static void playNote(char note, int duration)
{
char[] names = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', ' ' }; //The array o
//f possible notes
int[] tones = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 0 };//The frequen
//cy of each note in the previous array
for (int i = 0; i < 9; i++) //Goes through each note
{
if (names[i] == note)
{
playTone(tones[i], duration); //Plays the right note
}
}
}
} /// Close the Program Loop ///
} /// Close the Namespace Loop ///
Download the Code from ( http://nedx.org/CODE06 )
(copy the text and paste it into an empty Netduino Sketch)
//Libraries that our program uses
using System; //use base classes
using System.Threading; //use threading classes
using Microsoft.SPOT; //use smart personal objects technology classes
using Microsoft.SPOT.Hardware; //use hardware related SPOT classes
using SecretLabs.NETMF.Hardware; //use Secret Labs hardware framework
using SecretLabs.NETMF.Hardware.Netduino; //use the Netduino specific classes
namespace NCIR06 /// Define the namespace we are in ///
{
public class Program /// Define any variables used in the program and subrout
//ines after Main()///
{
static PWM speaker = new PWM(Pins.GPIO_PIN_D9); //Sets pin 9 as connecte
//d to our Piezo (PWM mode)
static char[] notes = "ccggaagffeeddc ".ToCharArray(); //The notes of our song
static int[] beats = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; //The number
// of beats each note is played
static int tempo = 300; //The speed
//the song is played
public static void Main() /// The Main loop (run at power up) ///
{
while (true) /// Do Forever ///
{
for (int i = 0; i < notes.Length; i++) //Run through each note
{
playNote(notes[i], beats[i] * tempo); //Plays the current note
playNote(' ', tempo / 2); //pauses before going on to nex
//t note
}
}
}
///
/// Plays a particular tone for a particular duration
///
/// The tone to play
/// How long to play the tone
static void playTone(int tone, int duration)
{
speaker.SetPulse((uint)(tone * 2), (uint)tone); //sets the pin to the right
//PWM settings
Thread.Sleep(duration); //waits the appropriate amou
//nt of time
}
///
/// Looks up the tone for a note based on it's letter and plays
/// it for a specific amount of time
///
/// The letter of the note to play
/// How long to play the tone
static void playNote(char note, int duration)
{
char[] names = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', ' ' }; //The array o
//f possible notes
int[] tones = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 0 };//The frequen
//cy of each note in the previous array
for (int i = 0; i < 9; i++) //Goes through each note
{
if (names[i] == note)
{
playTone(tones[i], duration); //Plays the right note
}
}
}
} /// Close the Program Loop ///
} /// Close the Namespace Loop ///
Not Working? (3 things to try)
No Sound
Given the size and shape of the piezo element it is easy to miss the right holes on the breadboard. Try double checking its placement.
Can't Think While the Melody is Playing?
Just pull up the piezo element whilst you think, upload your program then plug it back in.
Tired of Twinkle Twinkle Little Star?
The code is written so you can easily add your own songs, check out the code below to get started.
Given the size and shape of the piezo element it is easy to miss the right holes on the breadboard. Try double checking its placement.
Just pull up the piezo element whilst you think, upload your program then plug it back in.
The code is written so you can easily add your own songs, check out the code below to get started.
Making it Better?
Playing with the speed:
The timing for each note is calculated based on variables, as such we can tweak the sound of each note or the timing. To change the speed of the melody you need to change only one line.
static int tempo = 300;
|
static int tempo = (new #)
Change it to a larger number to slow the melody down, or a smaller number to speed it up.
Tuning the notes:
If you are worried about the notes being a little out of tune this can be fixed as well. The notes have been calculated based on a formula in the comment block at the top of the program. But to tune individual notes just adjust their values in the tones[] array up or down until they sound right. (each note is matched by its name in the names[] (array ie. c = 1915 )
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', ' ' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 0};
Composing your own melodies:
The program is pre-set to play 'Twinkle Twinkle Little Star' however the way it is programmed makes changing the song easy. Each song is defined in two arrays, the first array notes[] defines each note, and the second beats[] defines how long each note is played. Some Examples:
Twinkle Twinkle Little Star
static char[] notes =
"ccggaagffeeddc ".ToCharArray();
static int[] beats =
{1,1,1,1,1,1,2,1,1,1,1,1,1,2,4};
Happy Birthday (first line)
static char[] notes =
"ccdcfeccdcgf ".ToCharArray();
static int[] beats =
{1,1,1,1,1,1,2,1,1,1,1,1,1,2,4};
Playing with the speed:
The timing for each note is calculated based on variables, as such we can tweak the sound of each note or the timing. To change the speed of the melody you need to change only one line.
|
static int tempo = (new #)
Change it to a larger number to slow the melody down, or a smaller number to speed it up.
Tuning the notes:
If you are worried about the notes being a little out of tune this can be fixed as well. The notes have been calculated based on a formula in the comment block at the top of the program. But to tune individual notes just adjust their values in the tones[] array up or down until they sound right. (each note is matched by its name in the names[] (array ie. c = 1915 )
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', ' ' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 0};
Composing your own melodies:
The program is pre-set to play 'Twinkle Twinkle Little Star' however the way it is programmed makes changing the song easy. Each song is defined in two arrays, the first array notes[] defines each note, and the second beats[] defines how long each note is played. Some Examples:
Twinkle Twinkle Little Star
static char[] notes =
"ccggaagffeeddc ".ToCharArray();
static int[] beats =
{1,1,1,1,1,1,2,1,1,1,1,1,1,2,4};
Happy Birthday (first line)
static char[] notes =
"ccdcfeccdcgf ".ToCharArray();
static int[] beats =
{1,1,1,1,1,1,2,1,1,1,1,1,1,2,4};