Hide minor edits - Show changes to markup
Este tutorial muestra como usar un elemento PIEZO para detectar sonidos, en este caso un golpeteo en una puerta o sobre una mesa.
Un piezo es un dispositivo electrónico que genera un voltaje cuando se deforma físicamente. De la misma forma, cuando aplicas un voltaje al dispisitivo piezo, éste vibra. Por esta razón este dispositivo puede ser empleado tanto para producir sonidos como para detectarlos. En este ejemplo se debe conectar el dispositivo piezo a la entrada analógica de la placa Arduino del pin 0. El programa "sketch lee los cambios de voltaje usando la función analogRead(), codificando el voltaje de entre 0 y 5 voltios en un rango numérico de entre 0 y 1023. Esto se conoce como conversión analógico a digital o ADC. La función analogWrite() lleva a cabo esta ADC.
Este tutorial muestra como usar un elemento piezo-eléctrico (o micrófono de contacto) para detectar sonidos, en este caso un golpeteo en una puerta o sobre una mesa.
Un piezo es un dispositivo electrónico que genera un voltaje cuando se deforma físicamente. De la misma forma, cuando aplicas un voltaje al dispisitivo piezo, éste vibra. Por esta razón este dispositivo puede ser empleado tanto para producir sonidos como para detectarlos. En este ejemplo se debe conectar el dispositivo piezo a la entrada analógica de la placa Arduino del pin 0. El programa -sketch- lee los cambios de voltaje usando la función analogRead(), codificando el voltaje de entre 0 y 5 voltios en un rango numérico de entre 0 y 1023. Esto se conoce como conversión analógico a digital o ADC. La función analogWrite() lleva a cabo esta ADC.
Un elemento piezo conectado al pin 0 de entrada analógica con una resistencia de 1 Megaohm
Un dispositivo piezo conectado al pin 0 de entrada analógica con una resistencia de 1 Megaohm
Examples > Analog I/O
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.
click the image to enlarge
Ejemplos > E/S analógicas
Este tutorial muestra como usar un elemento PIEZO para detectar sonidos, en este caso un golpeteo en una puerta o sobre una mesa.
Un piezo es un dispositivo electrónico que genera un voltaje cuando se deforma físicamente. De la misma forma, cuando aplicas un voltaje al dispisitivo piezo, éste vibra. Por esta razón este dispositivo puede ser empleado tanto para producir sonidos como para detectarlos. En este ejemplo se debe conectar el dispositivo piezo a la entrada analógica de la placa Arduino del pin 0. El programa "sketch lee los cambios de voltaje usando la función analogRead(), codificando el voltaje de entre 0 y 5 voltios en un rango numérico de entre 0 y 1023. Esto se conoce como conversión analógico a digital o ADC. La función analogWrite() lleva a cabo esta ADC.
Los piezos son dispositivos polarizados, que quiere decir que la corriente pasa a través de ellos (o sale de ellos) en una dirección específica. Conecta el cable negro (el negativo) al pin negativo de la placa, y el cable rojo (el positivo) a un pin analógico de entrada. También en necesario conectar una resistencia de 1 megaohmnio en paralelo al dispositivo piezo para limitar el voltaje y la corriente producida por el piezo y proteger así la entrada analógica.
Es posible comprar elementos piezo sin la carcasa de plástico en los que suelen venir metidos. En ese caso se ve que estos dispositivos son unos discos metálicos muy finos, y que son fáciles de usar como sensores de entrada.
El programa que se muestra a continuación lee el voltaje procedente del elemento piezo. Si el sonido es mayor que un determinado umbral, envía un texto "Knok!" ("Toc-Toc") al ordenador a través de puerto serie. Abre la ventana del puerto serie en el entorno IDE Arduino ("Serial Monitor") para ver este texto.
pulsa sobre la imagen para verla más grande.
image developed using Fritzing. For more circuit examples, see the Fritzing project page
Schematic:
click the image to enlarge
imagen creada con el programa Fritzing. Para ver más circuitos de ejemplo consulta lapágina del proyecto Fritzing
Esquema:
pulsa sobre la imagen para verla más grande.
A Piezo to attached to analog pin 0 with a 1-Megohm resistor
Un elemento piezo conectado al pin 0 de entrada analógica con una resistencia de 1 Megaohm
/* 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
(:includeurl http://arduino.googlecode.com/svn/trunk/build/shared/examples/Sensors/Knock/Knock.pde border=0:)
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
*/
// 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 }
(:divend:)
(:divend:)
image developed using Fritzing. For more circuit examples, see the Fritzing project page
[@ /* Knock Sensor
(:div class=code :)
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.
/* 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
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
*/
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
*/
// 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
}
@]
// 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 }
(:divend:)
* 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
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
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
// 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
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
}
// 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
Piezos are polarized', meaning that voltage passes through them (or out of them) in a specific direction.
Piezos are polarized, meaning that voltage passes through them (or out of them) in a specific direction.
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.
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.
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.jpg
Example of connection of a Piezo to analog pin 0 with a resistor
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.
click the image to enlarge
Schematic:
click the image to enlarge
A Piezo to attached to analog pin 0 with a 1-Megohm resistor
}
}
delay(10); // short delay to avoid overloading the serial port
delay(10); // short delay to avoid overloading the serial port
Serial.println("Knock!"); // send the string "Knock!" back to the computer, followed by newline
delay(10); // short delay to avoid overloading the serial port
Serial.println("Knock!"); // send the string "Knock!" back to the computer, followed by newline
delay(10); // short delay to avoid overloading the serial port
delay(10); // short delay to avoid overloading the serial port
delay(100); // we have to make a delay to avoid overloading the serial port
Examples > Analog I/O
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. Later in this article we propose a program that works for the software designed by Reas and Fry.
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.
[=
[@
=]
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 = "";
}
}
@]
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. 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.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
}
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 = "";
}
}