Problems with SE-10 PIR Motion Sensor

Hey guys.
I'm developing a project that concerns the detection of Human presence inside a room.
For this I am using an Arduino with a SE-10 PIR Motion Sensor http://www.sparkfun.com/products/8630.
Although the easy way to put all the stuff working,the results obtained are not the expected.
For example this morning I made a test, where I put the PIR sensor inside a box, to not detect any kind of motion.
Unexpectedly the results were the following:
In 1275 iterations, it had detected motion for 140 times, what represents in the end an error % of 10%!!!

To let you know I am using an open collector scheme with a pullup resistor of 10kohms (http://bildr.org/2011/06/pir_arduino/), that sends the values to an arduino (supplies the sensor with 5V).

Because I am thinking to use both sensors in my master thesis project, this problem is really worrying.

Thus can somebody help me?

have you accounted for the 1-2 seconds it takes the pir to "warm up" in your code? (like adding a delay in void setup?) also, in my experience with a PIR (mine is a different model, but perhaps this is still your problem), the pin is pulled low for several seconds, so if your code loops several times in a second, the int that is are counting the times the alarm was tripped could be going up more than you intend it to (perhaps the pir was only tripped once, but your code looped 140 times before the pir pin returned to high).

This is my code (sorry for some names in portuguese :slight_smile: )
I put some lines to know about the error produced...

int calibrationTime = 60;        

int pirPin1 = 2; //digital 2

int LED1 = 4;
int n_detecoes=0;
int n_iteracoes=0;
int per_detecoes=0;

  
void setup()
{
  Serial.begin(9600);
  pinMode(pirPin1,INPUT);

  pinMode(LED1,OUTPUT);

  digitalWrite(LED1,LOW);

  //give the sensor some time to calibrate
  Serial.println("Sensor Calibration in Progress");
  Serial.println("------------------------------");
  for(int i = 0; i < calibrationTime; i++){
    Serial.print(".");
    digitalWrite(LED1, HIGH);
    delay(250);
    digitalWrite(LED1, LOW);
    delay(250);
  }

  Serial.println("");
  Serial.println("Sensor Calibration Completed");
  Serial.println("Sensor Reading Active");
  delay(50);
  
}

void loop()
{
  int pirVal1 = digitalRead(pirPin1);
  
  if(pirVal1 == LOW)
  { 
    //was motion detected
    Serial.print(pirVal1);
    Serial.print(';');
    
    n_detecoes=n_detecoes+1;
    Serial.print(n_detecoes);
    Serial.print(';');
    
    digitalWrite(LED1,HIGH);
    delay(2000);
  }
    else
    {
    Serial.print(pirVal1);
    Serial.print(';');
    Serial.print(n_detecoes);
    Serial.print(';');

    
    digitalWrite(LED1,LOW); 
    delay(2000);
    }
    
    n_iteracoes +=1;
    Serial.print(n_iteracoes); 
    Serial.print(';');
    per_detecoes =(n_detecoes*100)/n_iteracoes;
    Serial.print(per_detecoes);
    Serial.println(';');
}

aha! i am a rather inexperienced coder, but think i have found a problem. if i am not mistaken the first line inside your loop "int pirVal1 = digitalRead(pirPin1);" should be "pirVal1 = digitalRead(pirPin1);" and you should add "int pirVal1 = 0;" to where you have defined the rest of your variables at the top. i'm not sure if this is causing your "big" problem, but I've done the same thing before, and it does mess everything up

please modify your post, select the code and press the # button (looks so much better).

I'm developing a project that concerns the detection of Human presence inside a room.

PIR sensors in the first place detect movement, not presence (although these two are tightly coupled.

Once visited a company who had PIR in meeting rooms and if we just sat on the tables it took 2 minutes before lights went out. (immediately people started waving :slight_smile:

Hey Rob! Your totally right. I made the mistake using the word presence, but I know that this sensor just capt movement... Sorry for the mistake :wink:

About the code Teddy I'm not sure if the position of the definition of int variable ca affect the final result...
I will try on Monday to use your suggestion on my work

Muito Obrigado :smiley:

yeah, i have no idea why it seems to mess it up, but in my experience, EVERYTHING has been messed up when i've done that.

As far as motion sensor go, I have a couple parallax motion sensors, and I've beat myself up trying to find a way to make them accurate let alone precise. If you want to spend the money there are industrial grade motion sensors out there that are very reliable. The question is: Do you want to spend the money, and can you adapt them to arduino?

But you could also use a simple filter in your program like a mode/median filter similar to how people use them for ultra sonic detectors.
Average the readings.

Unhappily I don't have more money to spend on material, so I must work with the stuff I already bought :slight_smile:

Concerning master thesis and arduinos just google it and you will find a lot of thesis that use arduino as microcontroller over the world. Of course this PIR sensor's don't have the best quality, but as far as sparkfun sell them I thought they had the minimum quality acceptable...

At this moment I'm thinking to use something like mode/median filter, the question is: will this filter erase also the true values, when somebody is moving? How the code will know wich values are true and false? Thanks for you help Tesla :wink:

Happily a colleague suggest me to put the sensor in a dark room, instead of put it in a box. Seems that it sensibility isn't good for small spaces...

There's another problem now. Some of my movements in front of the sensor was not detected...

$) $)

Yes, they are not reliable. You could average the readings or you could use two, average those readings, etc.