pir sensor problem

hi, im new to arduino and im working on a really cool project. i have a PIR sensor and i set it up to send a serial command to gobetwino every time someone walks in front of it. there are a few problems though.
My first problem is that the sensor activates even if there is no one in front of it.
My second problem is that when it does activate, it sends the serial command thousands of time per second instead of one time when a person is in front of it.

Here is my code:

int inputPin = 9;
int ledPin = 13;
int val = 0;

void setup() {
digitalWrite(inputPin, HIGH);  
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
           
   
void loop() {
 val = digitalRead(inputPin); 
 if (val == LOW) {          
   digitalWrite(ledPin, HIGH);  
   Serial.println("#S|EMAILME|[]#");
 } else {
   digitalWrite(ledPin, LOW); 
 }
 }

Thanks for any help you can give me :slight_smile:

My first problem is that the sensor activates even if there is no one in front of it.

That sounds like a hardware problem, in which case your software is irrelevant.
You need to post the schematic of what you have an tell us what actual sensor you have so we can look up the data sheet.

My second problem is that when it does activate, it sends the serial command thousands of time per second instead of one time when a person is in front of it.

Yes that is what you have asked to code to do, as long as the input is low it will carry on printing out the message. You want to see the current state and compare it to the last time you looked at it and only print out when they are different.

can you explain the second one to me or show me how to do it?

See this site:-
http://www.kasperkamperman.com/blog/arduino/arduino-programming-state-change/

ok and for my first problem, the sensor is a passive infrared motion sensor from parallax. can be found http://www.parallax.com/Store/Microcontrollers/BASICStampModules/tabid/134/txtSearch/555-28027/List/1/ProductID/83/Default.aspx?SortField=ProductName,ProductName

Not so easy as there is no real data sheet with that link so it is hard to say what is in it.

It could have false triggering due to electrical noise in which case a bit of extra supply decoupling might not go amiss.
http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html
Are the leads to the arduino short? They should be.

The other thing is that it could be over sensitive, in which case you could cut down the IR getting to it with a translucent piece of plastic like the "lens" is made of.

could my computer be generating electrical noise?

Yes but so could anything else, electrical noise is all around us and virtually every device that uses electricity emits some to a greater or lesser extent.

what i dont get is that it is set to make the led go on when it detects motion, and it says it detects motion literally every 5 seconds in a pattern for some reason. i wonder why.

Sounds like noise.

@awsomnick

passive infrared motion sensor from parallax

There are some details here: http://www.parallax.com/dl/docs/prod/audiovis/PIRSensor-V1.1.pdf

There is some Arduino software here: http://www.arduino.cc/playground/Code/PIRsense. (I have not tested the code, but I would suggest that you give it a try.)

I have used similar sensors and I will tell you that they are very (and I mean it: very) sensitive to electrical noise. In addition to heavy hardware decoupling, I used a software technique to filter out small glitchy pulses that looked like motion when there wasn't any motion.

Regards,

Dave

@davekw7x

I have not tested the code

As a followup to my incomplete post:

I managed to "borrow" a Parallax PIR sensor from a colleague who had obtained one to use with his Basic Stamp, but lost interest in the whole Basic thing. I hope it wasn't too late. See Footnote.

I experimented a little with a 'scope connected to the sensor's output pin (supplied with +5 volts and not connected to a microprocessor).

There is a little three-pin header. One end pin is marked "H" and the other is marked "L". A jumper is supplied. It was in the "H" position. I don't know whether this is how it came from the vendor or whether my friend put it in that position.

I did a quick wave of the hand. The output signal went high for a couple of seconds and then went low. I repeated the experiment a number of times and the high time varied from something like two seconds to something like four seconds..

Then I waved my hand continuously and found that the output signal went high and stayed high until I got tired. When I stopped waving, the signal went low (after a couple of seconds), then went high for a couple of seconds and finally stayed low.

I moved the jumper to the "L" position and did my wave stuff again.

A quick wave resulted in a high output for a couple of seconds and then low again.

Continuous waving resulted in a repeated sequence of high for a couple of seconds and low for about a second, high for a couple of seconds and low for about a second ....

I tried the playground sketch and it showed results consistent with the 'scope measurements.

I will mention that the 'scope did capture an occasional bounce of a few milliseconds or so when the output changed state, but since meaningful use requires state changes to be reported on a time frame of several seconds, it shouldn't affect any real application. I did not see any spurious (noisy) state changes at all.

The setup was an Arduino Duemilanove with the PIR sensor on a solderless breadboard, connected with jumper wires. I did not use any additional hardware decoupling.

Bottom line(s) for the original question:

1. The sensor output indicates motion when it is HIGH, not LOW.

2. Run the playground sketch and observe how it reports newly detected motion. Make absolutely sure that you understand what you are seeing and how it relates to the code.

3. For your application, use some kind of state timing mechanism (like that in the playground sketch) to send the e-mail message when motion is newly detected. Maybe you can just use the playground sketch itself with minor modifications regarding the content of the motion report message.

Regards,

Dave

Footnote:
"It is practically impossible to teach good programming to students
that have had a prior exposure to BASIC: as potential programmers
they are mentally mutilated beyond hope of regeneration."
--Edsger Dijkstra