Tutorial.Knock History

Hide minor edits - Show changes to output

May 02, 2012, at 04:04 PM by Scott Fitzgerald -
Changed line 48 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/6.Sensors/Knock/Knock.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/06.Sensors/Knock/Knock.ino lang=arduino tabwidth=4:)
November 16, 2011, at 04:19 AM by Scott Fitzgerald -
Changed line 48 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/6.Sensors/Knock/Knock.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/6.Sensors/Knock/Knock.ino lang=arduino tabwidth=4:)
September 28, 2010, at 01:31 PM by Tom Igoe -
Added lines 44-45:

In the code below, the incoming piezo data is compared to a threshold value set by the user. Try raising or lowering this value to increase your sensor's overall sensitivity.
Deleted lines 46-47:

In the code below, the incoming piezo data is compared to a threshold value set by the user. Try raising or lowering this value to increase your sensor's overall sensitivity.
September 23, 2010, at 10:41 PM by Christian Cerrito -
Changed lines 3-4 from:
!!Knock
to:
!!!Knock
September 19, 2010, at 10:07 PM by Christian Cerrito -
Changed lines 1-2 from:
''Examples > Analog I/O''
to:
''Examples > Sensors''
September 18, 2010, at 07:07 AM by Christian Cerrito -
Added lines 68-70:


September 18, 2010, at 07:07 AM by Christian Cerrito -
Added lines 66-67:

September 18, 2010, at 06:29 AM by Christian Cerrito -
Added lines 51-65:

!!!See Also:

* [[Reference/PinMode | @@pinMode()@@ ]]
* [[Reference/AnalogRead| @@analogRead()@@]]
* [[Reference/If | @@if()@@]]
* [[Serial/Begin | @@serial.begin()@@]]
* [[Serial/Print | @@serial.print()@@]]


* [[Tutorial/AnalogInput]] - Use a potentiometer to control the blinking of an LED.
* [[Tutorial/AnalogInOutSerial]] - read an analog input, map its values, and then use that information to dim or brighten an LED.
* [[Tutorial/Memsic2125]] - read a two axis accelerometer.
* [[Tutorial/ADXL3xx]] - read a ADXL3xx accelerometer.
* [[Tutorial/Ping]] - detect objects with an ultrasonic range finder.
September 18, 2010, at 06:26 AM by Christian Cerrito -
Added lines 45-46:

