0

I am working on this code to turn a servo when the PIR motion sensor detects no motion, but it is not working. Some symptoms include:

  1. Servo is turning extremely slowly
  2. PIR is not turning the servo
  3. Servo is turning to incorrect angles

I have tried test programs, and when I type in the serial monitor, the servo turns (slowly and inaccurately) (I coded the serial monitor to do this). I have input from the PIR sensor but it is constantly high, even after waiting 2 minutes for it to initialize. My code and schematic are shown below.

Schematic

My code:

#include <Servo.h>
Servo servo;

void setup() {
  // put your setup code here, to run once:
  servo.attach(13);
  pinMode(2, INPUT);
  Serial.begin(9600);
  Serial.println("Please wait");
  delay(60000);
}

void ifMos() {
  if (digitalRead(2) == HIGH) {
    int USER = 270;
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  int USER = Serial.parseInt();
  servo.write(USER);
  Serial.println(USER);
  ifMos();
  delay(10000);
}
1
  • 1
    firget about the servo for now ... write the simplest code that lights an LED when motion is detected
    – jsotola
    Commented Jul 11, 2022 at 19:38

1 Answer 1

1

firget about the servo for now ... write the simplest code that lights an LED when motion is detected – -jsotola

It worked! Thank you so much. All I had to do was add the servo library, write to the servo and upload it. Only 21 lines of code! Final code:

#include <Servo.h>
Servo servo;

void setup() {
  // put your setup code here, to run once:
  pinMode(2, INPUT);
  pinMode(3, OUTPUT);
  servo.attach(13);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(2) == HIGH) {
    digitalWrite(3, HIGH);
    servo.write(270);
  }
  else {
    digitalWrite(3, LOW);
    servo.write(0);
  }
}

Thank you to everyone. This was my first time posting on stack exchange, and it was a huge success. Thank you again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.