Controlling Servo Motor with Arduino and PIR Sensor

Hi! So I'm basically trying to rotate a servo to 90 degrees once a PIR sensor detects motion, and then I'll like to rotate it back to its original position (0 degrees) after the PIR sensor detects the second motion. I am able to rotate the servo to 90 degrees, but I can't get it to rotate back after the second motion. Can anybody help? The picture below is what my code looks like right now. *I'm a noob so please don't judge haha.

Do not post images of code. Read the forum guidelines to see how to post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto Format) to indent the code for readability before posting code.

You will get better and faster help if you read and follow the forum guidelines linked above.

You may get away with powering a small unloaded servo from the Uno 5V, but servos should have an external power supply capable of supplying the servo's stall current. See the servo data sheet for the stall current specification.

#include <Servo.h>

// constants won't change
const int MOTION_SENSOR_PIN = 12; // Arduino pin connected to motion sensor's pin
const int SERVO_PIN         = 9; // Arduino pin connected to servo motor's pin

Servo servo; // create servo object to control a servo

// variables will change:
int angle = 0;          // the current angle of servo motor  
int currentMotionState; // the current state of motion sensor

void setup() {
  Serial.begin(1500);                // initialize serial
  pinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input mode
  servo.attach(SERVO_PIN);           // attaches the servo on pin 9 to the servo object

  servo.write(angle);
  currentMotionState = digitalRead(MOTION_SENSOR_PIN);
}

void loop() {
  currentMotionState = digitalRead(MOTION_SENSOR_PIN);
 
  if (currentMotionState == HIGH && angle <= 90) { // pin state change: LOW -> HIGH
    angle ++;
    servo.write(angle); }
  else
  if (currentMotionState == HIGH && angle >= 0) {
    angle --;
    servo.write(angle);
  }
}

Thanks for that! I already posted my code in the right way. And yes, i'm planning to have an external power supply for the servo

in the slightly reformatted code above, the only time the else condition is executed while currentMotionState is HIGH is when angle is 91. the else condition decrements it to 90. but the angle is then <= 90 on the next iteration and the if condition then increments it

code need a state variable that determine when to increment or decrement. that state variable is toggled when the angle == 90 or == 0

Yes, I've also realized that when looking at the code again. I don't quite understand your explanation though. Do I use the digitalToggle function?

consider

enum { Forward, Reverse };
int state = Forward;
int angle = 0;

void loop() {
    byte currentMotionState = digitalRead(MOTION_SENSOR_PIN);

    if (currentMotionState == HIGH)  {
        if (Forward == state)  {
            if (++angle >= 90)
                state = Reverse;
        }
        else  {     // reverse
            if (--angle <= 0)
                state = Forward;      // *** corrected
        }
    }

    servo.write(angle);
}

or

int angle = 0;
int dir   = 1;

void loop() {

    if (HIGH == digitalRead(MOTION_SENSOR_PIN))  {
        angle += dir;
        if (angle <= 0 || 90 <= angle)
            dir = -dir;
    }

    servo.write(angle);
}

your code works if i switch the Reverse and Forward state. however, the problem is that whatever number of angle i put, the servo is still going to rotate 180 degrees (and it goes back to its original position after the second motion detected).

should there be some delay between changes to angle?

hi, i've found the solution to my problem already. thanks!

do you mind sharing?

2 posts were split to a new topic: I need help coding two servos to be controlled by a PIR sensor

im doing a similar project using both components however i want to limit the servo motor moving after it moves 5 times from the pir detecting motion. after those 5 times im planning on the servo motor not moving until a button is pressed however i kinda guessing i have to you a if else or for loop statement however im not sure why my attempts are not successful if anyone can help me it will bereally appreciated

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.