Need help with my first project

Hi. This is my first Arduino project. I am making a motion activated servo that shoots silly string for a project with the kids. It works fine except for one thing. Within a few seconds of being turned on the servo moves through its motion (45 degrees and then back). After that the servo only moves if there is motion in the field of view of the PIR. Normally this wouldn't be a problem except that it shoots silly string all over before anyone has moved in front of it which gives away the presence of the device and spoils the surprise.

I have been unable to figure out why the servo moves at power on. I am looking for a way to eliminate that problem. Below are the components I used, the code and the wiring diagram. Thanks in advance for any help.

Servo: Deegoo-FPV MG995
PIR : HC-SR501
Arduino: Arduino Uno

#include <Servo.h>

const int pirPin = 12;    // PIR sensor OUT pin
const int servoPin = 4;   // Servo signal pin

Servo myServo;

bool inCooldown = false;
unsigned long cooldownStartTime = 0;

void setup() {
  pinMode(pirPin, INPUT);
  myServo.attach(servoPin);
  myServo.write(0); // Initial position
  Serial.begin(9600);
}

void loop() {
  unsigned long currentTime = millis();

  // If we're in cooldown, check if it's over
  if (inCooldown) {
    if (currentTime - cooldownStartTime >= 3000) {
      inCooldown = false;
      Serial.println("Cooldown ended. Ready for new motion.");
    }
    return; // Skip rest of loop while in cooldown
  }

  int motionDetected = digitalRead(pirPin);

  if (motionDetected == HIGH) {
    Serial.println("Motion detected! Moving to 45 degrees.");

    myServo.write(45);        // Move to 45°
    delay(5000);              // Wait 5 seconds
    myServo.write(0);         // Return to 0°
    Serial.println("Returned to 0 degrees. Starting cooldown...");

    cooldownStartTime = millis(); // Start cooldown *after* returning to 0
    inCooldown = true;
  }

  delay(100); // Small delay to prevent noisy readings
}

  myServo.attach(servoPin);

When you attach() the servo it will initially move to its default starting position of 90 degrees. However, if you write() the required starting position to it before attaching it then it will move to, or more importantly, stay in that position if it is already there

Giving 6 volt to the 5 volt pin is dangerous. Know that a new alcalic cell can hold 1.55 volt giving 6.2 volt.

1 Like

So All I have to do is add the code you provided? Where should I add that in the sketch?

Thank you. I didnt realize that. I thought there was a voltage regulator on board could handle a small excess. I was trying to keep the project simple by using a single power source for the arduino and the servo. Do you have any suggestions? Should I run the power to the arduino through the 9mm pin?

    myServo.attach(servoPin);
    myServo.write(0);  // Initial position

Reverse the order of these two lines of code in setup()

1 Like

Great. Thanks for the help

Does it work the way that you want now ?

I had to run out. Ill try when I get home

Just checked and yes that fixed the problem. Thank you for the help

I am glad that it worked

Good luck with further projects

2 Likes

There is if you connect via the barrel jack. The regulator requires more than 5V to work properly. The Arduino docs say the minimum voltage is 7V.

I see. Does that mean the 6V battery pack is too much for the 5V input and too little for the barrel jack? Just trying to find a single battery solution for the Arduino and the Servo. It seems like this must be a pretty common problem. Maybe I have to search the forum for a solution to see what others have done.

Yes.

You can use these

1 Like

Thanks. Am I correct to assume there is no way to power the servo directly from the Arduino? So I would splice the servo wires directly into these wires?

Yes, you are right. Never use the Arduinos 5V-Regulator to supply servos.
When I build stuff like your project, I often use 2-Cell-Lipos (2S 800 mAh) in combination with small 5V UBECs. I connect the 5V output from the UBEC directly to 5V Pin on Arduino and Servo.
But keep in mind, LiPos require a special charger with balancer to charge them safely.


You could replace the servo horn or reposition its default by taking out the little screw on the end of the shaft and reseating the horn on the spline to where you like as default on startup.

(:robot: translated)
You can use a step-up converter to obtain 9V from the battery and properly power the Arduino Vin pin. This way, a single 6V battery is enough for everything. Additionally, you should avoid passing high currents through the Arduino, for example through two GND pins — it's better to connect the servos directly to the battery.

Did you adjust the variable POTs?