Servo Motor Works with One Code but Not Another

Hello all,

I encountered a very strange problem with my servo motors and I am driving myself nuts trying to solve the issue. Basically, when I uploaded a piece of working code that I previously wrote I am able to drive the servo, so I know for sure that the motor itself, the connection, nor the board are faulty. However when I upload a very simple code that only calls servo.write() method for some reason the servo doesn't move at all and doesn't even make a sound. I made sure the program is executing by serial printing. That made me believe that the only issue is in the code but I just don't understand how it could be different. My old code that works creates a class for writing into the servo but fundamentally I am calling the same method servo.write(). Here is the "simple code" I can't get to work. If I could get any insight that would be greatly appreciated! I am powering the servo using a separate 5V external source and I made sure that the servo is still working by uploading the old code. Thanks!

#include <Servo.h>
Servo myservo;

void setup()

void loop() {


For your reference, here is the old code that uses class:

class gripper {
    byte pin;
    int gripAngle;
    int ungripAngle;
    Servo servo1;
    int currentAngle;
    gripper(byte pin);
    void init();
    void grip();
    void ungrip();
    void grip(int rate);
    void printCurrentAngle(); 

#include "gripper.h"

gripper::gripper(byte pin) {
  this->pin = pin;
void gripper::init() {
void gripper::grip() {

void gripper::ungrip() {

void gripper::grip(int rate) {
  int current =;
  for (int i = current; i >= gripAngle; i--) {


void gripper::printCurrentAngle(){
  Serial.print("Current Angle: ");


Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.
Please take a few moments to Learn and Use The Forum

It will help you get the very best out of the forum in the future.

  • Your OS and version can be valuable information, please include it along with extra security you are using. Antivirus etc.

  • Always list the version of the IDE you are using and the board version if applicable.

  • Use quote or add error messages as an attachment NOT a picture.

  • How to insert an image into your post. ( Thanks @sterretje )

  • Add your sketch where applicable but please use CODE TAGS

  • Add a SCHEMATIC were needed even if it is hand drawn

  • Add working links to any specific hardware as needed (NOT links to similar items)

  • Remember that the people trying to help cannot see your problem so give as much information as you can


  • Ensure you have FULLY inserted the USB cables.

  • Check you have a COMMON GROUND where required. ( Thanks @Perry)

  • Where possible use USB 2.0 ports or a USB 2.0 POWERED HUB to rule out USB 3.0 issues.

  • Try other computers where possible.

  • Try other USB leads where possible.

  • You may not have the correct driver installed. CH340/341 or CP2102 or FT232 VCP Drivers - FTDI

  • There may be a problem with the board check or remove your wiring first.

  • Remove any items connected to pins 0 and 1.


  • Close any other serial programs before opening the IDE.

  • Ensure you turn off any additional security / antivirus just to test.

  • There may be a problem with the PC try RESTARTING it.

  • You may be selecting the wrong COM port.

  • Avoid cloud/network based installations where possible OR ensure your Network/Cloud software is RUNNING.

  • Clear your browsers CACHE.

  • Close the IDE before using any other serial programs.

  • Preferably install IDE’s as ADMINISTRATOR or your OS equivalent


  • CH340/341 based clones do not report useful information to the “get board info” button.

  • NANO (Old Types) some require you to use the OLD BOOTLOADER option.

  • NANO (ALL Types) See the specific sections lower in the forum.

  • NANO (NEW Types) Install your board CORE’s.

  • Unless using EXTERNAL PROGRAMMERS please leave the IDE selection at default “AVRISP mkII”.

  • Boards using a MICRO usb connector need a cable that is both DATA and CHARGE. Many are CHARGE ONLY.

CREATE editor install locations.

  • On macOs ~/Applications/ArduinoCreateAgent-1.1/

  • On Linux ~/ArduinoCreateAgent-1.1/config.ini

  • On Windows C:\Users[your user]\AppData\Roaming\ArduinoCreateAgent-1.1

Performing the above actions may help resolve your problem without further help.

Language problem ?

Try a language closer to your native language:

Thanks to all those who helped and added to this list.

What do the Serial.prints say? What are the servo + wire, - wire and signal wire connected to?

The main difference I can see is that the old code is doing multiple writes moving only 1 degree at a time. The new code is trying to move in big jumps which takes more power. That can causes problem if the servo power is marginal. What servo do you have and what exactly is the "separate 5V external source"?


The external power source is a wall plug 5V power supply. Turns out that I need to ground the Arduino board to the same GND as the motors, at least that did the trick for me!

It is weird however that when I didn't ground the board my old code that uses class still worked....

Thanks for taking the time to reply!

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