126Views3Replies

Author Options:

PIR motion sensor,DC motor and Speaker alarm system help? Answered

I'm trying to create an alarm system using a PIR motion sensor, DC motor and a speaker so when someone gets close to it the DC motor should vibrate and sound and alarm through the speaker. (using and Arduino UNO) I've got  an external 9V battery powering it. the PIR and sound works fine I just want to hook up the DC motor to run along with everything else.  I've got some code for a button switch that could turn the motion sensor on and off when I want to use it also, if you know how to set that up that would help. I'm new to Arduino and circuits please help. 

Here is the code I'm using:

int ledPin = 13;                
int inputPin = 2;               
int pirState = LOW;             
int val = 0;                   
int pinSpeaker = 10;          
boolean on = true;

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

void loop() {   //button code for turning on and off the PIR
//  if (on == false) {
//    if (digitalRead(3) == HIGH) {
//      on = true;
//      Serial.println("turned on");
//    }
//    return;
//  }
// 
//  if (on == true) {
//    if (digitalRead(3) == HIGH) {
//      on = false;
//      Serial.println("turned off");
//    }
//  }
 
  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);
    }
}

    


Discussions

0
None
Nighter3D

Best Answer 3 years ago

Im not sure what it is that you want figured out. i see something about a motor and a switch, but no exact question at what is wrong.

Well. let me start with how to use the Motor. Thing is that you CAN'T run a motor straight from the arduino. The arduino can only give a small amount of energy that at most can power a led or 2 or a tiny piezo speaker. If you want to be able to control a motor you will need to use something that can can handle that kind of power. Most common solution is to use a N-channel transistor or Mosfet to do that (see Example below). Once you got the driver for it all you have to do assign a pin and send a DigitalWrite to it

As for switching the PIR and such off. a simple 'if' can do that. make the entire code below the button code only activate if on==true and you are done.

Hope that answers your question(s). let me know if you need any more help.

New Bitmap Image (4).bmp
0
None
TianL4Nighter3D

Answer 3 years ago

Thanks for that, answered everything :)

0
None
Nighter3DTianL4

Answer 3 years ago

Glad to be of help. don't forget to mark your question as answered!