Greetings Arduino community, I am fiddling around with Arduino UNO and the sketch "Detect a Knock". When I change the value of serial output on line 51 to sensorReading I am getting numeric values. My question is what are those values? Thank you for kind responses.
Ah, the famous "Detect a Knock" sketch?
Sorry, never heard of it.
Please post it (use code tags!)
/*
Knock Sensor
This sketch reads a piezo element to detect a knocking sound.
It reads an analog pin and compares the result to a set threshold.
If the result is greater than the threshold, it writes "knock" to the serial
port, and toggles the LED on pin 13.
The circuit:
- positive connection of the piezo attached to analog in 0
- negative connection of the piezo attached to ground
- 1 megohm resistor attached from analog in 0 to ground
created 25 Mar 2007
by David Cuartielles <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Knock
*/
// these constants won't change:
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);
// 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
}
A digital representation of the analog signal at A0. It will be between 0 - 1023.
0 - 5V input will be represented as 0 - 1023 counts.
The voltage at A0 can be calculated by Vin= 5/1024*sensorReading
better to use Vin= 5.0 / 1023 * sensorReading;
otherwise the integral calculation will get you 0
It is the value read by the analog pin.
In the case of the Arduino UNO, it is 10 bits.
Ranging from 0 to 1023.
That is correct. Thanks
I am trying to convert the values to the voltage, but constantly getting output 0.
Currently I am just trying to understand why any calculation of sensorReading outputs 0. For example if I simply add (5.0 / 1023 * sensorReading) between the brackets on the line 51, I get 0. If I try to introduce new variable, same thing ... (float sensorReading2 = sensorReading * 0.1;)
try something like this
void loop() {
// read the sensor and store it in the variable sensorReading:
sensorReading = analogRead(knockSensor);
float voltage = 5.0 * sensorReading / 1023;
Serial.print("Voltage = "); Serial.println("voltage);
ā¢ā¢ā¢
}
Are you creating a knock? This line has to be satisfied before it will print
if (sensorReading >= threshold)
The threshold is the knock level
This works! Thanks.
Yes, the funny thing was that when I knocked, the serial monitor printed a message, but the message would always be 0 or 0.00
Found some old sketch to try.
Leo..
// 1" Piezo with 1Megohm resistor across, connected to A0 and ground
int threshold = 100; // alarm threshold from 1 (very sensitive) to 1023
int alarmDuration = 100; // alarm duration in milliseconds
const byte piezoPin = A0;
int rawValue; // raw A/D readings
int piezoValue; // peak value
const byte ledPin = 13; // onboard LED and/or buzzer on pin 13
void setup() {
analogReference(INTERNAL); // remove this line if too sensitive
Serial.begin(115200); // set serial monitor to this baud rate
pinMode (ledPin, OUTPUT);
}
void loop() {
piezoValue = 0; // reset
for (int x = 0; x < 250; x++) { // multiple A/D readings
rawValue = analogRead(piezoPin);
if (rawValue > piezoValue) piezoValue = rawValue; // store peaks
}
if (piezoValue) { // if > 0
Serial.print(F("Piezo value is "));
Serial.println(piezoValue);
if (piezoValue >= threshold) {
Serial.print(F("Knock was over the threshold of "));
Serial.println(threshold);
digitalWrite (ledPin, HIGH);
delay(alarmDuration);
digitalWrite (ledPin, LOW);
}
}
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.