# Jittering motors ( and bad logic?..)

**URL:** <https://forum.arduino.cc/t/jittering-motors-and-bad-logic/1429679>\
**Category:** Programming\
**Created:** [February 8, 2026, 3:23am UTC](https://forum.arduino.cc/t/jittering-motors-and-bad-logic/1429679 "2026-02-08T03:23:03Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Duinojr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/duinojr/32/1258803_2.png) [@Duinojr](https://forum.arduino.cc/u/Duinojr)\
**Post date:** [February 8, 2026, 3:23am UTC](https://forum.arduino.cc/t/jittering-motors-and-bad-logic/1429679/1 "2026-02-08T03:23:03Z")

</div>

Hello, I have made the following code, trying to move 2 servo motors at different speeds so they sweep at the same time. However I have noticed that they can get a bit jittery, so I was wondering if anyone knew how I could fix this, by increasing the speed or the wait time ( I actually have no clue since I've never worked with motors before, if its useful to know, they are sg90 motors ). Any help is appreciated 🙂

```arduino
#include <Servo.h>
Servo servoMotor; 
Servo servoMotor2;
unsigned long previousMillis = 0;  
const long interval = 40000;
int initialPosMotor = 90; 
int initialPosMotor2 = 90;
int maxPosMotor = 100;
int maxPosMotor2 = 150;
int minPosMotor = 90;
int minPosMotor2 = 90;

bool goingBack = false;

void setup() {
  Serial.begin(9600);
  servoMotor.attach(7); 
  servoMotor2.attach(8);
}

void loop() {
   unsigned long currentMillis = micros();
   // Check if enough time has passed
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
 	int speed = 1;
	int x = maxPosMotor - minPosMotor;
    int y = maxPosMotor2 - minPosMotor2;
    int ratio = abs(y) / abs(x); // ratio for second motor
       //initialPosMotor += speed;
    
    if (!goingBack) {
      servoMotor.write(initialPosMotor);
      servoMotor2.write(initialPosMotor2);

      initialPosMotor += speed;
      initialPosMotor2 += ratio;
      Serial.println("This is the ratio");
      Serial.println(ratio);

      if (initialPosMotor >= maxPosMotor) {
        goingBack = true;
      }
    }

    else {
      servoMotor.write(initialPosMotor);
      servoMotor2.write(initialPosMotor2);

      initialPosMotor -= speed;
      initialPosMotor2 -= ratio;

      if (initialPosMotor <= minPosMotor) {
        goingBack = false;
      }
    }
	Serial.println("This is the first motor");
    Serial.println(initialPosMotor);
    Serial.println("This is the second motor");
    Serial.println(initialPosMotor2);
  }
}

```

---

<div class="post-metadata">

**Author:** ![MicroBahner](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/microbahner/32/238923_2.png) [@MicroBahner](https://forum.arduino.cc/u/MicroBahner)\
**Post date:** [February 8, 2026, 8:31am UTC](https://forum.arduino.cc/t/jittering-motors-and-bad-logic/1429679/3 "2026-02-08T08:31:51Z")

</div>

The main problem in your sketch are that many Serial.prints() in your loop - especially at 9600 Baud only. These prints slow down your loop significantly, what makes the jitter.  
At least use higher baud - e.g. 115200. Or delete the prints at all.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [February 8, 2026, 9:36am UTC](https://forum.arduino.cc/t/jittering-motors-and-bad-logic/1429679/4 "2026-02-08T09:36:17Z")

</div>

you could consider a time based interpolation

```arduino
#include <Servo.h>

const byte motorPin1 = 7;
const byte motorPin2 = 8;

Servo servoMotor;
Servo servoMotor2;

const float minPosMotor = 90.0;
const float maxPosMotor = 100.0;
const float minPosMotor2 = 90.0;
const float maxPosMotor2 = 150.0;

const unsigned long forwardTime = 400000; // 400 ms in microseconds
const unsigned long cycleTime = forwardTime * 2; // forward + backward

float mapf(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

void setup() {
  servoMotor.attach(motorPin1);
  servoMotor2.attach(motorPin2);
}

void loop() {
  unsigned long t = micros() % cycleTime;
  float fraction = t < forwardTime
                   ? mapf(t, 0, forwardTime, 0.0, 1.0)
                   : mapf(t, forwardTime, cycleTime, 1.0, 0.0);

  float posMotor = mapf(fraction, 0.0, 1.0, minPosMotor, maxPosMotor);
  float posMotor2 = mapf(fraction, 0.0, 1.0, minPosMotor2, maxPosMotor2);

  servoMotor.write(posMotor);
  servoMotor2.write(posMotor2);
}

```

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [February 8, 2026, 11:42am UTC](https://forum.arduino.cc/t/jittering-motors-and-bad-logic/1429679/5 "2026-02-08T11:42:36Z")

</div>

Hi, @Duinojr

What are you using for a power supply for the servos?

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?  
Hand drawn and photographed is perfectly acceptable.  
Please include ALL hardware, power supplies, component names and pin labels.

Tom.... 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![MicroBahner](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/microbahner/32/238923_2.png) [@MicroBahner](https://forum.arduino.cc/u/MicroBahner)\
**Post date:** [February 8, 2026, 11:55am UTC](https://forum.arduino.cc/t/jittering-motors-and-bad-logic/1429679/6 "2026-02-08T11:55:24Z")

</div>

> [@J-M-L](#):
>
> a time based interpolation

That won't really help, because servo.h can't handle float. If you need a better resolution, you need to use µs instead of angle for the pulslength.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [February 8, 2026, 2:12pm UTC](https://forum.arduino.cc/t/jittering-motors-and-bad-logic/1429679/7 "2026-02-08T14:12:13Z")

</div>

Sure but the value will be truncated and it will (or should) work OK anyway

➜ test it here

> **[2 servos with linear interpolation time based - Wokwi ESP32, STM32, Arduino...](https://wokwi.com/projects/455405072258153473)**
>
> Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!

---

<div class="post-metadata">

**Author:** ![Duinojr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/duinojr/32/1258803_2.png) [@Duinojr](https://forum.arduino.cc/u/Duinojr)\
**Post date:** [February 8, 2026, 3:09pm UTC](https://forum.arduino.cc/t/jittering-motors-and-bad-logic/1429679/8 "2026-02-08T15:09:42Z")

</div>

humm sooo for this project in particular I am also using a light sensor and 4 RGB LED so the only circuit I have is with all of those, so here is the circuit, ( It's a bit messy and the RGBs dont do the right color, but everything else is working, sorry )

 ![Capture d’écran 2026-02-08 100119](https://europe1.discourse-cdn.com/arduino/original/4X/1/b/a/1ba00cdefc5b5c3b061c266e21008b3e64f4548c.jpeg)

( I dont know if this is readable 🫠 )

 ![Capture d’écran 2026-02-08 100532](https://europe1.discourse-cdn.com/arduino/original/4X/b/6/7/b67107cfb178ac19d6a1e4efedbdfa0ef3e28ae5.png)

( No idea if this picture helps but I can’t take a better one 😕 )

 ![Capture d’écran 2026-02-08 100813](https://europe1.discourse-cdn.com/arduino/original/4X/5/b/d/5bdd685c56241c53fdc2e6830344df250c0dbf6b.jpeg)

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [February 8, 2026, 3:33pm UTC](https://forum.arduino.cc/t/jittering-motors-and-bad-logic/1429679/9 "2026-02-08T15:33:24Z")

</div>

> [@Duinojr](#):
>
> I am also using a light sensor and 4 RGB LED so the only circuit I have is with all of those, so here is the circuit,

So where ary getting the power from for the servos?  
The UNO USB?

Tom.... 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![Duinojr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/duinojr/32/1258803_2.png) [@Duinojr](https://forum.arduino.cc/u/Duinojr)\
**Post date:** [February 8, 2026, 4:24pm UTC](https://forum.arduino.cc/t/jittering-motors-and-bad-logic/1429679/10 "2026-02-08T16:24:21Z")

</div>

> [@TomGeorge](#):
>
> So where ary getting the power from for the servos?  
> The UNO USB?

uh well right now I am indeed, I connected them to the 5v 😅 , but when I just used a simple code to sweep them, it worked well, so I did not move them

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [February 8, 2026, 10:47pm UTC](https://forum.arduino.cc/t/jittering-motors-and-bad-logic/1429679/11 "2026-02-08T22:47:13Z")

</div>

Hi, @Duinojr

> [@Duinojr](#):
>
> . However I have noticed that they can get a bit jittery,

Driving two let alone one servo from the UNO is not recommended from the PC supply, you need a separate supply, possibly 3 x AA batteries to test.

Tom.... 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 7, 2026, 10:47pm UTC](https://forum.arduino.cc/t/jittering-motors-and-bad-logic/1429679/12 "2026-08-07T22:47:21Z")

</div>

Closed on August 7, 2026, 10:47pm UTC

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