Servo power issue with Mini Pro?

In spite of the title I’m not really sure if my issue is power related or something else.

I’m trying to drive a servo (MG995) through an Arduino Pro Mini (MEGA328P) using a toggle switch to control the servo direction.

My circuit is powered by a 9V battery running through a 7805 voltage regulator to bring the voltage down to 5V.

When the circuit is powered through the loader via the computer USB the system works flawlessly, so I assume the coding is fine as is the Pro Mini configuration. However, when powered by the battery alone the servo acts randomly and since part of the code reads servo position, the system fails.

I have tried 2 9V batteries in parallel thinking more amperage was required but got the same results.

Interestingly when I run the same script on the Arduino Uno, configured the same way, it works fine with 9V battery power.

So my question is whether there is something special about the Mini Pro that I am not aware of that would cause this issue? Or any other reason(s) I might be experiencing this problem.

I have attached a copy of the sketch and a (rough) schematic. It’s not obvious from the diagram, but I have all grounds connected at a common point. The pulldown resistor is 1K ohms.

#include <Servo.h>

const int  buttonPin = 3;  //input pin for the switch
int buttonState = 0;       // status of the switch - open or closed (HIGH)
int OFFpos = 103;          // variable to control the 'at rest' position of the servo. 
int ONpos = 10;              //variable to control the max rotation of the servo to flip the switch
int ledPin = 13;           // LED in box


Servo myservo;  

int pos;                // variable to store the servo position

void setup()
{
  pinMode(buttonPin, INPUT);
  pinMode (ledPin, OUTPUT);
  myservo.attach(6);          // attaches the servo on pin 3  to the servo object
  myservo.write(OFFpos);      //sets initial position of servo
}

void loop()                
{
  buttonState = digitalRead(buttonPin);  // Read the button position
  if (buttonState == HIGH) {
     for(pos = myservo.read(); pos >= ONpos; pos -= 1) { // moves servo from at rest (OFFpos) in single steps                                  
       digitalWrite(ledPin, HIGH);                 //turns LED on
       myservo.write(pos);                        // tell servo to change position
       delay(15);                                  // controls speed to reach position
       buttonState=digitalRead(buttonPin);
       if (buttonState != HIGH) { break; } //allows for user to close switch early
     }
  }
  else {
       for(pos = myservo.read(); pos <=OFFpos; pos += 1) {// returns servo to 'at rest' position                            
       digitalWrite(ledPin, LOW);
       myservo.write(pos);                         // tell servo to go to position in variable 'OFFpos'                
       delay(15);                                         // controls speed for the servo to retract the arm
     }
  }
}

Where is the servo's power coming from? The 7805 may not be able to pass enough current to start the servo and if the voltage is dragged down enough, the Mini will reset, a 9V battery is hopeless for running even a small motor, even 2 in parallel may not be able to start a servo.

Try connecting 9volt(+) directly to the V-in(raw) pin of the ProMini.
And only power the servo through the 7805 (which needs decoupling caps).
As outsider already explained, 9volt smoke alarm batteries are hopeless for a servo setup.
Leo..

Thanks. I will try to connect directly to the RAW pin as suggested. But based on your info plus other reading in this forum I'm going to change out my power to a 4xAA setup which should (if I understand correctly) allow me to power both the servo directly and the mini (via RAW input). I'll be sure to provide an update (or desperate request for help) once I do.

Wawa:
Try connecting 9volt(+) directly to the V-in(raw) pin of the ProMini.
And only power the servo through the 7805 (which needs decoupling caps).
As outsider already explained, 9volt smoke alarm batteries are hopeless for a servo setup.
Leo..

Wawa - I just tried your suggestion and it now works fine. Thank you! Will still upgrade to 4xAA for power though.