YourDuinoStarter MotionDetector

From ArduinoInfo
Jump to navigation Jump to search

Motion Detector:


This device has a complex bunch of circuitry on it. Take a close look. This module is used in many "Motion Detector Lights" and similar products, but you can make your own. Here, we'll make an alarm system.

PIR-Jumpers-450.jpgLet's try out your Motion Detector brick. Connect it where the pushbutton was, on I/O Pin 3.
{UPDATE} WAIT for about 2 minutes for it to stabilize. Run the above program while you move a hand or body across the field of view of the detector. You should see the LED and Serial Monitor output show the result from the Motion Detector.

NOTE: The same detector is also available as a plain module with 3 pins (Right photo). In that case use jumper wires to connect the (G)round, (Voltage), (S)ignal.

PIR-Pot-450.jpgHINT: The detector has a small (usually orange) adjustment on its side. This sets how long the alarm is active once it detects motion. For this test, set it fully counter-clockwise. (Photo). Later if you wish it to stay active for a longer time after detecting movement (like you have it turn on the room lights) you can turn the adjustment more clockwise.

Simple Alarm System


If you have that working OK, let's make an alarm system. All we need to add is the buzzer to the LED we already have, and add a little code to our software Sketch.

Connect the buzzer brick with a cable to I/O #10.

Again, save your program if you've made changes, get another blank window and copy and paste the following Sketch:


/* YourDuino Electronic Brick Test 3
 Simple Switch type INPUT DEVICES
 INTRUDER ALARM
 with Serial Monitor Output
 terry@yourduino.com */

/*-----( Declare Constants )-----*/
#define SWITCHPIN      3
#define LEDPIN        11
#define BUZZERPIN     10

/*-----( Declare Variables )-----*/
int  switch_state;  /* Holds the last state of the switch */
int  buzzer_state;  /* Buzzer ON or OFF */

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  pinMode(LEDPIN, OUTPUT);
  pinMode(BUZZERPIN, OUTPUT);
  buzzer_state = false;
  Serial.begin(9600);

}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  Serial.print("ALARM IS - ");
  switch_state = digitalRead(SWITCHPIN);
  if (switch_state == HIGH)
  {
    digitalWrite(LEDPIN, HIGH);
    Serial.println("GOING OFF ! !");

    buzzer_state = ! buzzer_state;
    if (buzzer_state == 1)
    {
      digitalWrite(BUZZERPIN, HIGH);
    }
    else
    {
      digitalWrite(BUZZERPIN, LOW);
    }
  }
  else /* The switch is OFF */
  {
    digitalWrite(LEDPIN, LOW);
    digitalWrite(BUZZERPIN, LOW);
    buzzer_state = false;
    Serial.println("NOT ACTIVE");
  }
  delay(250); /* Wait a bit to see display and listen to buzzer*/
}/* --(end main loop )-- */

/* ( THE END ) */




Upload and run this version. When the alarm is activated, the LED should light and the buzzer should go on and off until the motion detector becomes inactive again.