Motion detector alarm circuit (Constant Alarm when uploading code)

Hello, I am new to electronics and I have decided to do a Motion detector alarm circuit as part of my project.

The Buzzer is constantly emitting sound as soon as i upload the code. I am not sure where i am going wrong. Can someone please help me out here. If possible rewrite the code using the inputs and outputs declared.

Kindest Regards,

// Uses a PIR sensor to detect movement, buzzes a buzzer

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;                    // variable for reading the pin status
int pinSpeaker = 10;           //Set up a speaker on a PWM pin (digital 9, 10, or 11)

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

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

 
 if (pirState == LOW) {
   // we have just turned on
   Serial.println("Motion detected!");
   // We only want to print on the output change, not state
   pirState = HIGH;
 }
} else {
   digitalWrite(ledPin, LOW); // turn LED OFF
   playTone(0, 0);
   delay(300);    
   if (pirState == HIGH){
   // we have just turned off
   Serial.println("Motion ended!");
   // We only want to print on the output change, not state
   pirState = LOW;
 }
}
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
 duration *= 1000;
 int period = (1.0 / freq) * 1000000;
 long elapsed_time = 0;
 while (elapsed_time < duration) {
     digitalWrite(pinSpeaker,HIGH);
     delayMicroseconds(period / 2);
     digitalWrite(pinSpeaker, LOW);
     delayMicroseconds(period / 2);
     elapsed_time += (period);
 }
}

Schematic ?
Edit your post and use CODETAGS (the “</>” toolbutton after highlighting the code.
Always use that toolbutton when posting code.

raschemmel:
Schematic ?
Edit your post and use CODETAGS (the “</>” toolbutton after highlighting the code.
Always use that toolbutton when posting code.

here’s the schematic How to Build a Motion Detector Alarm Circuit

Thats exactly how i’ve done it except i have used a LED on the arduino. Also i still didn’t get how to use codetags. it just comes up with CODE

Faheem23: here's the schematic http://www.learningaboutelectronics.com/Articles/Motion-detector-alarm-circuit.php

Thats exactly how i've done it except i have used a LED on the arduino. Also i still didn't get how to use codetags. it just comes up with CODE

Just place your code between the CODE tags! It will show as code then!

Your code I/O pin numbers don’t match the pin numbers used in the link you posted.

// Uses a PIR sensor to detect movement, buzzes a buzzer



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;                    // variable for reading the pin status
int pinSpeaker = 10;           //Set up a speaker on a PWM pin (digital 9, 10, or 11)

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

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

  
  if (pirState == LOW) {
    // we have just turned on
    Serial.println("Motion detected!");
    // We only want to print on the output change, not state
    pirState = HIGH;
  }
} else {
    digitalWrite(ledPin, LOW); // turn LED OFF
    playTone(0, 0);
    delay(300);    
    if (pirState == HIGH){
    // we have just turned off
    Serial.println("Motion ended!");
    // We only want to print on the output change, not state
    pirState = LOW;
  }
}
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
  duration *= 1000;
  int period = (1.0 / freq) * 1000000;
  long elapsed_time = 0;
  while (elapsed_time < duration) {
      digitalWrite(pinSpeaker,HIGH);
      delayMicroseconds(period / 2);
      digitalWrite(pinSpeaker, LOW);
      delayMicroseconds(period / 2);
      elapsed_time += (period);
  }
}

Arduino-motion-detector-alarm-circuit-schematic.png

raschemmel:
Your code I/O pin numbers don’t match the pin numbers used in the link you posted.

// Uses a PIR sensor to detect movement, buzzes a buzzer

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;                    // variable for reading the pin status
int pinSpeaker = 10;          //Set up a speaker on a PWM pin (digital 9, 10, or 11)

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

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

if (pirState == LOW) {
    // we have just turned on
    Serial.println(“Motion detected!”);
    // We only want to print on the output change, not state
    pirState = HIGH;
  }
} else {
    digitalWrite(ledPin, LOW); // turn LED OFF
    playTone(0, 0);
    delay(300);   
    if (pirState == HIGH){
    // we have just turned off
    Serial.println(“Motion ended!”);
    // We only want to print on the output change, not state
    pirState = LOW;
  }
}
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
  duration *= 1000;
  int period = (1.0 / freq) * 1000000;
  long elapsed_time = 0;
  while (elapsed_time < duration) {
      digitalWrite(pinSpeaker,HIGH);
      delayMicroseconds(period / 2);
      digitalWrite(pinSpeaker, LOW);
      delayMicroseconds(period / 2);
      elapsed_time += (period);
  }
}





