0

I have used PIR motion sensor with arduino Uno r3, alongwith one piezo buzzer. and the program is as

 /*
 * PIR sensor tester
 */

int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;  
int piezoPin = 11;
// variable for reading the pin status

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);
  pinMode(piezoPin, OUTPUT);
  Serial.begin(9600);
  // declare sensor as input

}

void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);
    tone(piezoPin,300,160);
    delay(150);

    // turn LED ON
    if (pirState == LOW) {
      // we have just turned on

      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
    digitalWrite(ledPin, LOW);
    tone(piezoPin,0,0);
    delay(300);

    // turn LED OFF
    if (pirState == HIGH){
      // we have just turned of

      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }
}

but it supposed to ring buzzer at the time of pir motion sensor getting signal. But buzzer continuously ringing and at the time of pir sensor getting signal, it just ring louder. What is the problem in program``

  • 1
    Do you have a "buzzer" or a "transducer"? One makes sound by itself when provided with power, and the other needs an audio signal. Your code is for the latter. If you have the former it won't work right. – Majenko Oct 7 '17 at 13:55
1

noTone()

Description

Stops the generation of a square wave triggered by tone(). Has no effect if no tone is being generated.

NOTE: if you want to play different pitches on multiple pins, you need to call noTone() on one pin before calling tone() on the next pin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.