PIR sensor controlling servo always reading HIGH

I am using a PIR motion sensor to control a servo arm. The servo opens and closes the lid on a container. It opens and then stays open for five seconds and then closes until motion is detected once again. Initially it was working well but now the PIR seems to be stuck on HIGH and so when the lid closes it automatically reopens, waits the five seconds and then closes again, and then repeats and repeats. I have checked the wires to make sure everything is correct. I have switched out the PIR and it doesn't make a difference. here is the code I am using ( it is from https://www.instructables.com/Motion-Activated-Servo/ with an added delay for staying open a few seconds.) I am new at this and trying to teach my son. Any help would be appreciated. Thanks.
#include <Servo.h>

Servo myservo; //creates a servo object
//a maximum of eight servo objects can be created

int pos = 0; //variable to store servo position

//amount of time we give the sensor to calibrate(10-60 secs according to the datasheet)

int calibrationTime = 30;

//the time when the sensor outputs a low impulse
long unsigned int lowIn;

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;

boolean lockLow = true;
boolean takeLowTime;

int pirPin = 12; //digital pin connected to the PIR's output
int pirPos = 13; //connects to the PIR's 5V pin

void setup(){
myservo.attach(4); //attaches servo to pin 4
Serial.begin(9600); //begins serial communication
pinMode(pirPin, INPUT);
pinMode(pirPos, OUTPUT);
digitalWrite(pirPos, HIGH);

//give the sensor time to calibrate
Serial.println("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(calibrationTime - i);
Serial.print("-");
delay(1000);
}
Serial.println();
Serial.println("done");

//while making this Instructable, I had some issues with the PIR's output
//going HIGH immediately after calibrating
//this waits until the PIR's output is low before ending setup
while (digitalRead(pirPin) == HIGH) {
delay(500);
Serial.print(".");
}
Serial.print("SENSOR ACTIVE");
}

void loop(){

if(digitalRead(pirPin) == HIGH){ //if the PIR output is HIGH, turn servo

/*turns servo from 0 to 180 degrees and back
it does this by increasing the variable "pos" by 1 every 5 milliseconds until it hits 180
and setting the servo's position in degrees to "pos" every 5 milliseconds
it then does it in reverse to have it go back
to learn more about this, google "for loops"
to change the amount of degrees the servo turns, change the number 180 to the number of degrees you want it to turn
**/
for(pos = 0; pos < 100; pos += 1)  //goes from 0 to 180 degrees
{                                                 //in steps of one degree
  myservo.write(pos);                   //tells servo to go to position in variable "pos"
  delay(15);                                   //waits for the servo to reach the position
}
delay(5000);
for(pos =100; pos>=1; pos-=1)    //goes from 180 to 0 degrees
{                               
  myservo.write(pos);                  //to make the servo go faster, decrease the time in delays for
  delay(15);                                  //to make it go slower, increase the number.
    }
if(lockLow){ 
  //makes sure we wait for a transition to LOW before further output is made
  lockLow = false;           
  Serial.println("---");
  Serial.print("motion detected at ");
  Serial.print(millis()/1000);
  Serial.println(" sec");
  delay(50);
}        
takeLowTime = true;

}

if(digitalRead(pirPin) == LOW){

if(takeLowTime){
  lowIn = millis();             //save the time of the transition from HIGH to LOW
  takeLowTime = false;    //make sure this is only done at the start of a LOW phase
}

//if the sensor is low for more than the given pause,
//we can assume the motion has stopped
if(!lockLow && millis() - lowIn > pause){
  //makes sure this block of code is only executed again after
  //a new motion sequence has been detected
  lockLow = true;                       
  Serial.print("motion ended at "); //output
  Serial.print((millis() - pause)/1000);
  Serial.println(" sec");
  delay(50);
}

}
}

Since it worked then quit I would expect a hardware problem, not a code problem. Post a Schematic as you wired it, not a frizzy thing. Include links to technical information on the hardware devices.

why not reprogram to simply report the state of the PIR to verify the circuitry?

manually move the lid to verify that the PIR indicates what you expect

Yes. Or use a very simple program, perhaps even one you have around, that simply tests the PIR device.

a7

Hi, @fizzywizzy3
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

Can you please post a copy of your circuit diagram, a hand drawn schematic would be fine.
Please include pin names and component labels as well as your power supply.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

for all future posting of code
You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

My experience with PIR sensors are that they are very sensitive to noice. Try to add a LC filter before the PIR sensor.