Questo sito non è ne attivo ne aggiornato, specialmente la pagina del download รจ ferma a 5 vesioni fa , utilizzate il sito in inglese finchè questo sito non sarà annunciato ufficialmente

Learning   Examples | Foundations | Hacking | Links

Examples > Analog I/O

Knock

Here we use a Piezo element to detect sound, what will allow us to use it as a knock sensor. We are taking advantage of the processors capability to read analog signals through its ADC - analog to digital converter. These converters read a voltage value and transform it into a value encoded digitally. In the case of the Arduino boards, we transform the voltage into a value in the range 0..1024. 0 represents 0volts, while 1024 represents 5volts at the input of one of the six analog pins.

A Piezo is nothing but an electronic device that can both be used to play tones and to detect tones. In our example we are plugging the Piezo on the analog input pin number 0, that supports the functionality of reading a value between 0 and 5volts, and not just a plain HIGH or LOW.

The other thing to remember is that Piezos have polarity, commercial devices are usually having a red and a black wires indicating how to plug it to the board. We connect the black one to ground and the red one to the input. We also have to connect a resistor in the range of the Megaohms in parallel to the Piezo element; in the example we have plugged it directly in the female connectors. Sometimes it is possible to acquire Piezo elements without a plastic housing, then they will just look like a metallic disc and are easier to use as input sensors.

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 can use the Arduino serial monitor.

Example of connection of a Piezo to analog pin 0 with a resistor

/* Knock Sensor
 * by DojoDave <http://www.0j0.org>
 *
 * Program using a Piezo element as if it was a knock sensor.
 *
 * We have to basically listen to an analog pin and detect 
 * if the signal goes over a certain threshold. It writes
 * "knock" to the serial port if the Threshold is crossed,
 * and toggles the LED on pin 13.
 *
 * http://www.arduino.cc/en/Tutorial/Knock
 */

int ledPin = 13;      // led connected to control pin 13
int knockSensor = 0;  // the knock sensor will be plugged at analog pin 0
byte val = 0;         // variable to store the value read from the sensor pin
int statePin = LOW;   // variable used to store the last LED status, to toggle the light
int THRESHOLD = 100;  // threshold value to decide when the detected sound is a knock or not

void setup() {
 pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
 Serial.begin(9600);       // use the serial port
}

void loop() {
  val = analogRead(knockSensor);    // read the sensor and store it in the variable "val"
  if (val >= THRESHOLD) {
    statePin = !statePin;           // toggle the status of the ledPin (this trick doesn't use time cycles)
    digitalWrite(ledPin, statePin); // turn the led on or off
    Serial.println("Knock!");       // send the string "Knock!" back to the computer, followed by newline
    delay(10);                      // short delay to avoid overloading the serial port
   }
}