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
}
}
}