PIR Motion Sensor gives wrong Outputs

It's very simple to understand. I have a PIR motion sensor : Grove - PIR Motion Sensor - Seeed Wiki
I just copy pasted the code : `#define PIR_MOTION_SENSOR 2//Use pin 2 to receive the signal from the module

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

}

void loop()
{
if(digitalRead(PIR_MOTION_SENSOR))//if it detects the moving people?
Serial.println("Hi,people is coming");
else
Serial.println("Watching");

delay(200);
}`

For some reasons it outputs this no matter what I do like cover it entirely or living my room :


I have the exact same output no matter what I do, it's always the same cycle

How do I make it work ?

The PIR may be 'open collector, low active' in which case use "INPUT_PULLUP" in your setup, and expect that the signal is low when it senses people.
IN other words, swap your two print statements.
C

That presumes, of course, that you have it wired correctly. Can't tell, no schematic.

I tried and it prints exactly the same thing

I plugged in the grove on D2 to the sensor and then the Arduino to my pc that's all. Sorry for no schematics but I'm quite new to this and I don't really know how to make one.

Pen & paper, take picture, insert in post.

I don't know what is in the library, but this line looks suspicious to me.
if(digitalRead(PIR_MOTION_SENSOR))
if it what? I don't have one yet but will in a week or so when it arrives, but I suspect it should be something like:

digitalRead(PIR_MOTION_SENSOR);
if (PIR_MOTION_SENSOR  == 1 )
Serial.println("Hi,people is coming");

I'm sure someone will correct me, and please do, but it looks like an incomplete IF to me.
Joe

Hi!

I used once a PIR sensor in a prototype and it was such a disappointment! from an electronic point of view, a bell attached to a string will do better job.

Your PIR has two phases:

  • at start-up, it collect ambient light (rather: IR background) to determine its zero level
  • when the ambient level of IR goes up the internal threshold (not settable in my configuration), the module will sent a 2 sec pulse (what you see into your monitor), whaterver it detects, from size, time of stimulation, and will stop after 2sec even if it is still stimulatiing.
    The Fresnel lenght participates to this mess, as it allows the module to detect any variation oh heat in a large area around it.
    Very cool if you need a very large detection (hallway, parking....) but really bad if you want narrow detection.

Seeing the behavior of your module (pause at rest for less than 1 sec, then activation for 2sec, then repeat) I have 2 hypothesis:

  • an electric issue in the module that make it difficult to get aquire a zero background between 2 measures (so the device feels it is always time to activate)
  • a source of heat in the room you tested it (but it will explain it well if the source is not constant).

I gave up the module in the junk part box and will not use it anymore.

Joe
digitalRead() reads the state of a pin, in this case whatever PIR_MOTION_SENSOR is defined as. It will return a 1 or a 0. The if statement expects a 1 or a 0. So there is no issue with this statement.

Ok. So it expects a 1 or a 0. Then what? Where is the decision on what a 1 or a 0 means? Is it considering a 1 as true and a 0 as false? That makes sense. But expecting either state without saying what to do if one or the other is received doesn’t. Not intending to argue here so please don’t take it that way. I just have not seen this explained well I guess. It feels like calling a coin flip in the air without determining what heads and tails means first.

I don’t mean to pull this away from the OP, but I am thinking what I said about 1 being seen as true and 0 as false may be right. I just can’t find a single example saying or showing that in use.

Hmm. From the Arduino Language Reference,
here

if

[Control Structure]

Description

The if statement checks for a condition and executes the following statement or set of statements if the condition is 'true'.

Syntax

if (condition) {
  //statement(s)
}

So I think it's pretty obvious. Don't look too hard for the trees, or the forest might bite you!

As to whether a 1 or a 0 is true or false, well, I leave that to those who like to make it 'mysterious' and 'difficult' to comprehend a programming language. It's always been that way in my (simple, admittedly) world.

First when you post code please post it formatted and using code tags. This is your code:

#define PIR_MOTION_SENSOR 2  //Use pin 2 to receive the signal from the module

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

void loop() {
  if (digitalRead(PIR_MOTION_SENSOR))  //if it detects the moving people?
    Serial.println("Hi,people is coming");
  else
    Serial.println("Watching");

  delay(200);
}

There is nothing wrong in your code. The code works just fine. So here is what I suggest. Disconnect your sensor from your Arduino. Now with your Arduino Pin 2 connect a jumper wire to pin 2. Next toggle that jumper wire between 5 volts and ground. You can use the Arduino 5V out and Arduino Ground. What do you see? With pin 2 at ground you should see "Watching" with pin 2 at 5 volts you should see "Hi, people is coming" . That's it. If you do not see what I mentioned then you have a sensor fault. This assumes the sensor is normally low and goes high when motion is detected.

Ron

If you know the language and how it works it’s easy. For those of us learning it, me anyway, that does not explicitly say enough to infer that as the case. Now that I know, sure it means that. Thanks for the help on this. Very much appreciated and will be utilized.

Don't worry about it's just a ;earning curve. I spent over a 40 year career in electrical engineering and thankfully never had to do much programming. Been retired almost 10 years and now I just play around with stuff like this. If I had to depend on my programming skills to put beanies and weenies on the table I would have starved to death years ago. :slight_smile:

Ron

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.