Simple Servo & motor control - odd behavior

Hey guys, been bashing my head against a wall with this… any suggestions would be most welcome.

I have been working on a robotics project, it has a few sensors, 2 servos and 2 motors (parts list at the bottom). I’m just focusing on the servos and motors. I have a strange issue that will only present itself when the servo code and motor code are present together and only seems to effect one motor and one servo in particular.

here is the code first of all… it’s very basic to eliminate anything from my main code bing the issue I just added what was needed to see the problem.

// ===== MOTOR CONTROL PINS =====

// Right Hand Motor pins of W-3
int enA = 9;
int in1 = 8; //HIGH for backward motion
int in2 = 7; //HIGH for forward motion

// Left Hand Motor pins of W-3
int enB = 3;
int in3 = 5; //HIGH for backward motion
int in4 = 4; //HIGH for forward motion

// ===== SERVO PINOUT =====
const int PitchServo = A1; //Blue = Yaw servo = A1
const int YawServo = A2; //Orange = Pitch Servo = A2

#include <Servo.h>

Servo Yaw;
Servo Pitch;

void setup() {
  // put your setup code here, to run once:
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  pinMode(YawServo, OUTPUT);
  pinMode(PitchServo, OUTPUT);


void loop()

void servoTest()
  for (int i = 0; i <180;i++)

void motorTest()

  for (int i = 0; i < 255; i++)
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    analogWrite(enA, i); // this line in particular seems to cause the pitch servo to randomly twitch and has no effect on the speed of the motors

    digitalWrite(in4, HIGH);
    digitalWrite(in3, LOW);
    analogWrite(enB, i);

There are 2 servos connected to the Arduino on the analog pins (ran out of digital pins). Pitch is set to A1 and Yaw to A2. it is the pitch servo that twitches it seems. There is also an L298n motor controller powered by a 12v power supply. The servos and Arduino uno are powered by a DC DC buck convertor from the 12v battery which has been stepped down to 5v. The motor controller is mapped to the digital pins on the Arduino as described in the code.

Here is where things go wrong. It will run the servoTest() code perfectly but when it then runs the motorTest() code, only one motor (in3, in4, enB) will work and the servo will jitter. If I remove the line referencing the enA pin, no jitter. Likewise if I detach the Servos, both motors will run as expected.

I have tested this with just one servo and just the motor in question and seem to get the same output - both work perfectly on their own but not together.

I can make a circuit diagram if needed.

Parts in the circuit:

2 X MG90S servos (pitch and yaw)
2 X 12v motors
L298n Motor Controller
Arduino Uno R3
LM2596S DC-DC Buck converter

also in the circuit but not being used in this code (although powered and connected to the Arduino)
HC-05 Bluetooth Module
HC-SR04 Ultrasonic Sensor
2 3mm LEDs

Any thoughts on what I might be doing wrong would be greatly appreciated.


From the Servo reference page - Servo - Arduino Reference

"On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins."


Most likely Servo.h is using a timer that is also used for PWM, this must be causing the glitch. As Slipstick is saying, so you will need to move the motor pin to a different PWM channel (eg different pin)

Oh guys, I knew it must be something like that.. the problem is there is so much reference, unless you know where to look it's impossible to find. I will try a different pin. Thank you both so much, you have no idea how crazy this has been making me!

EDIT: Just moved the PWM for the motor to Pin 6 and all is well. Uploaded the original code and it's working as expected now! I originally thought it might be a current limit but when I checked the specs of the buck converter I knew it must either be a wiring issue or some kind of pin conflict.

Thank you so much, i really appreciate it! :slight_smile:

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