![Arduino-motion-detector-alarm-circuit-schematic.png|622x535](upload://kVrvE9AJzNlE7XIX0FBn1diDr9B.png)

raschemmel:
Your code I/O pin numbers don’t match the pin numbers used in the link you posted.

// Uses a PIR sensor to detect movement, buzzes a buzzer

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;                    // variable for reading the pin status
int pinSpeaker = 10;          //Set up a speaker on a PWM pin (digital 9, 10, or 11)

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

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

if (pirState == LOW) {
    // we have just turned on
    Serial.println(“Motion detected!”);
    // We only want to print on the output change, not state
    pirState = HIGH;
  }
} else {
    digitalWrite(ledPin, LOW); // turn LED OFF
    playTone(0, 0);
    delay(300);   
    if (pirState == HIGH){
    // we have just turned off
    Serial.println(“Motion ended!”);
    // We only want to print on the output change, not state
    pirState = LOW;
  }
}
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
  duration *= 1000;
  int period = (1.0 / freq) * 1000000;
  long elapsed_time = 0;
  while (elapsed_time < duration) {
      digitalWrite(pinSpeaker,HIGH);
      delayMicroseconds(period / 2);
      digitalWrite(pinSpeaker, LOW);
      delayMicroseconds(period / 2);
      elapsed_time += (period);
  }
}





![Arduino-motion-detector-alarm-circuit-schematic.png|622x535](upload://kVrvE9AJzNlE7XIX0FBn1diDr9B.png)

This is the website i used to build it but both are really the same. They’re connected to to the same pins etc

http://makezine.com/projects/pir-sensor-arduino-alarm/

The inputs and outputs do match? What should it be ?

pinSpeaker= 10 does not = D12

raschemmel: pinSpeaker= 10 does not = D12

Ive sussed it. Thank god. Now it only emits sound when motion is detected. Only issue here now is as soon as its detected it rings for 3 seconds and switches off. when it switches off i wave at the PIR sensor nothing happens until after another 3 seconds. Not sure what i need to do to make it to sound constantly as since it's a motion detector alarm circuit it needs to ring constantly and then as soon as i press reset it should stop until again motion is detected. Please help me mate :(

Try reducing the values that are not part of the tone frequency code to see how that affects it.

I think you can just put an infinite (empty) WHILE loop at the end of the tone code .

raschemmel:
Try reducing the values that are not part of the tone frequency code to see how that affects it.

I think you can just put an infinite (empty) WHILE loop at the end of the tone code .

i tried everything nothing happening. What is the exact code you’d type after the delay(150);

Faheem23: Ive sussed it. Thank god. Now it only emits sound when motion is detected. Only issue here now is as soon as its detected it rings for 3 seconds and switches off. when it switches off i wave at the PIR sensor nothing happens until after another 3 seconds. Not sure what i need to do to make it to sound constantly as since it's a motion detector alarm circuit it needs to ring constantly and then as soon as i press reset it should stop until again motion is detected. Please help me mate :(

Typical PIR behaviour.

You need to latch an alarm state in your code until you press reset.

I see no reset button in your circuit though.

Boardburner2: Typical PIR behaviour.

You need to latch an alarm state in your code until you press reset.

I see no reset button in your circuit though.

The Reset button is on the Arduino board. Once the PIR is triggered all I need to do is press the reset button and it's back to the beginnings. Also I still cannot get it to alarm constantly. It just lasts for 3 seconds and switches off with a pause of 2 seconds (even though I wave at the PIR after it stops)

Faheem23: The Reset button is on the Arduino board. Once the PIR is triggered all I need to do is press the reset button and it's back to the beginnings. Also I still cannot get it to alarm constantly. It just lasts for 3 seconds and switches off with a pause of 2 seconds (even though I wave at the PIR after it stops)

Have you checked the Trigger Selection Jumper? You can have it set for single or repeatable triggers upon motion detection.

Have a look down on this page --> http://henrysbench.capnfatz.com/henrys-bench/arduino-sensors-and-input/arduino-hc-sr501-motion-sensor-tutorial/

Good luck!