Dave's Brain

Browse - programming tips - arduino input motion detector

Date: 2009sep11

Q.  How do use a motion detector as input for an Arduino ?

A.  I used this PIR Motion Sensor
http://creatroninc.com/product.php?ProductID=289

With these connections:

+5V ------------------+---------------------------------- RED   
                      |                                             PIR
                      |                      GND -------- BROWN 
                     10K Resistor
                      |	
                      |
DIGITAL INPUT --------+---------------------------------- BLACK
(eg 5)

Sketch:

const int motionIn =  5;      
const int ledOut = 13; // Builtin
unsigned long lastMove = 0L;

void setup() {
  pinMode(motionIn, INPUT);
  pinMode(ledOut, OUTPUT); 
}

void loop()
{
  int    out;
  
  if (digitalRead(motionIn) == LOW)
  {
      lastMove = millis();
  }

  if ((millis() - lastMove) < 5000L && lastMove != 0)
  {
    out = HIGH;
  }
  else
  {
    out = LOW;
  }
  
  digitalWrite(ledOut, out);
}
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.