I'm sure someone has used the SparkFun motion sensor with arduino before (http://www.trossenrobotics.com/sparkfun-pir-motion-sensor.aspx) but I'm having some difficulty. I understand the wiring and am pretty sure I have it correct. The problem I'm having is that every time it is tripped it never resets.
Something like this (adapted from various examples I've found) :
int timer = 1000;
int alarmPin = 0;
int alarmValue = 0;
int ledPin = 13;
void setup () {
Serial.begin (9600);
pinMode(ledPin, OUTPUT);
pinMode(alarmPin, INPUT);
delay (2000);
}
void loop (){
delay(500);
alarmValue = analogRead(alarmPin);
if (alarmValue < 100){
digitalWrite(ledPin,HIGH);
delay(2000);
digitalWrite(ledPin,LOW);
}
delay(200);
Serial.println (alarmValue);
}
.. This is the really strange thing --- closing the serial monitor seems to reset the sensor. If I leave the serial mon open the pin always reads 20 after it is tripped but if I close it and re-open it (just the serial monitor), it starts reading 1023 when the sensor settles.
I have it hooked to an arduino with a 10k resistor on the alarm pin. Like I said - it works great to detect the first motion but I can't seem to get the thing to reset!
I have four of these running - I suspect you have your resistor in series (from the alarm pin on the sensor to the analog port rather than being used as a pull-up resistor from the alarm pin to +5V.)
The data sheet shows:
The alarm pin is an open collector meaning you will need a pull up resistor on the alarm pin. The open drain setup allows multiple motion sensors to be connected on a single input pin. If any of the motion sensors go off, the input pin will be pulled low.
Try this code (remove the resistor on your alarm pin, and connect the alarm pin to digital 2...):
int timer = 1000;
int alarmPin = 2;
int alarmValue = 0;
int ledPin = 13;
void setup () {
Serial.begin (9600);
pinMode(ledPin, OUTPUT);
pinMode(alarmPin, INPUT);
// turn on internal pull-up resistor
digitalWrite(alarmPin, HIGH);
delay (2000);
}
void loop (){
delay(500);
alarmValue = digitalRead(alarmPin);
if (alarmValue == LOW){
digitalWrite(ledPin,HIGH);
delay(2000);
digitalWrite(ledPin,LOW);
}
delay(200);
Serial.println (alarmValue);
}
I had the resistor between the 5v and the alarm pin.
I rigged it the way you suggested and used your code and get the same end result. It reads 1 until I trip the sensor, then it reads 0 forever unless I close and re-start the serial monitor.. Crazy, eh?
I have just the black wire plugged into the digital 2, the red plugged into +5V and the brown into GND.
After the sensor initializes (50 or so ms) it shows 1 repeatedly on the console, and the LED on 13 is out.
When I put my hand over the front of the lens, it goes 1, and LED on 13 glows. [EDIT] when I remove my hand, it floats for a couple more samples at 0 and then goes back high (to 1)[/EDIT]
Can you verify yours is the same? If not, you may have a bad sensor...
All,
Well I am stuck with another problem(?). I have a power supply 6V connected to the same motion sensor. No arduino board or anything except an LED connected to the alarm pin and then grounded.
The problem is that the LED blinks always! irresptice of the motion.!
Interestingly , it works perfectly fine when powered from the USB port of the computer!! .
Initially I suspected that my power supply is not giving enough voltage.So I checked with the multimeter.It showed me 7.9 V which is enough for this sensor( i believe). The powersupply that I have is 12V variable power supply which can produce 500mA. Could you please suggest me what could be the problem?
you'll need a resistor (10k should work), between the alarm pin and 5v.
Mitch is right, the alarm pin is an open collector that is essentially connected to GND when motion is detected. Otherwise, it floats. The LED will light when motion isn't detected, not when it is detected as I suspect you want.
Where are you putting the alarm pin in your USB setup?
Mitch: I meant , I powered the Arduino board from the USB rather than from an external power supply. This was actually two experiments. Let me explain
1st Experiment: Sensor alone
I connected the motion sensor to the external powersupply and found that it is bliking always(irrespective of the motion) - except in the night times.
2nd Experiment.
This has two parts:
Part A: I Connected the Sensor to the Arduino board (connected the alarm pin to the digital pin 3) and powered the Arduino from the external power supply. I make use of the in built LED connected to pin 13 . I could see that the LED 13 is bliking always.
Part B: The same setup, I just removed the external power supply and connected the board to the computer USB port. And it started functioning properly. Meaning the LED 13 is blinking only when there is a motion.
Please note: If I connect my Arduino board to the external power supply in the night times(in night times the voltage will be higher than the day time ,because there is less usage by the industries around) it works perfectly fine.
As I mentioned earlier, increasing the voltage in the power supply did not fix the issue.
Where are you putting the alarm pin in your USB setup?
Mitch: I meant , I powered the Arduino board from the USB rather than from an external power supply. This was actually two experiments. Let me explain
1st Experiment: Sensor alone
I connected the motion sensor to the external powersupply and found that it is bliking always(irrespective of the motion) - except in the night times.
2nd Experiment.
This has two parts:
Part A: I Connected the Sensor to the Arduino board (connected the alarm pin to the digital pin 3) and powered the Arduino from the external power supply. I make use of the in built LED connected to pin 13 . I could see that the LED 13 is bliking always.
Part B: The same setup, I just removed the external power supply and connected the board to the computer USB port. And it started functioning properly. Meaning the LED 13 is blinking only when there is a motion.
Please note: If I connect my Arduino board to the external power supply in the night times(in night times the voltage will be higher than the day time ,because there is less usage by the industries around) it works perfectly fine.
As I mentioned earlier, increasing the voltage in the power supply did not fix the issue.
Spin : Your code works fine so I think my sensor and board is OK (now).
The biggest problem I have is that once it is tripped it takes a long time (and a lot of iterations of loop()) before the sensor settles.
I have it rigged up to transmit an "alarm" signal through a rf transmitter but since the pin reads LOW for so long it is taking a minute or so to reset.
Introducing delay() calls in loop() only makes the sensor take longer to settle.
Is there some way to reset the sensor all together? I'm thinking about just resetting the whole board after an alarm is received but I'm not even sure if you can trigger a reset in software..
The biggest problem I have is that once it is tripped it takes a long time (and a lot of iterations of loop()) before the sensor settles
The pull-up resistor set in setup shouldn't allow it to float. My sensors all seem to go almost instantly from low to high and back to low with movement.
However, I noticed that sometimes if I power the sensor and then move the sensor itself, it has a hard time figuring out if I am moving or not and sometimes does what you are experiencing... However, I can't get it to duplicate it at the moment (of course...)
Make sure that the sensor is stationary and allow 100-500ms of you not being around so that the sensor gets a baseline measurement of the area it is to sense. The data sheet shows 50ms, but I like to be safe....
I put the sensor pointing away from the arduino in my test, power it, wait and then walk in front of it. If the sensor is pointing up or at me when I power it up, that's when it appears to go silly for a bit.