Arduino knock sensor with a twist (problem)

Hey there! I'm new to this forum so please bear with me :slight_smile:

I live in a dorm and there's no preinstalled doorbell so i thought i'd do something funkier.
I've used the stock Arduino Knock code and modified it a bit so agitating the piezo will make
a connected buzzer beep.

The problem is that while the entire setup is working, the buzzer itself is very very quiet.
With a pure tone making script the buzzer will make a loud BEEEP, but combined with the
knock code it's extremely quiet.

I'm new to Arduino's so please bear with me when i show my code (most is copy/paste to fit):

/* 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:
    * + connection of the piezo attached to analog in 0
    * - connection of the piezo attached to ground
    * 1-megohm resistor attached from analog in 0 to ground
  
   created 25 Mar 2007
   by David Cuartielles 
   modified 30 Jun 2009
   by Tom Igoe

 */
 

// these constants won't change:
int speakerPin = 9;
int ledpin = 13;
const int ledPin = 12;      // led connected to digital pin 13
const int knockSensor = 1;  // the piezo is connected to analog pin 0
const int threshold = 10;  // 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
}

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);
    buzz(9, 2500, 500); // buzz the buzzer on pin 9 at 2500Hz for 1000 milliseconds
  delay(100); 
  }
    // delay to avoid overloading the serial port buffer
}

void buzz(int targetPin, long frequency, long length) {
  long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
  //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  //// there are two phases to each cycle
  long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
  //// multiply frequency, which is really cycles per second, by the number of seconds to
  //// get the total number of cycles to produce
 for (long i=0; i < numCycles; i++){ // for the calculated length of time...
    digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
    delayMicroseconds(delayValue); // wait for the calculated delay value
    digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
    delayMicroseconds(delayValue); // wait again for the calculated delay value
  }
}

I'm close to giving up after spending some (a lot when you're studying) money on this project so i'd really apreciate some help.

ps: i hope this is the right section to get help - i couldn't find a deffinitive section to put this in :S so please bear with me if i posted it the wrong place.

I have some questions...

  1. If you put the buzz function into a simple Sketch (no knock sensor code) and call it, is the tone produced loud enough?

  2. When you run the test from question #1, is the knock sensor circuit powered?

  3. What buzzer are you using?

  1. If you put the buzz function into a simple Sketch (no knock sensor code) and call it, is the tone produced loud enough?

yes, it's loud enough with a clean melody sketch

  1. When you run the test from question #1, is the knock sensor circuit powered?

the sensor is powered and the sound is still loud and clear, the pins are not set as output though

  1. What buzzer are you using?

i'm using a salvaged motherboard piezo buzzer

I ended up using this code:

(code can be found by following the links)

And it works! I'm still wondering what was wrong with my (pasted) code though.

The only significant difference I can spot is this...

const int threshold = 10;  // threshold value to decide when the detected sound is a knock or not

...versus this...

int THRESHOLD = 100;

But I cannot see how that difference would affect the buzz. I'm at a loss.

I'm glad you got it working!

Yeah the threshhold value is just to define how much the piezo needs to be agitated before the arduino reacts (aka how hard a knock/vibration has to be).

Thx :smiley: it's fully working and connected to my dorm door, whenever i'm expecting company i can just flip the switch and whenever someone knocks i'll hear a dududu beep ^.^