The Sound and Buzzer Module can be used as either an output for creating sound, or as an input for detecting vibration or knocks.
Module Pinout

The module is not polarised, so you can connect it in either orientation.
Sound Output
To generate tones the Sound and Buzzer Module should have one connection linked to GND (0V) on your microcontroller and the other connection linked to a digital I/O pin that is capable of Pulse Width Modulation (PWM) output. By driving different frequencies to the PWM output it's possible to play rudimentary tones.
In this example we connected pin 2 of the module to GND, and pin 1 of the module to digital I/O pin D9, which supports PWM output.

The Arduino website has a page explaining how to drive a piezo element at http://www.arduino.cc/en/Tutorial/PlayMelody, including the following example sketch by David Cuartielles. Copy and paste this into the Arduino IDE and upload it to your Arduino with the Sound and Buzzer Module connected as shown above:
int ledPin = 13; int speakerOut = 9; byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'}; int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956}; byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p"; // count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 // 10 20 30 int count = 0; int count2 = 0; int count3 = 0; int MAX_COUNT = 24; int statePin = LOW; void setup() { pinMode(ledPin, OUTPUT); } void loop() { analogWrite(speakerOut, 0); for (count = 0; count < MAX_COUNT; count++) { statePin = !statePin; digitalWrite(ledPin, statePin); for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) { for (count2=0;count2<8;count2++) { if (names[count2] == melody[count*2 + 1]) { analogWrite(speakerOut,500); delayMicroseconds(tones[count2]); analogWrite(speakerOut, 0); delayMicroseconds(tones[count2]); } if (melody[count*2 + 1] == 'p') { // make a pause of a certain size analogWrite(speakerOut, 0); delayMicroseconds(500); } } } } }
Knock Detection
The Sound and Buzzer Module contains a piezo-electric element that can be used to detect vibrations or sharp knocks. The module includes a built-in 1M resistor, so you don't need to add any other parts to use it as a knock sensor.
Detection of vibration is performed by measuring the voltage across the module terminals. To do this, connect one terminal to GND (0V) on your microcontroller and the other terminal to an analog input pin. In this example we've used A0.

The Arduino website has a tutorial explaining how to do knock detection at http://www.arduino.cc/en/Tutorial/Knock, including the following example sketch by David Cuartielles and Tom Igoe. Copy and paste this code into the Arduino IDE and upload it to your Arduino with the Sound and Buzzer Module connected as shown above, then open the serial console in the IDE with the baud rate set to 38400bps to match the setting in the sketch. Place the Sound and Buzzer Module on a hard surface such as a table top, and tap on the sensor or nearby to see the effect. When the Arduino detects a knock it will send the message "Knock!" to the serial console.
const int ledPin = 13; // led connected to digital pin 13 const int knockSensor = A0; // the piezo is connected to analog pin 0 const int threshold = 100; // threshold value to decide when the detected sound is a knock or not int sensorReading = 0; // variable to store the value read from the sensor pin int ledState = LOW; // variable used to store the last LED status, to toggle the light void setup() { pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT Serial.begin(38400); // use the serial port } void loop() { // read the sensor and store it in the variable sensorReading: sensorReading = analogRead(knockSensor); // 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 }





