Piezo Arduino Interfacing

I am working on a secret knock pattern based door opening and closing project. Issue I am facing is the fact that some times piezo is taking some randomly high value on constant bases without any input (around 300-400). Can you help me solve that? Is my program code doing something wrong or is it some interfacing issue or anything else?

knock.ino (1.93 KB)

do you have something like a 1Mega Ohm resistor attached to your piezo ?

cf tutorial here

Kavish5:
I am working on a secret knock pattern based door opening and closing project. Issue I am facing is the fact that some times piezo is taking some randomly high value on constant bases without any input (around 300-400). Can you help me solve that? Is my program code doing something wrong or is it some interfacing issue or anything else?

A piezo sensor is basically a ceramic capacitor you can bend, and a capacitor holds charge for ages
unless you let it leak away. Arduino analog inputs are extremely high resistance (10^10 ohms) so
won't do this for you, a resistor is needed.