Pages: [1]   Go Down
Author Topic: Problem getting PIR sensor working using interrupts  (Read 1355 times)
0 Members and 1 Guest are viewing this topic.
Karlsruhe, Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I am trying to get this PIR sensor (https://www.sparkfun.com/products/8630) working in my arduino using interrupts. I have it hooked up as described in this page (http://bildr.org/2011/06/pir_arduino/) and the example that comes in that page works perfectly. However I would like to execute some code when the PIR detects motion and polling the value of the sensor in my the loop is not the best option for me since I want to have delays (few seconds) in the loop and this will cause that not all the motions detected by the sensors are handled by the arduino. So it seems that using interrupts is the way to go and I tried but I got some results I was not expecting.

The call back for the interrupt is executed all the time, so this means that the sensor is going from HIGH to LOW constantly but if this would be true, the example should also not work.

This is my sketch:
Quote
volatile boolean motionDetected;
void setup() {
  Serial.begin(9600);
  Serial.print("calibrating sensor ");
  for(int i = 0; i < 20 ; i++){
    Serial.print(".");
    delay(1000);
  }
  Serial.println(" done");
  Serial.println("SENSOR ACTIVE");

  attachInterrupt(0, motionDetectedIntrrumpt, FALLING);
}

void motionDetectedIntrrumpt(){
  motionDetected = true;
  Serial.println("*");
}

void loop() {
  if(motionDetected){
    Serial.println("Motion detected handled.");
    motionDetected = false;
  }
  delay(5000);
}


is it a debouncing problem?
I read in the comments of the product in the sparkfun page, that some people are having problems when using the 5v of the arduino and that the sensor should use 9v power supply could this be my problem as well?
« Last Edit: August 12, 2012, 12:29:53 pm by hectorzeta » Logged

Montreal
Offline Offline
Edison Member
*
Karma: 23
Posts: 2487
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have a multimeter to measure a voltage at pin 2?
As alternative, connect one of analog pins in parallel to pin 2 and print results in a loop, or you can upload this simple scketch and print oscillogram in serial monitor window: http://coolarduino.wordpress.com/2011/03/14/quasi-real-time-oscilloscope/
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4764
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void motionDetectedIntrrumpt(){
  motionDetected = true;
  Serial.println("*");
}

You should never use the serial interface within an interrupt handler.

Quote
since I want to have delays

Try to avoid delays, use the millis() call to time your stuff.
Logged

Karlsruhe, Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



Magician, thanks for the sketch it is quite useful. Using it I noticed that in fact the sensor was going many times from 1 to 0.  So I decided to play a bit more with the sensor before trying the interrupts. I uploaded to the arduino this simple sketch:

Quote
void setup() {
  Serial.begin(9600);
  Serial.print("calibrating sensor ");
  for(int i = 0; i < 5 ; i++){
    Serial.print(".");
    delay(1000);
  }
  Serial.println(" done");
  Serial.println("SENSOR ACTIVE");
  pinMode(3, INPUT);
}

void loop() {
  count++;
  if(count%80 == 0)
  {
    Serial.println();
    count = 0;
  }
  Serial.print(digitalRead(3));
 delay(200);
}



I powered up the arduino using the USB cable and I got this output when I wave my hand infront of it:

calibrating sensor ..... done
SENSOR ACTIVE
1111111111111111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111110000000000001111000100100110000010110110001000101111
00010010111100001000111100110011011000000000111100000000111000000011110010010100
11000000001011000001001110001001101110001101011110000010011100000000101110000001
01110001001001100000010011100101010111100000100101100100000101100100000101000000
10111110000010011110000000101110000000011110000000111110000000111110000010111110
00001011110000001101100000000010110000010111110000000011110000010011100000001001
11000000101110000000000111000001011110000000010110010100011110000010011100000000
01110010010101110000010110110000010011100000000101100010010011100000010011100000
00001010000000010110001010011110011000111100100000011100000000101100001011011000
00001101100101000111100000001111001010100111000000110111000100101111000000001111
10000100111100000001001100000010111000000010011100010010111110000000000000011100
00000110000001101111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111111111111111


it took about 15 seconds (sometimes even 30sec) to start printing only 1s. I repeated this for few times and sometimes the sensor did not stabilize at all.

 
My next step was to connect the V+ of the PIR to the arduino Vin instead of the 5V and I was surprised with the results:

calibrating sensor ..... done
SENSOR ACTIVE
1111111111111111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111111101111111
11111111111110011111111111111011111111111100111111100111111111111111111111111111


It only printed the 0s when I passed my hand infront of the PIR. Now I tried the interrupt code and it worked perfectly getting this output:

calibrating sensor ..... done
SENSOR ACTIVE
11111111111111111111111111111111111111111111111110Motion detected handled.011111111
1111111111111111110Motion detected handled.0111111111111111111111111111111111111111
1111111111110Motion detected handled.0111111111111111111111111111111111111111111111


My question now is, why does it work when I take the power from Vin and not when I take it from 5V?
I also try powering up the arduino using a 9V battery but the results were completely different, the sensor almost never stabilized.

pylon, you are right I should have not used the serial interface in the interrupt. But it seemed so straight forward to use it for debugging purposes.
Logged

Montreal
Offline Offline
Edison Member
*
Karma: 23
Posts: 2487
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, I was thinking that using analogRead you would get more information, than using digitalRead. Digital only tells you "1" when voltage above 2.7V and "0" when it below 2.2V. Oscilloscope would provide much more details on the nature of interference.   And especially when there is power line issue, you can simply hook up resistive voltage divider and check how clean your power line is. Of course, you could simply put decoupling capacitor across + and - in blind, but how you can be sure? Where is this 5V coming from?
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4764
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The datasheet clearly says it's operated at 12V. Maybe SparkFun had success with some of the units operating at 5V but the unit may use too much current for an Arduino to supply. What voltage did you supply to the Arduino when you used the VIN to drive the PIR? If it was 12V you exactly gave the PIR the voltage the datasheet specifies.
Logged

Karlsruhe, Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was thinking that using analogRead you would get more information, than using digitalRead. Digital only tells you "1" when voltage above 2.7V and "0" when it below 2.2V.
First I was using analogRead and I was getting values around 20 and 1020 after trying this I decided to use the digitalRead just to have a simpler output.

Where is this 5V coming from?
The 5V came from the arduino 5V pin.

What voltage did you supply to the arduino when you used the VIN to drive the PIR?

I had the arduino plugged to the USB of my Mac. And in the tests I did the only difference was that the V+ of the sensor was either connected to the arduino 5V pin or the arduino Vin pin.

I re-read the arduino power specs http://arduino.cc/en/Main/ArduinoBoardUno/:

Quote
The board can operate on an external supply of 6 to 20 volts. If supplied with less than 7V, however, the 5V pin may supply less than five volts and the board may be unstable. If using more than 12V, the voltage regulator may overheat and damage the board. The recommended range is 7 to 12 volts.

The power pins are as follows:

    VIN. The input voltage to the Arduino board when it's using an external power source (as opposed to 5 volts from the USB connection or other regulated power source). You can supply voltage through this pin, or, if supplying voltage via the power jack, access it through this pin.

    5V.This pin outputs a regulated 5V from the regulator on the board. The board can be supplied with power either from the DC power jack (7 - 12V), the USB connector (5V), or the VIN pin of the board (7-12V). Supplying voltage via the 5V or 3.3V pins bypasses the regulator, and can damage your board. We don't advise it.

and I realized that the main difference between VIN and 5V pins is that 5V comes from the regulator and VIN connects directly to the power supply. So I need to get a multimeter and check the values that the PIR is getting in both cases. But in the case that in both cases it gets 5V is there any difference if the 5volts come from the regulator or not?

I read the forum in sparkfun about this product and it seems that more people is having similar problems. The sensor has its own regulator and it requires more than 5V to work properly. Some people suggest to pass the sensor internal regulator and with this the sensor can work even with 3.3V. So it seems that the sensor is not as straight forward to use as I initially thought. I am planning to run my project using USB and I am not too happy with the idea of getting power for the sensor using the VIN, also using a different power supply for the sensor is not an option. If I would understand what is the main difference of the power coming from VIN and 5V pins when the arduino is powered using USB I would be happier.

Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4764
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

VIN has to be always at least 2V higher than 5V because the voltage regulator needs that gap to work correctly. So you have at least 7V at VIN, in most cases it's 8V5 or more. If your power supply gives 12V you have about 11.5V (the diode for reverse voltage protection has that 0.5V drop) on VIN which is more or less what the datasheet specifies.
Logged

Karlsruhe, Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That may explain a lot. But I was powering up the arduino using USB, isn't it that the USB gives maximum 5V?
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4764
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But then you don't have power on the VIN pin, do you? What kind of Arduino do you use?
Logged

Karlsruhe, Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


I have power in VIN when using the USB. I mean all the test I did were using only the USB to power the arduino no other power supplies were used. I am using an Arduino Uno r3 (and it has attached an Ethernet Shield).

I definitely need to get an multimeter to understand better what is going on here.
Logged

Karlsruhe, Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I definitely need to get an multimeter to understand better what is going on here.

Finally I got a multimeter and checked the values I am getting in pins 5V and VIN. For me surprise 5V and VIN give me pretty much the same results (5V gives me 4.7 and VIN gives me 4.2). However when the PIR is powered by the 5V the output is not good, it takes too long time to stabilize (if it stabilizes); but when it is powered using VIN the output is really clean. Any ideas why?

Note: Arduino was powered up using USB.
Logged

Pages: [1]   Go Up
Jump to: