Trouble Having Arduino Uno Run Sketch

Here’s the code I’m trying to run:

#include <Servo.h>
Servo servoRight;

void setup(){
  Serial.begin(9600); //Serial Debugging
  servoRight.attach(2); //Attach servo
  
}

void loop()
{
  Serial.println("My sketch has started"); // Serial Debugging
  delay(10000);
  servoRight.write(180); //spin servo backwards
  delay(10000); //wait ten seconds
  servoRight.write(0); //Spin servo other way
}

It’s very simple, just spin the servo ten seconds, then spin it the other way. (I’m using a continuous rotation servo). I’m using arduino uno and connecting it to the computer using the on board serial to usb connection. I upload the sketch, the lights on the board blink, and the ide says upload complete. And i have the uno selected under boards.

Here’s My circuit setup:

It’s exactly that, AND i have tried this with the breadboard and direct connection of servo to uno. I also have verified that the digital port is the same one in my code.

All that seems right I’m mostly sure.

Here’s where the problem starts:

After the sketch gets uploaded, (with the circuit broken), I connect the power wire to the 5V slot and two things happen simultaneously:

  1. The arduino board instantly disconnects from the computer and draws only power, sending no data through.
  2. The servo begins to spin in one direction (good i suppose), 20 seconds later nothing happens, just keeps spinning.

I break the circuit and the uno reconnects to the computer. I’ve also tried the same procedure using the sweep example form the IDE, same results.

So what am I doing wrong exactly? Arduino issue or user error?

servoRight.attach(2); //Attach servo

Pin 9, or pin 2?

CrossRoads: servoRight.attach(2); //Attach servo

Pin 9, or pin 2?

To clarify, I did this with two different codes, the one posted specifies digital 2 is attached to servoRight and th servo is correctly connected to pin 2 on this trial, on the other code i tried, i used the sweep example from the ide, the example specifies pin 9 and i connected the servo to pin 9.

Seeing as i used two different trials using seperate pins and codes (one of which is verified to work by the ide), its not an incorrect/faulty pin or code problem i think.

My guess, your servo is pulling too much current and browning out the arduino (or rebooting it something). Can you try powering the servo from a dedicated power source and connecting the grounds?

WendoNZ: My guess, your servo is pulling too much current and browning out the arduino (or rebooting it something). Can you try powering the servo from a dedicated power source and connecting the grounds?

This fixed it. Thanks guys.