Motor servo only moves after reset

Hey, I'm new to arduino (just started last week) and I don't really know if this is the right subcategory or not so sorry beforehand. I'm currently making my school project that's due in less than 3 days so any help is greatly appreciated.

I'm using arduino uno board, breadboard, ultrasonic sensor, and mg996r motor servo and the problem is it only works after reset or if I unplug then plug back the arduino to my laptop. It'd move once or twice then stop moving at all. At first I thought there was a problem on the arduino but the sensor seems to work fine. The thing is my motor servo was working normally the day before so I have no clue what happened. I checked the wiring and all, even remade the whole thing for precaution.

Also, the servo sometimes made noises and sometimes not at all, it'd also vibrate sometimes. I changed the servo using sg90 instead but the same issue follows. I changed arduino board and same thing happened.

Here's the code I use, it's from the video I followed

#include <Servo.h>
Servo servo;
int angle = 10;
// defines pins numbers
const int trigPin = 12;
const int echoPin = 11;
// defines variables
long duration;
int distance;
void setup() {
  servo.attach(8);
  servo.write(angle);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
delay(10);

if(distance<30)
{
  servo.write(160);
  delay(4000);
  }
  else
  {
    servo.write(0);
    }
}

If the issue can be solved quickly, I'm also planning to add lcd with i2c so any tips are appreciated. Sorry if my question sucks, english isn't my first language.

never mind, it sometimes works and moves randomly (???) even when nothing is in front of the ultrasonic sensor.

You need to show a diagram of how you have things connected.

Verify wiring connections are good and your power supply is sufficient.

l'm only using the 5v pin from arduino, do you think it's enough? Because it was working good enough

any app recommendation that's free?

That is the problem. Making that connection can even destroy the Arduino.

Use an external power supply capable of providing 5-6V at at least 2.5 Amperes, as that is what the servo requires to start moving. Don't forget to connect the grounds.

No you need to power the servo from a 5V supply or batteries NOT from the 5V pin

Nothing you can learn in a few minutes but EasyEDA is nice.
Just make a nice drawing with pen and paper.

can I connect 2xAA twice as the external power supply to the breadboard? Thanks for the info btw!

ah makes sense, thanks! Is there a chance for me to connect 2xAA twice as the external power supply to the breadboard?

  • Sure but a servo needs 4 AA batteries (6V)

okay, thanks!

  • That’s 4 (four) 4 X 1.5v = 6v

got it, thx again

You can power the servo with 4AAs but not the Uno or ultrasonic sensor

Yea, thanks a lot!