Go Down

Topic: project 12 / knock lock: problem reading the input value of piezo (Read 17385 times) previous topic - next topic


Hi ,
i'm following the guideline from arduino project book for knock lock but i'm stuck reading the analogRead(piezoPin), it gives me always 1023.
If i detatch the piezo this value drop to 0 with little to no oscillation.
I really tried only the input part without the servo motor part but i suppose it's quite indipendent one from another.
my code:
const int piezoPin = A0;
int sound;

void setup(){

void loop(){
  sound = analogRead(piezoPin);
  Serial.print("Piezo value: ");

and i just plug in in A0 with 1 Mohm on the - of the piezo link with ground and A0, the + just go to Vcc

It's so simple that i'm quite clueless on what to do


and i just plug in in A0 with 1 Mohm on the - of the piezo link with ground and A0, the + just go to Vcc

This does not sound right.  See the circuit diagram here:

Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


I'm having the exact same problem. When using the diagram in the guidebook, the only value I get is 1023. When using the alternate diagram with the piezo and the resistor in parallel, I only get the value 0.
I'm thinking that maybe it has something to do with the piezo in my Kit being different from the piezo's shown on the Starter Kit page, I have no idea what else it could be... Is this possible? I'm new to Arduino and electronics, so I don't know if this could be the reason.


Sorry but i've got a little to do so i have tried the circuit proposed by johnwasser only now.
Same result as Pieter_C. Only a bunch of 0...
So i' don't know why but i'm quite sure now that what we have in our hand is different from the piezo's shown on the Starter Kit page.
I've seen other image of the piezo and mine effectively seems a little different at sight.
I suppose that me and Pieter_C are just unlucky!
But thanks anyway!


So i' don't know why but i'm quite sure now that what we have in our hand is different from the piezo's shown on the Starter Kit page.

Sometimes people will try to use a piezo buzzer thinking it's a piezo speaker.  The buzzer has some active electronics to play a tone when the buzzer is connected to power.  The speaker is just a piezo element and will do nothing when connected to power.

You want a piezo speaker or piezo element, not a piezo buzzer.

You can often find drum-machine toys at thrift stores and yard sales.  Each pad has a piezo element.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


im having exactly the same problem as they are, so as you say we have piezo buzzer probably when we should have piezo speaker/element?


I finally could solve the problem and it as nothing to do if its diferent piezo or not(tried with two). the values you're getting, in this case 1023 as to do with voltage going through piezo and going to analog pin(because obviously) the piezo is getting 5v from supply. So, after some research i found other way to connect piezo to circuit,like in picture below and i started to have normal readings. At first i was thinking that we would cause some resistance vibrating piezo, causing voltage divider and get a reading but we would need to create big resistance on piezo to get readings from 10 to 100 and i thought that is weird.But now, (although i dont get any votage read from multimeter),from what i understood we cause some voltage vibrating piezo and thats the voltage we read,because my values are around 15 i dont get that reading on multimeter anyway i think its how it works. cumps

votlage=(15/1024)*5= 0.07v



Unfortunately, the solution in the previous post doesn't help either. No matter what I do, I either get 1023 (wired as in the supplied starter kit guide) or 0 as a value from the piezo (wires as in schemes posted here). Annoying ... :(


Hi to everyone!

This is my first post and i hope is going to be helpful.

I found somewhere in the internet another way to connect the piezo. It works, and the values returned (in my case) are between 1 and 10. I attached the scheme of the connections.
Furthermore, the code in the book when the counter goes to three is wrong: it does not reset the counter numberOfKnocks, causing a persistent state of "unlocked".

Here is the code:
if(numberOfKnocks >= 3){
      locked = false;
      digitalWrite(greenLed, HIGH);
      digitalWrite(redLed, LOW);
      Serial.println("The box is unlocked!");
      numberOfKnocks = 0; //line added


I have the exact same problem stated above. I'm using the Arduino Starter kit itself, so the piezo should be the right one, unless the wrong one was in the box. It keeps showing 1023... no fluctuations whatsoever.
Also, my Voltmeter does not show any fluctuations when I hook it up to the piezo.


Maybe the documentation is wrong. You need to use an (high) pull-up resistor, not a pull-down one. In the original schematic you used a pull-down. But with my multimeter I saw a voltage of 4.85V on the upside of the resistor. So, is normal to see 1023 on the analog input. High voltage, high input. Also, with this high voltage you need to knock very hard to get a different value, something around 1015, because it is less sensitive. The minimal variation in voltage is shadowed by the presence of an high voltage. And also, the high and low limits of the knock value is from 10 to 100, so this mean you need to use low voltage. To achieve this you need an (high) pull-up resistor.
Connect one side of the 1 Megaohm resistor to 5V. Put the piezo in series with resistor, with the plus sign looking the resistor. Take the analog signal in the middle from the resistor and piezo. Connect the negative side of the piezo to ground. Now, at the analog pin, the multimeter show 5mV and the analog value is 0. When I knock (and not so hard) I can see a momentary voltage (few hundreds millivolts to 1.3V) and analog values ranging from 4/5 to 30.

A credit, and a lot of thanks, to Comme, for the software patch. You need to modify the code for resetting the counter numberOfKnocks or you get a persistent state of "unlocked"..

Hope this help.



Man i was stuck on this for ages too, I just solved mine here is how: Red of the piezo to one leads of the resistor then to Analog 2. Black of the piezo to the other lead of the resistor and then to ground. On the arduino code, you have to to put:


for some reason you have to put A2, idk why, and well at least it worked for me hope it helps


Hi there

I hope somebody respons

code aint working keeps doing  strange things

bad knocks and stuf

is the code wrong or what

i cant get it right

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131