PIR sensor turning servo motor 90 degrees and stopping

Hey All,

First and foremost I am extremely new to this whole world and have never built,interacted, or coded anything before. I am having a lot of trouble with the terms and dialogue I have researched simply because they are new to me. I am trying to piece them together and learn so please bare with me.

What I am attempting to do is set up a PIR sensor near a door so that whenever someone walks in or exits it make a servo motor turn 90 degrees and then stop. Seems simple but I am having issues.

I have gone through the led tutorials and know my sensor is working properly.

Here is what I have: http://www.amazon.com/Arduino-Ultimate-Starter-Includes-Instruction/dp/B00BT0NDB8 and http://www.adafruit.com/products/189?gclid=CO-3oq_g88MCFVFhfgodvBAA_Q

Here is a code I’ve seen online a few times that makes the servo motor turn 180 degrees and then back looped a few times. It works and is close to what I want. I thought I could modify it to make it only turn 90 degrees and then stop but I can’t figure it out:


/* This code sweeps a servo from 0 degrees to 180 when the PIR sensor detects motion.
   Special thanks goes to the author of the PIR sensor code, whose code helped tremendously
   in the making of this code and Instructable.
   author of PIR sensor code: Kristian Gohlke / krigoo (_) gmail (_) com / http://krx.at

#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);
  //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) {
  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 < 180; 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(5);                                   //waits for the servo to reach the position
    for(pos = 180; 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(5);                                  //to make it go slower, increase the number.
      //makes sure we wait for a transition to LOW before further output is made
      lockLow = false;           
      Serial.print("motion detected at ");
      Serial.println(" sec");
    takeLowTime = true;

  if(digitalRead(pirPin) == LOW){      

      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");

I have done some research and it seems servo motors can’t turn more then 180 degrees? Is this in one motion or ever? The servo motor is attached to a wheel that I want to keep spinning 90 degrees counterclockwise each time the PIR sensor is activated.

So essentially someone opens the front door the PIR sensor is activated sends signal to the servo motor to turn 90 degrees and then stops. Next time someone walks in or exits it moves 90 degrees from the previous spot and so on. So it needs to be able to move 360 degrees but only 90 degrees each time it’s activated.

If a servo motor can’t turn 360 degrees what is it that I should purchase to accomplish this? A stepper motor? Again sorry if I am way off. I’ve tried for a while now to figure it out but I figured I’d come to the professionals for some guidance and to prevent me from buying something I may not need.

Any help is appreciated. Thanks!

stone_196: I have done some research and it seems servo motors can't turn more then 180 degrees? Is this in one motion or ever? The servo motor is attached to a wheel that I want to keep spinning 90 degrees counterclockwise each time the PIR sensor is activated.

A hobby servo can be positioned anywhere in a (roughly) 180-degree arc. It will hit a physical stop if it tries to turn a full revolution.

You can use a stepper motor to turn a controlled amount and continue to turn as many turns as you like.

You could also use the limited range of a servo to ratchet a wheel around by 1/4 turn at a time. That will take some mechanical design.