SACC

/ / navigation


Intro
Generate a number
Motor Pulse
Getting Creative
Cheesy Hallmark Card
Build an Instrument
Theremin
Soft Circuit

/ / Introduction



piezo.png

diagram:
diagram.png
Inside the Piezo Buzzer
inside.png

Piezo

A Piezo is an electronic device that can be used to play tones and to detect tones. The normal piezoelectric effect is generating electricity from squeezing a crystal. The piezo speaker consists of two metal plates and when electricity is applied to the piezo speaker it will make the metal plates attract and repel generating quick vibration which in turn will generate sound.

Piezos have polarity. Commercial devices usually have a red and a black wire indicating how to plug it to the board. Connect the black one to ground and the red one to the input. You also have to connect a resistor in the range of the Megaohms in parallel to the Piezo element;


Sometimes it is possible to acquire Piezo elements without a plastic housing. These devices look like a metallic disc and are easier to use as input sensors.
To read a piezo you can just hook it into an analog input, but you need to drain off any voltage with a resistor, or it just builds up.

When the Piezo element is used to detect sound, it is a knock sensor. Knock sensors read a voltage value and transform it into a value encoded digitally. When using an Arduino, you transform the voltage into a value in the range 0..1024. 0 represents 0 volts, while 1024 represents 5 volts at the input of one of the six analog pins.


The code example will capture the knock and if it is stronger than a certain threshold, it will send the string "Knock!" back to the computer over the serial port. In order to see this text you could either use a terminal program, which will read data from the serial port and show it in a window, or make your own program in e.g. Processing.


 int ledPin = 13;
int piezoPin = 2;

int THRESHOLD = 100;  // set minimum value that indicates a knock

int val = 0;       // variable to store the value coming from the sensor
int t = 0;         // the "time" measured for how long the knock lasts

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(19200);
  Serial.println("ready");      // indicate we're waiting
}

void loop() {
  digitalWrite(ledPin,LOW);     // indicate we're waiting

  val = analogRead(piezoPin);   // read piezo
  if( val >THRESHOLD ) {      // is it bigger than our minimum?
    digitalWrite(ledPin, HIGH); // tell the world
    t = 0;
    while(analogRead(piezoPin) > THRESHOLD) {
      t++;
    } // wait for it to go LOW  (with a little hysteresis)
    if(t>100) {  // cut off the low values because they're noise
      Serial.print("knock! ");
      Serial.println(t);
    }
  }

 
  delay(100);  // make a delay to avoid overloading the serial port
}