Author Topic: Arduino PIR  (Read 1202 times)

0 Members and 1 Guest are viewing this topic.

Offline darinsquaredTopic starter

  • Contributor
  • Posts: 39
  • Country: ca
Arduino PIR
« on: December 07, 2019, 08:27:44 pm »
I use the following code for a PIR sensor triggering a LED:
int led = 13;               
int sensor = 2;             
int state = LOW;         
int val = 0;               

void setup() {
  pinMode(led, OUTPUT);      // initalize LED as an output
  pinMode(sensor, INPUT);    // initialize sensor as an input
  Serial.begin(9600);        // initialize serial
}

void loop(){
  val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    delay(100);                // delay 100 milliseconds
   
    if (state == LOW) {
      Serial.println("Motion detected!");
      state = HIGH;       // update variable state to HIGH
    }
  }
  else {
      digitalWrite(led, LOW); // turn LED OFF
      delay(200);             // delay 200 milliseconds
     
      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW
    }
  }
}

Everything works fine when I have Arduino connected through USB to my computer.  When I disconnect from USB and operate from the 9V battery only the LED blinks constantly.  What is my error?
 

Offline igendel

  • Frequent Contributor
  • **
  • Posts: 359
  • Country: il
    • It's Every Bit For Itself (Programming & MCU blog)
Re: Arduino PIR
« Reply #1 on: December 07, 2019, 08:31:27 pm »
What is the power source of the sensor?
Maker projects, tutorials etc. on my Youtube channel: https://www.youtube.com/user/idogendel/
 

Offline darinsquaredTopic starter

  • Contributor
  • Posts: 39
  • Country: ca
Re: Arduino PIR
« Reply #2 on: December 07, 2019, 09:18:32 pm »
What is the power source of the sensor?

PIR connected directly to 5V and ground on Arduino and Arduino board connected to 9V battery
 

Offline darinsquaredTopic starter

  • Contributor
  • Posts: 39
  • Country: ca
Re: Arduino PIR
« Reply #3 on: December 07, 2019, 09:23:41 pm »
more info: when I disconnect the USB so only (9V battery is connected) the LED is off until PIR is triggered. At that point the LED is ON the length of time as set on its POT then is off approximately 2 seconds then ON again repeatedly.
 

Offline igendel

  • Frequent Contributor
  • **
  • Posts: 359
  • Country: il
    • It's Every Bit For Itself (Programming & MCU blog)
Re: Arduino PIR
« Reply #4 on: December 07, 2019, 09:46:38 pm »
more info: when I disconnect the USB so only (9V battery is connected) the LED is off until PIR is triggered. At that point the LED is ON the length of time as set on its POT then is off approximately 2 seconds then ON again repeatedly.

I assume it's one of those HC-SR501 modules? They have a 2-seconds "dead time" window between the end of a one signal and the beginning of the next. Now, assuming the wiring is correct and that the battery is fresh, there should be no difference in behavior. It would seem the PIR sensor still picks something up, this can happen especially near power-up when it's trying to stabilize. Perhaps it was in a different place/angle when it was connected with USB? try putting something over it so that it's blind, check what happens then with USB and with battery.
Maker projects, tutorials etc. on my Youtube channel: https://www.youtube.com/user/idogendel/
 

Offline StillTrying

  • Super Contributor
  • ***
  • Posts: 2850
  • Country: se
  • Country: Broken Britain
Re: Arduino PIR
« Reply #5 on: December 07, 2019, 09:48:14 pm »
PIRs usually need a lot of gain to get a usable signal out of them so it's probably the PIRs 5V supply varying bit. In software perhaps you could ignore any PIR triggers for the 2 seconds after the LED is turned off to give it time to stabilize.
.  That took much longer than I thought it would.
 

Offline Wimberleytech

  • Super Contributor
  • ***
  • Posts: 1133
  • Country: us
Re: Arduino PIR
« Reply #6 on: December 07, 2019, 10:04:57 pm »
What color is the LED?  Red????
Can the PIR see it???
 

Offline darinsquaredTopic starter

  • Contributor
  • Posts: 39
  • Country: ca
Re: Arduino PIR
« Reply #7 on: December 07, 2019, 10:11:21 pm »
What color is the LED?  Red????
Can the PIR see it???

white. I cover the LED (it is directly on Arduino) and it make no difference.
 

Offline Wimberleytech

  • Super Contributor
  • ***
  • Posts: 1133
  • Country: us
Re: Arduino PIR
« Reply #8 on: December 07, 2019, 10:13:54 pm »
What color is the LED?  Red????
Can the PIR see it???

