I tried to connect it to an Analog input and tried a different code and it works. However, everytime I connect it to the digital input it only works between when touched between my fingers.
Here is the second code when it's used with an Analog Pin an the piezo works when it's touched by anything.
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 = 500; // 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 = digitalRead(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
}
When on the floor it is installed under a rubber square that when people step on it activate the piezo so I get a βknockβ signal in Arduino and after it will be translated to a MIDI signal launching notes and abstract sounds.
In order to get a signal the piezoelectric crystals have to be deformed. It is during deformation that the voltage is generated.
Putting them under rubber is going to make it hard to deform.
It is not a very suitable solution to your problems.
I did a similar project many years ago and used conducting foam to make the sensors.
My guess when you are touching the piezo the 60Hz pickup from your body is being sensed by your processor.
I can't tell what signal you may get if the sensor is under a rubber mat. It likely depends on how fast the folks are walking.
In any case the signal will be a short spike if there even is one. You will NOT get a signal the entire time someone is standing on it. The Piezo element only output voltage during the time the piezo is being distorted or put under and removing pressure.
Piezoelectric transducers of the type shown respond to vibration rather than pressure. You need quite a rapid change of force to get a measurable output, and as the transducer is electrically a very small capacitor, the output voltage spike is very short-lived.