Arduino PIR - Page 1
Forum section: electronics / beginners

author: darinsquared
author_rank: Contributor
timestamp: December 07, 2019, 08:27:44 pm
content: 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 outputpinMode(sensor, INPUT); // initialize sensor as an inputSerial.begin(9600); // initialize serial}void loop(){val = digitalRead(sensor); // read sensor valueif (val == HIGH) { // check if the sensor is HIGHdigitalWrite(led, HIGH); // turn LED ONdelay(100); // delay 100 millisecondsif (state == LOW) {Serial.println("Motion detected!");state = HIGH; // update variable state to HIGH}}else {digitalWrite(led, LOW); // turn LED OFFdelay(200); // delay 200 millisecondsif (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?

author: igendel
author_rank: Frequent Contributor
timestamp: December 07, 2019, 08:31:27 pm
content: What is the power source of the sensor?

author: darinsquared
author_rank: Contributor
timestamp: December 07, 2019, 09:18:32 pm
content: Quote from: igendel on December 07, 2019, 08:31:27 pmPIR connected directly to 5V and ground on Arduino and Arduino board connected to 9V battery

author: darinsquared
author_rank: Contributor
timestamp: December 07, 2019, 09:23:41 pm
content: 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.

author: igendel
author_rank: Frequent Contributor
timestamp: December 07, 2019, 09:46:38 pm
content: Quote from: darinsquared on December 07, 2019, 09:23:41 pmI 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.

author: StillTrying
author_rank: Super Contributor
timestamp: December 07, 2019, 09:48:14 pm
content: 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 secondsafterthe LED is turned off to give it time to stabilize.

author: Wimberleytech
author_rank: Super Contributor
timestamp: December 07, 2019, 10:04:57 pm
content: What color is the LED? Red?Can the PIR see it???

author: darinsquared
author_rank: Contributor
timestamp: December 07, 2019, 10:11:21 pm
content: Quote from: Wimberleytech on December 07, 2019, 10:04:57 pmwhite. I cover the LED (it is directly on Arduino) and it make no difference.

author: Wimberleytech
author_rank: Super Contributor
timestamp: December 07, 2019, 10:13:54 pm
content: Quote from: darinsquared on December 07, 2019, 10:11:21 pmI 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.

author: darinsquared
author_rank: Contributor
timestamp: December 07, 2019, 10:48:17 pm
content: Quote from: Wimberleytech on December 07, 2019, 10:13:54 pm9V battery is putting out 6.8V.How is your code different?

author: darinsquared
author_rank: Contributor
timestamp: December 07, 2019, 11:02:42 pm
content: Quote from: Wimberleytech on December 07, 2019, 10:13:54 pmI 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.

author: Wimberleytech
author_rank: Super Contributor
timestamp: December 07, 2019, 11:27:19 pm
content: Gotta go now...will come back to this later.

author: Kasper
author_rank: Frequent Contributor
timestamp: December 07, 2019, 11:54:53 pm
content: Quote from: darinsquared on December 07, 2019, 10:48:17 pmYour 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) = ___ VVbat (led off) = ___VIf 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.

author: Wimberleytech
author_rank: Super Contributor
timestamp: December 08, 2019, 01:41:04 pm
content: QuoteYour battery is dead.--------------------Here is my code--------------------int ledpin = 13; //Set the digital 13 to LEDint buzzPin = 6; // buzzer pin for pwm outputint PIRpin = 2; //Set the digital 2 to PIRvoid setup() {Serial.begin(9600);pinMode( ledpin, OUTPUT); //initialize the led pin as outputpinMode( PIRpin, INPUT); //initialize the PIR pin as inputpinMode( buzzPin, OUTPUT); //initialize the buzzer pin as an output}int count = 0;void loop() {//Serial.print (count);delay(1 * 100); //delay one hundred millisecondsif (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 ONcount=0;}}

author: Wimberleytech
author_rank: Super Contributor
timestamp: December 08, 2019, 04:40:19 pm
content: 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 VThe 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).