white. I cover the LED (it is directly on Arduino) and it make no difference.

I just ran an experiment using a red LED and it will indeed trigger the PIR and oscillate.  Different code.

Next question.  Is it a fresh 9V battery?

BTW, you did not need to test the PIR for a transition.  The PIR is already handling that for you.
 

Offline darinsquaredTopic starter

  • Contributor
  • Posts: 39
  • Country: ca
Re: Arduino PIR
« Reply #9 on: December 07, 2019, 10:48:17 pm »


I just ran an experiment using a red LED and it will indeed trigger the PIR and oscillate.  Different code.

Next question.  Is it a fresh 9V battery?

BTW, you did not need to test the PIR for a transition.  The PIR is already handling that for you.

9V battery is putting out 6.8V.
How is your code different?
 

Offline darinsquaredTopic starter

  • Contributor
  • Posts: 39
  • Country: ca
Re: Arduino PIR
« Reply #10 on: December 07, 2019, 11:02:42 pm »
What color is the LED?  Red????
Can the PIR see it???

white. I cover the LED (it is directly on Arduino) and it make no difference.

I just ran an experiment using a red LED and it will indeed trigger the PIR and oscillate.  Different code.

Next question.  Is it a fresh 9V battery?

BTW, you did not need to test the PIR for a transition.  The PIR is already handling that for you.

I covered the LED so it could not be triggering the PIR. If that was the case it should have occurred as well when the board was connected to computer?

I also noticed that when connected to computer the TX diode blinks before and after the LED turns ON and OFF.  this does not occur connected to battery.
 

Offline Wimberleytech

  • Super Contributor
  • ***
  • Posts: 1133
  • Country: us
Re: Arduino PIR
« Reply #11 on: December 07, 2019, 11:27:19 pm »
Gotta go now...will come back to this later. 
 

Offline Kasper

  • Frequent Contributor
  • **
  • Posts: 742
  • Country: ca
Re: Arduino PIR
« Reply #12 on: December 07, 2019, 11:54:53 pm »
9V battery is putting out 6.8V.

Your battery seems low. Batteries sometimes change output voltage when load changes and it sometimes changes extra when battery is low.

Measure Vbat when led on and when led off.
Vbat (led on) = ___ V
Vbat (led off) = ___V

If it varies then do some experiments to see if turning led on/off triggers motion detection.  Try a new battery, does that solve it? Do you have long battery wires? That causes extra voltage fluctuations with changing load.

Some PIRs can be sensitive to fluctuations in their supply voltage. The changing load from the LED can cause Vbatt to change and cause false trigger on PIR.

A picture of your setup and wiring could be helpful.
 

Offline Wimberleytech

  • Super Contributor
  • ***
  • Posts: 1133
  • Country: us
Re: Arduino PIR
« Reply #13 on: December 08, 2019, 01:41:04 pm »
Quote
9V battery is putting out 6.8V.
How is your code different?
Your battery is dead.

--------------------
Here is my code
--------------------

int ledpin = 13;         //Set the digital 13 to LED
int buzzPin = 6;     // buzzer pin for pwm output
int PIRpin = 2;         //Set the digital 2 to PIR
void setup() {
  Serial.begin(9600);
  pinMode( ledpin, OUTPUT);    //initialize the  led pin as output
  pinMode( PIRpin, INPUT);    //initialize the  PIR pin as input
  pinMode( buzzPin, OUTPUT);  //initialize the buzzer pin as an output
}

int count = 0;
void loop() {
  //Serial.print (count);

   delay(1 * 100); //delay one hundred milliseconds
  if (digitalRead(PIRpin) == HIGH) //Detecting whether the body movement information
  {
    count = 0;
    digitalWrite(ledpin,LOW);//LED ON
  }
  else
  {
    count++;
  }
  if(count == 30)
  {
    digitalWrite(ledpin,HIGH);//LED ON
    count=0;
  } 
}
« Last Edit: December 08, 2019, 01:51:32 pm by Wimberleytech »
 

Offline Wimberleytech

  • Super Contributor
  • ***
  • Posts: 1133
  • Country: us
Re: Arduino PIR
« Reply #14 on: December 08, 2019, 04:40:19 pm »
An arduino uno takes your input voltage and drops it across a diode and then regulates using a regulator with about 1V dropout voltage.

6.8 V - 0.7 V = 6.1 V

The regulator is just on the edge of stability.

In addition, the internal resistance will drop the open-circuit voltage from 6.8V to something lower (as current flows out of the battery).

Thus, for this application, your battery is dead (as stated in a previous post).
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf