controlling a servo with a PIR sensor

I’ve been learning abuot arduino lately and i could not make this work properly, the servo goes crazy when i put my hand in front of the PIR and i don’t know what to do.
Would someone please tell me if my code is correct?
My sketch is downn here.

self_switch.ino (490 Bytes)

1.) ALWAYS power servos off an external power supply. This may be the cause of your servos not working properly. How are you powering them?

2.) Look at this tutorial.


#include <avr/power.h> 

#include <Servo.h>

Servo servo;

#define PIR 7

boolean state =HIGH;
int stateser =0;

void setup() {

  servo.attach (9);
  pinMode (PIR ,INPUT);
  servo.write (125);


void loop() {

  state = digitalRead (PIR);
  if (( state == HIGH)&& (stateser == 125)) {
    servo.write (50);
    delay (1000);
    state = LOW;
  if (( state == HIGH )&& (stateser = 50)){
    servo.write (125);
    delay (1000);
    state = LOW;


Thanks, I'll try it out and see what happens.

(stateser = 50)

You probably meant:(stateser == 50)

I powered the servo from an external power supplie and connected all negatives together but still nothing changed, my servo gos back and forth from 125 to 55 degrees a couple of times when i activate the PIR.
I think there's something wrong with the code, I also added the "=" sing that was missing.
Can anyone tell me please what can i do to correct it?

I powered the servo from an external power supplie and connected all negatives together but still nothing changed

You still haven't answered my question:

How are you powering them?

Can you post a schematic?

Another piece of advice would be to include serial prints in different places in your code to see what exactly the program is doing.

The way the code is written, as long as the input is HIGH the servo will toggle back and forth. It sound like you want the servo to only toggle each time a new motion is detected.

To tell a new motion from an old motion you have to know if the input is CHANGING from LOW to HIGH or is staying HIGH. To do that, you save the previous state of the input and check that the new state is both HIGH and different from the old state.

#include <Servo.h>
Servo servo;

const byte PIR = 7;

boolean PreviousState = LOW;

void setup() {
  pinMode(PIR, INPUT);

void loop() {
  boolean state = digitalRead(PIR);

  if (state != PreviousState) {
    PreviousState = state;
    if (state == HIGH) {
      if ( == 125) {
        servo.write (50);
      } else {
        servo.write (125);

If you remove the PIR and switch the input manually, what happens?
That is, switch the input ON and leave it ON for 5 Seconds, then turn it OFF.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom... :slight_smile:

I'm powering it trough a pealed usb cable with a portatile charger, an the ngative connected to a negative port of the arduino, but it wasn't necessary.
The code of johnwasser works great, thanks.
And thank you all for helping me.

Hi good day Ma’am/Sir,

I just want to ask for a help regarding my project "Automated Door using Pir and Servo(DOOR) like the one on the entrance of malls but in cheaper and miniature version.

​I just want to ask for your help in coding.

​The scenario is, when the PIR SENSOR detects motion, the door opens BUT it closes after 5 seconds(5 seconds was based on the adjustment I’ve done on the Time Delay trimmer of the PIR).
​Now, the condition is, when the PIR detects someone and he/she is staying at the opening of the door, the door should remain open.
​Can you please help me figure out the logic behind that automated door? where as long as im staying in the front of the door, the servo/door SHOULD still remain open and it only CLOSES when i walk pass the door or no any other motion is detected.
​Please :frowning:

​This is my code and i know im missing some logic in this. I hope for your response, and any advice would be a big big big help to me. Thank you and godbless!

​#include <Servo.h> // Library for Servo
Servo myservo; // Name of my Servo
int led = 13;

int sensor = 2; // Sensor is connected to D2
int state = LOW; // Initial value for variable state is low
int val = 0; // Initial value for variable val is 0
void setup() {
pinMode(led, OUTPUT);
myservo.attach(9); // Servo is connected to D9
pinMode(sensor, INPUT); // D2 is set as a Input
Serial.begin(9600); // Start for Serial Communication
void loop() {
val = digitalRead(sensor);
digitalWrite(led, HIGH);
if (state == LOW){
Serial.println(“Motion Detected”);
state = HIGH;

else {
digitalWrite(led, LOW);
if(state == HIGH){
Serial.println(“Motion Stopped”);
state = LOW;