In the code below, the incoming piezo data is compared to a threshold value set by the user. Try raising or lowering this value to increase your sensor's overall sensitivity.
September 18, 2010, at 06:22 AM by Christian Cerrito -
Changed lines 5-14 from:
This tutorial shows you how to use a [[ http://en.wikipedia.org/wiki/Piezoelectric_sensor | '''Piezo element''']] to detect vibration, in this case, a knock on a door or a table.

A '''piezo''' is an electronic device that generates a voltage when it's physically deformed by a vibration, sound wave, or mechanical strain. Similarly, when you put a voltage across a piezo, it vibrates and creates a tone. Piezos can be used both to play tones and to detect tones. In this example you're conecting the piezo to analog input pin 0. The sketch reads the changing voltage using the @@analogRead()@@ command, encoding the voltage range from 0 to 5 volts to a numerical range from 0 to 1023 in a process referred to as '''analog-to-digital conversion''', or '''ADC'''.

Piezos are '''polarized''', meaning that voltage passes through them (or out of them) in a specific direction. Connect the black wire (the lower voltage) to ground and the red wire (the higher voltage) to the input. You also have to connect a 1-megohm resistor in parallel to the Piezo element to limit the voltage and current produced by the piezo and to protect the analog input.

It is possible to acquire piezo elements without a plastic housing. These will look like a metallic disc, and are easier to use as input sensors.

The sketch below will read the piezo's output. If it is stronger than a certain threshold, it will send the string "Knock!" to the computer over the serial port. Open the serial monitor to see this text.
to:
This tutorial shows you how to use a [[ http://en.wikipedia.org/wiki/Piezoelectric_sensor | '''Piezo element''']] to detect vibration, in this case, a knock on a door, table, or other solid surface.

A '''piezo''' is an electronic device that generates a voltage when it's physically deformed by a vibration, sound wave, or mechanical strain. Similarly, when you put a voltage across a piezo, it vibrates and creates a tone. Piezos can be used both to play tones and to detect tones.

The sketch reads the piezos output using the @@analogRead()@@ command, encoding the voltage range from 0 to 5 volts to a numerical range from 0 to 1023 in a process referred to as '''analog-to-digital conversion''', or '''ADC'''.

If the sensors output is stronger than a certain threshold, your Arduino will send the string "Knock!" to the computer over the serial port.

Open the serial monitor to see this text.

!!!Hardware Required
* Arduino Board
* (1) Piezo electric disc
* (1) Megohm resistor
* solid surface
Added lines 23-26:
Piezos are '''polarized''', meaning that voltage passes through them (or out of them) in a specific direction. Connect the black wire (the lower voltage) to ground and the red wire (the higher voltage) to analog pin 0. Additionally, connect a 1-megohm resistor in parallel to the Piezo element to limit the voltage and current produced by the piezo and to protect the analog input.

It is possible to acquire piezo elements without a plastic housing. These will look like a metallic disc, and are easier to use as input sensors. PIezo sensors work best when firmly pressed against, taped, or glued their sensing surface.
September 18, 2010, at 06:09 AM by Christian Cerrito -
Changed lines 5-11 from:
This tutorial shows you how to use a Piezo element to detect sound, in this case, a knock on a door or a table.

A '''piezo''' is an electronic device that generates a voltage when it's physically deformed. Similarly, when you put a voltage across a piezo, it vibrates. It can this be used both to play tones and to detect tones. In this example you're conecting the piezo to analog input pin 0. The sketch reads the changing voltage using the @@analogRead()@@ command, encoding the voltage range from 0 to 5 volts to a numerical range from 0 to 1023. This is referred to as '''analog-to-digital conversion''', or '''ADC'''. The @@analogWrite()@@ function performs the ADC.

Piezos are '''polarized''', meaning that voltage passes through them (or out of them) in a specific direction.
Connect the black wire (the lower voltage) to ground and the red wire (the higher voltage) to the input. You also have to connect a 1-megohm resistor in parallel to the Piezo element to limit the voltage and current produced by the piezo and to protect the analog input.
to:
This tutorial shows you how to use a [[ http://en.wikipedia.org/wiki/Piezoelectric_sensor | '''Piezo element''']] to detect vibration, in this case, a knock on a door or a table.

A '''piezo''' is an electronic device that generates a voltage when it's physically deformed by a vibration, sound wave, or mechanical strain. Similarly, when you put a voltage across a piezo, it vibrates and creates a tone. Piezos can be used both to play tones and to detect tones. In this example you're conecting the piezo to analog input pin 0. The sketch reads the changing voltage using the @@analogRead()@@ command, encoding the voltage range from 0 to 5 volts to a numerical range from 0 to 1023 in a process referred to as '''analog-to-digital conversion''', or '''ADC'''.

Piezos are '''polarized''', meaning that voltage passes through them (or out of them) in a specific direction. Connect the black wire (the lower voltage) to ground and the red wire (the higher voltage) to the input. You also have to connect a 1-megohm resistor in parallel to the Piezo element to limit the voltage and current produced by the piezo and to protect the analog input.
September 18, 2010, at 06:03 AM by Christian Cerrito -
Changed lines 29-31 from:
%width=400px%[[Attach:knock_schem.png | Attach:knock_schem.png]]

to:
%width=400px%[[Attach:knock2_schem.png | Attach:knock2_schem.png]]

September 18, 2010, at 05:38 AM by Christian Cerrito -
September 17, 2010, at 10:56 PM by Tom Igoe -
Changed lines 7-8 from:
A '''piezo''' is an electronic device that generates a voltag when it's physically deformed. Similarly, when you put a voltage across a piezo, it vibrates. It can this be used both to play tones and to detect tones. In this example you're conecting the piezo to analog input pin 0. The sketch reads the changing voltage using the @@analogRead()@@ command, encoding the voltage range from 0 to 5 volts to a numerical range from 0 to 1023. This is referred to as '''analog-to-digital conversion''', or '''ADC'''. The @@analogWrite()@@ function performs the ADC.
to:
A '''piezo''' is an electronic device that generates a voltage when it's physically deformed. Similarly, when you put a voltage across a piezo, it vibrates. It can this be used both to play tones and to detect tones. In this example you're conecting the piezo to analog input pin 0. The sketch reads the changing voltage using the @@analogRead()@@ command, encoding the voltage range from 0 to 5 volts to a numerical range from 0 to 1023. This is referred to as '''analog-to-digital conversion''', or '''ADC'''. The @@analogWrite()@@ function performs the ADC.
September 16, 2010, at 10:19 PM by Tom Igoe -
Changed line 37 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Sensors/Knock/Knock.pde language=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/6.Sensors/Knock/Knock.pde lang=arduino tabwidth=4:)
February 24, 2010, at 04:29 AM by Tom Igoe -
Changed lines 37-47 from:
%color=#7e7e7e%/* Knock Sensor
%color=#7e7e7e%
%color=#7e7e7e% This sketch reads a piezo element to detect a knocking sound.
%color=#7e7e7e% It reads an analog pin and compares the result to a set threshold.
%color=#7e7e7e% If the result is greater than the threshold, it writes
%color=#7e7e7e% "knock" to the serial port, and toggles the LED on pin 13.
%color=#7e7e7e%
%color=#7e7e7e% The circuit:
%color=#7e7e7e% * + connection of the piezo attached to analog in 0
%color=#7e7e7e% * - connection of the piezo attached to ground
%color=#7e7e7e% * 1-megohm resistor attached from analog in 0 to ground
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Sensors/Knock/Knock.pde language=arduino tabwidth=4:)
Changed lines 39-80 from:
%color=#7e7e7e% http://www.arduino.cc/en/Tutorial/Knock
%color=#7e7e7e%
%color=#7e7e7e% created 25 Mar 2007
%color=#7e7e7e% by David Cuartielles <http://www.0j0.org>
%color=#7e7e7e% modified 30 Jun 2009
%color=#7e7e7e% by Tom Igoe

%color=#7e7e7e% */


%color=#7e7e7e%// these constants won't change:
const %color=#cc6600%int%% ledPin = 13; %color=#7e7e7e%// led connected to digital pin 13
const %color=#cc6600%int%% knockSensor = 0; %color=#7e7e7e%// the piezo is connected to analog pin 0
const %color=#cc6600%int%% threshold = 100; %color=#7e7e7e%// threshold value to decide when the detected sound is a knock or not


%color=#7e7e7e%// these variables will change:
%color=#cc6600%int%% sensorReading = 0; %color=#7e7e7e%// variable to store the value read from the sensor pin
%color=#cc6600%int%% ledState = %color=#006699%LOW%%; %color=#7e7e7e%// variable used to store the last LED status, to toggle the light

%color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
%color=#cc6600%pinMode%%(ledPin, %color=#006699%OUTPUT%%); %color=#7e7e7e%// declare the ledPin as as OUTPUT
%color=#cc6600%Serial%%.%color=#cc6600%begin%%(9600); %color=#7e7e7e%// use the serial port
}

%color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
%color=#7e7e7e%// read the sensor and store it in the variable sensorReading:
sensorReading = %color=#cc6600%analogRead%%(knockSensor);

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

(:divend:)
to:
(:divend:)
August 27, 2009, at 08:48 PM by Tom Igoe -
Added lines 22-24:
[-image developed using [[http://www.fritzing.org |Fritzing]]. For more circuit examples, see the [[http://fritzing.org/projects/|Fritzing project page]] -]

July 05, 2009, at 07:46 PM by Tom Igoe -
Changed lines 31-32 from:
[@
/* Knock Sensor
to:
!!!Code
(:div class=code :)
Changed lines 34-37 from:
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.
to:
%color=#7e7e7e%/* Knock Sensor
%color=#7e7e7e%
%color=#7e7e7e% This sketch reads a piezo element to detect a knocking sound.
%color=#7e7e7e% It reads an analog pin and compares the result to a set threshold.
%color=#7e7e7e% If the result is greater than the threshold, it writes
%color=#7e7e7e% "knock" to the serial port, and toggles the LED on pin 13.
%color=#7e7e7e%
%color=#7e7e7e% The circuit:
%color=#7e7e7e% * + connection of the piezo attached to analog in 0
%color=#7e7e7e% * - connection of the piezo attached to ground
%color=#7e7e7e% * 1-megohm resistor attached from analog in 0 to ground

%color=#7e7e7e% http://www.arduino.cc/en/Tutorial/Knock
%color=#7e7e7e%
%color=#7e7e7e% created 25 Mar 2007
%color=#7e7e7e% by David Cuartielles <http://www.0j0.org>
%color=#7e7e7e% modified 30 Jun 2009
%color=#7e7e7e% by Tom Igoe

%color=#7e7e7e% */
Deleted lines 54-66:
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

http://www.arduino.cc/en/Tutorial/Knock

created 25 Mar 2007
by David Cuartielles <http://www.0j0.org>
modified 30 Jun 2009
by Tom Igoe

*/
Changed lines 56-88 from:

// these constants won't change:
const int ledPin = 13; // led connected to digital pin 13
const int knockSensor = 0; // 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


// these variables will change:
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(9600); // 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
}

@]
to:
%color=#7e7e7e%// these constants won't change:
const %color=#cc6600%int%% ledPin = 13; %color=#7e7e7e%// led connected to digital pin 13
const %color=#cc6600%int%% knockSensor = 0; %color=#7e7e7e%// the piezo is connected to analog pin 0
const %color=#cc6600%int%% threshold = 100; %color=#7e7e7e%// threshold value to decide when the detected sound is a knock or not


%color=#7e7e7e%// these variables will change:
%color=#cc6600%int%% sensorReading = 0; %color=#7e7e7e%// variable to store the value read from the sensor pin
%color=#cc6600%int%% ledState = %color=#006699%LOW%%; %color=#7e7e7e%// variable used to store the last LED status, to toggle the light

%color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
%color=#cc6600%pinMode%%(ledPin, %color=#006699%OUTPUT%%); %color=#7e7e7e%// declare the ledPin as as OUTPUT
%color=#cc6600%Serial%%.%color=#cc6600%begin%%(9600); %color=#7e7e7e%// use the serial port
}

%color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
%color=#7e7e7e%// read the sensor and store it in the variable sensorReading:
sensorReading = %color=#cc6600%analogRead%%(knockSensor);

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

(:divend:)
June 30, 2009, at 10:17 PM by Tom Igoe -
Changed lines 33-42 from:
* 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
to:

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

http://www.arduino.cc/en/Tutorial/Knock

created 25 Mar 2007
by David Cuartielles <http://www.0j0.org>
modified 30 Jun 2009
by Tom Igoe
Changed lines 52-58 from:

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
to:


// these constants won't change:
const int ledPin = 13; // led connected to digital pin 13
const int knockSensor = 0; // 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


// these variables will change:
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
Changed lines 70-76 from:
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
}
to:
// 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
June 30, 2009, at 10:10 PM by Tom Igoe -
Changed line 9 from:
Piezos are ''polarized''', meaning that voltage passes through them (or out of them) in a specific direction.
to:
Piezos are '''polarized''', meaning that voltage passes through them (or out of them) in a specific direction.
June 30, 2009, at 10:10 PM by Tom Igoe -
Changed lines 7-8 from:
A '''piezo'' is an electronic device that generates a voltag when it's physically deformed. Similarly, when you put a voltage across a piezo, it vibrates. It can this be used both to play tones and to detect tones. In this example you're conecting the piezo to analog input pin 0. The sketch reads the changing voltage using the @@analogRead()@@ command, encoding the voltage range from 0 to 5 volts to a numerical range from 0 to 1023. This is referred to as '''analog-to-digital conversion''', or '''ADC'''. The @@analogWrite()@@ function performs the ADC.
to:
A '''piezo''' is an electronic device that generates a voltag when it's physically deformed. Similarly, when you put a voltage across a piezo, it vibrates. It can this be used both to play tones and to detect tones. In this example you're conecting the piezo to analog input pin 0. The sketch reads the changing voltage using the @@analogRead()@@ command, encoding the voltage range from 0 to 5 volts to a numerical range from 0 to 1023. This is referred to as '''analog-to-digital conversion''', or '''ADC'''. The @@analogWrite()@@ function performs the ADC.
June 30, 2009, at 10:06 PM by Tom Igoe -
Changed lines 26-28 from:
%width=400px%[[Attach:knock_schem.png | Attach:accelerometer_knock.png]]

to:
%width=400px%[[Attach:knock_schem.png | Attach:knock_schem.png]]

June 30, 2009, at 10:05 PM by Tom Igoe -
Changed lines 20-21 from:
%width=400px%[[Attach:knock_bb.png | Attach:accelerometer_bb.png]]
to:
%width=400px%[[Attach:knock_bb.png | Attach:knock_bb.png]]
June 30, 2009, at 09:51 PM by Tom Igoe -
Changed lines 5-16 from:
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.

[[http://static.flickr.com/28/53535494_73f63436cb_o.jpg|http://static.flickr.com/28/53535494_73f63436cb.jpg]]

''Example of connection of a Piezo to analog pin 0 with a resistor''
to:
This tutorial shows you how to use a Piezo element to detect sound, in this case, a knock on a door or a table.

A '''piezo'' is an electronic device that generates a voltag when it's physically deformed. Similarly, when you put a voltage across a piezo, it vibrates. It can this be used both to play tones and to detect tones. In this example you're conecting the piezo to analog input pin 0. The sketch reads the changing voltage using the @@analogRead()@@ command, encoding the voltage range from 0 to 5 volts to a numerical range from 0 to 1023. This is referred to as '''analog-to-digital conversion''', or '''ADC'''. The @@analogWrite()@@ function performs the ADC.

Piezos are ''polarized''', meaning that voltage passes through them (or out of them) in a specific direction.
Connect the black wire (the lower voltage) to ground and the red wire (the higher voltage) to the input. You also have to connect a 1-megohm resistor in parallel to the Piezo element to limit the voltage and current produced by the piezo and to protect the analog input.

It is possible to acquire piezo elements without a plastic housing. These will look like a metallic disc, and are easier to use as input sensors.

The sketch below will read the piezo's output. If it is stronger than a certain threshold, it will send the string "Knock!" to the computer over the serial port. Open the serial monitor to see this text.

!!!Circuit

[-click the image to enlarge-]

%width=400px%[[Attach:knock_bb.png | Attach:accelerometer_bb.png]]

'''Schematic:'''

[-click the image to enlarge-]

%width=400px%[[Attach:knock_schem.png | Attach:accelerometer_knock.png]]


''A Piezo to attached to analog pin 0 with a 1-Megohm resistor''
April 09, 2008, at 01:21 AM by Paul Badger -
Changed line 49 from:
}
to:
}
April 09, 2008, at 01:21 AM by Paul Badger -
Changed line 48 from:
delay(10); // short delay to avoid overloading the serial port
to:
delay(10); // short delay to avoid overloading the serial port
April 09, 2008, at 01:21 AM by Paul Badger -
Changed lines 47-49 from:
Serial.println("Knock!"); // send the string "Knock!" back to the computer, followed by newline

delay(10); // short delay to avoid overloading the serial port
to:
Serial.println("Knock!"); // send the string "Knock!" back to the computer, followed by newline
delay(10); // short delay to avoid overloading the serial port
April 09, 2008, at 01:20 AM by Paul Badger -
Added lines 48-49:

delay(10); // short delay to avoid overloading the serial port
Deleted line 50:
delay(100); // we have to make a delay to avoid overloading the serial port
March 25, 2007, at 10:42 AM by David A. Mellis -
Changed lines 1-2 from:
!!Knock Sensor
to:
''Examples > Analog I/O''

!!Knock
March 25, 2007, at 10:42 AM by David A. Mellis -
Changed lines 9-10 from:
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. [[ http://www.processing.org | Processing ]]. Later in this article we propose a program that works for the software designed by Reas and Fry.
to:
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.
Changed line 15 from:
[=
to:
[@
Changed lines 50-117 from:
=]

!!!Representing the Knock in Processing
If, e.g. we would like to capture this "knock" from the Arduino board, we have to look into how the information is transferred from the board over the serial port.
First we see that whenever there is a knock bigger that the threshold, the program is printing (thus sending) "Knock!" over the serial port. Directly after sends the byte 10, what stands for EOLN or End Of LiNe, and byte 13, or CR - Carriage Return.
Those two symbols will be useful to determine when the message sent by the board is over. Once that happens, the processing program will toggle the background color of the screen and print out "Knock!" in the command line.

[=
// Knock In
// by David Cuartielles <http://www.0j0.org>
// based on Analog In by Josh Nimoy <http://itp.jtnimoy.com>

// Reads a value from the serial port and makes the background
// color toggle when there is a knock on a piezo used as a knock
// sensor.
// Running this example requires you have an Arduino board
// as peripheral hardware sending values and adding an EOLN + CR
// in the end. More information can be found on the Arduino
// pages: http://www.arduino.cc

// Created 23 November 2005
// Updated 23 November 2005

import processing.serial.*;

String buff = "";
int val = 0;
int NEWLINE = 10;

Serial port;

void setup()
{
size(200, 200);

// Open your serial port
port = new Serial(this, "COMXX", 9600); // <-- SUBSTITUTE COMXX with your serial port name!!
}

void draw()
{
// Process each one of the serial port events
while (port.available() > 0) {
serialEvent(port.read());
}
background(val);
}

void serialEvent(int serial)
{
if(serial != NEWLINE) {
buff += char(serial);
} else {
buff = buff.substring(1, buff.length()-1);
// Capture the string and print it to the commandline
// we have to take from position 1 because
// the Arduino sketch sends EOLN (10) and CR (13)
if (val == 0) {
val = 255;
} else {
val = 0;
}
println(buff);
// Clear the value of "buff"
buff = "";
}
}
=]
to:
@]
March 25, 2007, at 10:25 AM by David A. Mellis -
Added lines 1-117:
!!Knock Sensor

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 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. [[ http://www.processing.org | Processing ]]. Later in this article we propose a program that works for the software designed by Reas and Fry.

[[http://static.flickr.com/28/53535494_73f63436cb_o.jpg|http://static.flickr.com/28/53535494_73f63436cb.jpg]]

''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(100); // we have to make a delay to avoid overloading the serial port
}

=]

!!!Representing the Knock in Processing
If, e.g. we would like to capture this "knock" from the Arduino board, we have to look into how the information is transferred from the board over the serial port.
First we see that whenever there is a knock bigger that the threshold, the program is printing (thus sending) "Knock!" over the serial port. Directly after sends the byte 10, what stands for EOLN or End Of LiNe, and byte 13, or CR - Carriage Return.
Those two symbols will be useful to determine when the message sent by the board is over. Once that happens, the processing program will toggle the background color of the screen and print out "Knock!" in the command line.

[=
// Knock In
// by David Cuartielles <http://www.0j0.org>
// based on Analog In by Josh Nimoy <http://itp.jtnimoy.com>

// Reads a value from the serial port and makes the background
// color toggle when there is a knock on a piezo used as a knock
// sensor.
// Running this example requires you have an Arduino board
// as peripheral hardware sending values and adding an EOLN + CR
// in the end. More information can be found on the Arduino
// pages: http://www.arduino.cc

// Created 23 November 2005
// Updated 23 November 2005

import processing.serial.*;

String buff = "";
int val = 0;
int NEWLINE = 10;

Serial port;

void setup()
{
size(200, 200);

// Open your serial port
port = new Serial(this, "COMXX", 9600); // <-- SUBSTITUTE COMXX with your serial port name!!
}

void draw()
{
// Process each one of the serial port events
while (port.available() > 0) {
serialEvent(port.read());
}
background(val);
}

void serialEvent(int serial)
{
if(serial != NEWLINE) {
buff += char(serial);
} else {
buff = buff.substring(1, buff.length()-1);
// Capture the string and print it to the commandline
// we have to take from position 1 because
// the Arduino sketch sends EOLN (10) and CR (13)
if (val == 0) {
val = 255;
} else {
val = 0;
}
println(buff);
// Clear the value of "buff"
buff = "";
}
}
=]

Share