Arduino: Piezo Diagrams & Code: Projects 01 & 02: Scale and Playing A Tune
Arduino: Piezo Diagrams & Code: Projects 01 & 02: Scale and Playing A Tune
void setup(){
for (int i = 0; i < numTones; i++) // counts through tones 0-9 using the variable i
{
tone(speakerPin, tones[i]); // play the tone in each array position
delay(500); // wait 500 milliseconds
}
noTone(speakerPin); // stop playing
}
void loop(){
}
// We'll set up an array with the notes we want to play. Change these values to make different
songs!
const int songLength = 18; // Length must equal the total number of notes and spaces
char notes[] = "cdfda ag cdfdg gf "; // Notes is an array of text characters corresponding to the
notes in your song. A space represents a rest (no tone).
int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2}; // Beats is an array of values for each note
and rest.
// A "1" represents a quarter-note, 2 a half-note, etc. Don't
forget that the rests (spaces) need a length as well.
int tempo = 150; // The tempo is how fast to play the song - to make the song play faster,
decrease this value.
void setup() {
pinMode(speakerPin, OUTPUT); // sets the piezo as an output
}
void loop() {
int i, duration;
while(true){} // we only want to play the song once, so we'll pause forever (remove this line
to play repeatedly)
}
int frequency(char note) { // this function takes a note character (a-g), and returns the
corresponding frequency in Hz for the tone() function.
int i;
const int numNotes = 8; // number of notes we're storing
// The following arrays hold the note characters and their corresponding frequencies. The last
"C" note is uppercase
// to separate it from the first lowercase "c". If you want to add more notes, you'll need to
use unique characters.
// For the "char" (character) type, we put single characters in single quotes.
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
// Now we'll search through the letters in the array, and if we find it, we'll return the
frequency for that note.
for (i = 0; i < numNotes; i++) { // step through the notes
if (names[i] == note) { // is this the one?
return(frequencies[i]); // if yes, return the frequency
}
}
return(0); // we looked through everything and didn't find it, but we still need to return a
value, so return 0.
}
11/2015
Brown County Library
Projects 03: Theramin
Components needed:
Arduino Uno board
breadboard
piezo
4 jumper wires
1 1K ohm resistor
light dependent resistor (sometimes called a photoresistor)
11/2015
Brown County Library
/*
Piezo 03 : Theremin
Code adapted from Adafruit Arduino Lesson 10 (learn.adafruit.com/adafruit-arduino-lesson-
10-making-sounds)
*/
void setup() {
pinMode(speakerPin, OUTPUT); // sets the piezo as an output
}
void loop() {
int reading = analogRead(photoresistorPin); // read the amount of light hitting the
photoresistor
int pitch = 200 + reading / 4; // set the pitch value
tone(speakerPin, pitch); // play a tone at the set pitch
}
11/2015
Brown County Library
Projects 04: Knock Sensor
Components needed:
Arduino Uno board
breadboard
piezo
2 jumper wires
1 million ohm resistor
1 LED
11/2015
Brown County Library
/*
Piezo 04 : Knock Sensor
Code adapted from Arduino Knock Sensor Tutorial (arduino.cc/en/Tutorial/KnockSensor)
and Learning About Electronics (learningaboutelectronics.com/Articles/Piezo-knock-sensor-circuit.php)
*/
void setup() {
pinMode(LED, OUTPUT); // sets the digital PIN (LED) as an output
}
void loop() {
int val= analogRead(knockSensor); // listen for a knock
if (val >= threshold) { // if a knock that is higher than the set threshold occurs...
digitalWrite(LED, HIGH); // turn the LED on
delay(500); // wait for half a second
digitalWrite(LED, LOW); // turn the LED off
} else {
digitalWrite(LED, LOW); // if there is no knock, leave the LED off
}
}
11/2015
Brown County Library
Ideas to Build On
Add code to the Knock Sensor you can track the knocks using the Serial Monitor:
https://www.arduino.cc/en/Tutorial/KnockSensor
Learn More
Want to learn more about how piezos work? Try these resources:
11/2015
Brown County Library