Piezo film vibration sensor

Hi I am hoping someone can tell me where I am going wrong with this project.

I would like to detect vibrations to the sensor in a scale from 1-100 1 being the smallest vibration and 100 being the greatest.

I will post my current code below. It seems that if if the threshold is set to 100 its very hard to set off. If i put it lower at like 30 then it just goes off for no reason at all even if I am completely still.

If it barely touch it or blow on it lightly i would like it to read 1 or 1.01 and if i hit it hard I would like it to read 100.00

Also I am using a 1 meg ohm resistor across the sensor leads. which might be too high for the sensitivity that I would like.

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

// 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);
  int sensorValue=analogRead(A0);
float vibrator=sensorValue*(100/1023.0);     //convert the analog value of the sensor to a percentage 1-100

  // 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
  delay(100);  // delay to avoid overloading the serial port buffer

Any help would be great;y appreciated

Thanks in advance.

The code as it is seems like it is not reading how I thought it would even with the threshold at 100. The output numbers seem very random and don't coincide with how hard I tap the sensor.

You need to sit down and study the example carefully, until you understand what every line is doing.


 sensorReading = analogRead(knockSensor);
  int sensorValue=analogRead(A0);

Why do you read the sensor twice, into two different variables?

float vibrator=sensorValue*(100/1023.0);     //convert the analog value of the sensor to a percentage 1-100

The above line assumes that the sensor will produce values ranging from 0 to 1023, but you don't know that yet.

You never use the value of "vibrator".

Just have loop() continuously print out values that you get from the sensor until you see a pattern.

void loop() {

 // read the sensor and store it in the variable sensorValue
  int sensorValue=analogRead(A0);

  delay(100);  // delay to avoid overloading the serial port buffer