Books

Notes and explanations

Piezos can work as either a microphone or a speaker. In this example from http://arduino.cc/en/Tutorial/Knock.html we will use it to register a knock. Note the reset button on the arduino. The piezo needs to lay flat on the table for this to work. You can change the threshhold to make your particular piezo more or less sensitive.

This is a parallel circuit. Note the resistor position.

Parts & Symbols

8 ohm speaker1 M resister

Copy & Paste the following code to the Arduino IDE.

/* Knock Sensor

This sketch reads a piezo element to detect a knocking sound.
It reads an analog pin and compares the result to a set threshold.
If the result is greater than the threshold, it writes
"knock" to the serial port, and toggles the LED on pin 13.

The circuit:
* + connection of the piezo attached to analog in 0
* - connection of the piezo attached to ground
* 1-megohm resistor attached from analog in 0 to ground

// if the sensor reading is greater than the threshold:
if (sensorReading >= threshold) {
// toggle the status of the ledPin:
ledState = !ledState;
// update the LED pin itself:
digitalWrite(ledPin, ledState);
// send the string "Knock!" back to the computer, followed by newline
Serial.println("Knock!");
}
delay(100); // delay to avoid overloading the serial port buffer
}