PIR and Counting People

halo guys.
at this time I'm learning arduino and PIR sensor.
and it will used to count people like the Thread Starter project.

but i have the litle problem.
my sensor always counting 3 time for 1 motion. sometime it counting 2 time, sometime 1.
and what I want is, PIR counting 1 for 1 motion.

Im sorry for my weird english, because english is not my primary language :blush:
I hope you guys can understand.

this is the code

#include <LiquidCrystal.h>

int ledPin = 13;
int pirPin = 7;
int statusPir = LOW;
int readPir = 0;
int counting = 0;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(pirPin, INPUT);
  lcd.begin(16, 2);
}
void loop() {
  readPir = digitalRead(pirPin);
  if (readPir == HIGH) {
    digitalWrite(ledPin, HIGH);
    if (statusPir == LOW) {
      counting = counting + 1;
      lcd.setCursor(0, 0);
      lcd.print("Intruder ");
      lcd.print(counting);
      lcd.println(" Aliens");

      statusPir = HIGH;
    }
  } else {
    digitalWrite(ledPin, LOW);
    if (statusPir == HIGH) {
      // OFF
      statusPir = LOW;
      lcd.setCursor(0, 1);
      lcd.println("NO INTRUDER");
      lcd.clear();
    }
  }
}

// okay lets learning TONE :D

That code looks ok to me, as it should seeing as it's straight from here with no attribution. Looks like it should only increment on a new high.

If I were you I'd put some Serial.print() lines in strategic places so you can see where the logic is taking you each time through loop().

ps: I suggested to mods that they split your question out into a new thread

JimboZA:
That code looks ok to me, as it should seeing as it's straight from here with no attribution. Looks like it should only increment on a new high.

If I were you I'd put some Serial.print() lines in strategic places so you can see where the logic is taking you each time through loop().

yes Im learn from adafruit.
thanks, I'll try and post the results.

Right, I used the code below and it works perfectly. It never increments the counter until the pir has been high and undisturbed long enough foe it to reset. I waved my hand in front of the sensor like crazy after the pir had gone high and count had incremented: only counts again after a low, as expected.

It's your code with the lcd stuff removed and some Serial.prints added.

//Thread 309454
//Based on adafruit pir tutorial

int ledPin = 13;
int pirPin = 7;
int statusPir = LOW;
int readPir = 0;
int counting = 0;


void setup() {

  Serial.begin(9600);
  Serial.println("Starting....");
   Serial.println("");
  pinMode(ledPin, OUTPUT);
  pinMode(pirPin, INPUT);
}
void loop() {
  readPir = digitalRead(pirPin);

  if (readPir == HIGH) {
    digitalWrite(ledPin, HIGH);
    //Serial.println("pir reads high");
    if (statusPir == LOW) {
      counting = counting + 1;
      Serial.print("pir reads high, was low, counting to ");
      Serial.println(counting);


      statusPir = HIGH;
      Serial.println("pir status set high, no increment before goes low");
    }
  } 
  else {
    digitalWrite(ledPin, LOW);
    if (statusPir == HIGH) {
      Serial.println("pir reads low, was high");
      Serial.println("pir status set low, can increment now");
      statusPir = LOW;

    }
  }

  }
Starting....

pir reads high, was low, counting to 1
pir status set high, no increment before goes low
pir reads low, was high
pir status set low, can increment now
pir reads high, was low, counting to 2
pir status set high, no increment before goes low

A "delay" can also be set with the "time" adjustment pot on the sensor.
That is, if it's a common cheap PIR sensor with two trimpots.
With the "time" pot fully anticlockwise, the out pin goes low again very quickly after movement has been detected.
But if you turn it slightly clockwise, "hold" time increases.
Fully clockwise is about seven minutes "on".
If it's e.g. a hallway, an IR gate might be better at counting many people in a short time.
Some IR door sensors for shops have customer counters.
Leo..

JimboZA:
Right, I used the code below and it works perfectly. It never increments the counter until the pir has been high and undisturbed long enough foe it to reset. I waved my hand in front of the sensor like crazy after the pir had gone high and count had incremented: only counts again after a low, as expected.

It's your code with the lcd stuff removed and some Serial.prints added.

//Thread 309454

//Based on adafruit pir tutorial

int ledPin = 13;
int pirPin = 7;
int statusPir = LOW;
int readPir = 0;
int counting = 0;

void setup() {

Serial.begin(9600);
  Serial.println("Starting....");
  Serial.println("");
  pinMode(ledPin, OUTPUT);
  pinMode(pirPin, INPUT);
}
void loop() {
  readPir = digitalRead(pirPin);

if (readPir == HIGH) {
    digitalWrite(ledPin, HIGH);
    //Serial.println("pir reads high");
    if (statusPir == LOW) {
      counting = counting + 1;
      Serial.print("pir reads high, was low, counting to ");
      Serial.println(counting);

statusPir = HIGH;
      Serial.println("pir status set high, no increment before goes low");
    }
  }
  else {
    digitalWrite(ledPin, LOW);
    if (statusPir == HIGH) {
      Serial.println("pir reads low, was high");
      Serial.println("pir status set low, can increment now");
      statusPir = LOW;

}
  }

}






Starting....

pir reads high, was low, counting to 1
pir status set high, no increment before goes low
pir reads low, was high
pir status set low, can increment now
pir reads high, was low, counting to 2
pir status set high, no increment before goes low

sorry im late to relpy.
i must to finish my work first.

thanks for your relpy and thats code, i can understand how the logic work.

but my pir still counting 2 when i waving my hand.
maybe i should buy new pir.

Wawa:
A "delay" can also be set with the "time" adjustment pot on the sensor.
That is, if it's a common cheap PIR sensor with two trimpots.
With the "time" pot fully anticlockwise, the out pin goes low again very quickly after movement has been detected.
But if you turn it slightly clockwise, "hold" time increases.
Fully clockwise is about seven minutes "on".
If it's e.g. a hallway, an IR gate might be better at counting many people in a short time.
Some IR door sensors for shops have customer counters.
Leo..

thanks for your advice.
yes i was set the delay, but it wasnt work :frowning:
and yes i bought the PIR for $3 :smiley: (made in china)