
One came with an assortment of sensors and stuff that came with arduino from makershed, and the other is a small salvaged speaker from an old digital camera. I don't have data sheets for the piezos that I have.
#Arduino piezo sensor code serial#
send the string "Knock!" back to the computer, followed by newlineĭelay(100) // delay to avoid overloading the serial port buffer if the sensor reading is greater than the threshold: read the sensor and store it in the variable sensorReading: Serial.begin(9600) // use the serial port PinMode(ledPin, OUTPUT) // declare the ledPin as as OUTPUT Int ledState = LOW // variable used to store the last LED status, to toggle the light Int sensorReading = 0 // variable to store the value read from the sensor pin * 1-megohm resistor attached from analog in 0 to groundĬonst int ledPin = 13 // led connected to digital pin 13Ĭonst int knockSensor = 5 // the piezo is connected to analog pin 0Ĭonst int threshold = 100 // threshold value to decide when the detected sound is a knock or not * - connection of the piezo attached to ground * + connection of the piezo attached to analog in 0 "knock" to the serial port, and toggles the LED on pin 13.

If the result is greater than the threshold, it writes It reads an analog pin and compares the result to a set threshold. This sketch reads a piezo element to detect a knocking sound. I'll place the code below for reference: /* Knock Sensor
#Arduino piezo sensor code how to#
Guess not.Īny idea what could be wrong and/or how to fix it? And I am using the recommended 1 mega ohm resistor in parallel with the ground.Īnd that's about it. Only when I disconnect the wire to Analog 0 do I begin to get random values in the Serial.Ī few notes: Yes, I do have the piezo element oriented correctly in terms of +/- (it works fine in playing simple tunes for other sketches). However, it always reads "0" despite any amount of knocking I do. Anyway, I'm using the code provided and I've added the line: Serial.println(analogRead(knockSensor)) which should print out the sensor value regardless of whether or not it's hit the threshold. I'm fairly new to arduino, but I feel like this should not be an issue whatsoever. (had to break up the urls because it wouldn't let me post them on my first post) Alternate wiring diagrams for breadboard and info here: fritzing org/projects/knock/.


I'm following tutorial and code found at arduino cc/en/Tutorial/Knock which is also included with the arduino software. I'm trying to make a very simple knock sensor out of a piezo and it just wont give me anything.
