Servo works fine while my arduino is plugged into the laptop but goes nuts around a battery

don't know what's causing it... i made a simple bar gate that opens on the condition that the ultrasonic sensor detects something and a button is pressed. it works as intended when i plug it into my laptop but it starts glitching out and doing it's thing. here's the code:

const int triggerPin = 7;
const int echoPin = 8;
const int butpin = 12;
const int led = 13;
int pos = 0;
bool valid = false;

#include <Servo.h>

Servo myservo;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  pinMode(butpin, INPUT_PULLUP);
  myservo.attach(4);
  pos = 0;
}

void loop() {
  // establish variables for duration of the ping, and the distance result
  // in inches and centimeters:
  long duration, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(triggerPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH pulse
  // whose duration is the time (in microseconds) from the sending of the ping
  // to the reception of its echo off of an object.
  duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);

  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);


  if(digitalRead(butpin) == LOW && cm <= 50){
    digitalWrite(led, HIGH);
    valid = true;
  }
  else{
    digitalWrite(led, LOW);
    valid = false;
  }

  if(valid == true){
    for (pos = 0; pos <= 90; pos += 1){
      myservo.write(pos);
      delay(15);
    }
    for (pos = 90; pos >= 0; pos -= 1) {
      myservo.write(pos);
      delay(15);
    }
  }
}


long microsecondsToCentimeters(long microseconds) {
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the object we
  // take half of the distance travelled.
  return microseconds / 29 / 2;
}

What kind of battery?

it is a 9v 6F22. i plug it into the arduino through the barrel jack

a few things wrong with that.

  • 9V smoke alarm battery cannot provide the current needed by the servo
  • By feeding the Arduino and running the servo off the Arduino 5V out (you didn't provide a diagram, so I'm assuming...), you're running all the servo current through the tiny regulator on the Arduino, which will cause it to fail, or overheat and shut down

When you use USB, it bypasses the onboard regulator and feeds computer 5V directly to the servo, which, while it works, is also not a good idea. Buy a small 5V adaptor, or a USB phone charger which provides 5V.

1 Like

oh I see the problem now. could i solve it by using something like an IC 7805 voltage regulator?

Yes, but not with the 9V smoke alarm battery.

1 Like

what batteries would you recommend?

Tell us more about what you want to do. I'm worried that suggested alternatives will, in turn, run afoul of things you haven't told us.

it's nothing big really.


my phone doesn't have good image quality but this was just a small learning experience. i have pretty much written everything about it.

And you want to now run the device as we see it off battery, not USB? It's a bit of a problem. You could supply the Servos with 4AA batteries (6.4 v fully charged, declining to about 5 when nearly discharged), but that voltage is not good for the Arduino. You could run that with the 9V battery, but between it and the ultrasonic sensor, your 9V battery life will be fairly short, so it becomes a matter of how short is too short.

If you do use the AA solution for the servo, be sure to connect the - side of the battery pack to the Arduino ground, as the control signal from the Arduino to the servo MUST have a return path, or the signal from the Arduino won't be understood by the servo.

Sorry, that's just the way it is.

1 Like

thank you, you were a huge help :folded_hands:

1 Like

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