Problems with a moving PIR sensor?

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Problems with a moving PIR sensor?

Postby zinc55 » Tue Jan 24, 2012 8:52 pm

Hi, I'm using the PIR motion sensor (from Adafruit) on top of a moving piece of rubberish Styrofoam. I'm trying to make the motor stop for a few seconds and play a noise if it detects movement. I tested it with the following code with the PIR stationary and it worked fine. When it is mounted on the rig, however, it only detects movement when the servo is in the process of changing direction. Is there any way to tweak the sensitivity or re-triggering to make it detect other movement?


Code: Select all
// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.


#include <Servo.h>

Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position
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


void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
}


void loop()
{
  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(30);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 90; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                               
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(30);                       // waits 15ms for the servo to reach the position
  }
val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    delay(5000);  // pause motor
    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
    if (pirState == HIGH){
      // we have just turned of
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }
}

zinc55
 
Posts: 30
Joined: Thu Jan 19, 2012 5:52 pm

Re: Problems with a moving PIR sensor?

Postby philba » Tue Jan 24, 2012 9:40 pm

I have doubts that PIR is the right sensor for your application. Maybe an ultrasonic ranger but that's got to be a challenge too.
philba
 
Posts: 387
Joined: Mon Dec 19, 2011 5:59 pm

Re: Problems with a moving PIR sensor?

Postby Stephanie » Tue Jan 24, 2012 9:40 pm

Based on my understanding of how PIR motion detectors work, a moving sensor is going to constantly detect its own motion and would not be useful for detecting external motion.

If you are moving it back and forth in order to 'sweep' a larger radius than a stationary PIR could monitor, then what you really need to do is move it, then stop, wait for it to stabilize, then 'watch' for movement for some period of time, then move it to its next position, stop, wait for it to stabilize, then 'watch' for movement for some period of time... and so on.

It cannot detect movement when it is being moved itself.

Ladyada's PIR tutorial explains how it works, which I think also makes it clear why it won't work if the sensor is being moved:
http://www.ladyada.net/learn/sensors/pir.html

Cheers!
User avatar
Stephanie
 
Posts: 286
Joined: Sat Dec 11, 2010 12:17 am
Location: Canada

Re: Problems with a moving PIR sensor?

Postby zinc55 » Tue Jan 24, 2012 9:48 pm

Thanks! I read the tutorial before but I guess I didn't catch it the first time! I think I can add it on top of the device on its own platform (so it won't spin) and that should hopefully solve it. :o
zinc55
 
Posts: 30
Joined: Thu Jan 19, 2012 5:52 pm


Return to General Project help

Who is online

Users browsing this forum: adafruit_support_mike, psbot [Picsearch] and 10 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [103]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
Android[6]
 
BeagleBone[24]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[33]
LCDs & Displays[48]
Components & Parts[69]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[109]
 
Wireless[14]
Cables[61]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]