Arduino mega 2 DC motors and a Servo


For my project I need the following motors:
2 DC motors (using L298N)
1 x 360 servo
2 or 3 stepper motors (using CNC v3 shield)

For now I’m testing the DC motors and servo. I tested the DC motors and servo separately and they worked fine! But when I try to control both, it doesn’t work anymore.

Please see my code below:

#include <Servo.h>

//Setup Motor Pins 
#define in1 44 //PWM pin.
#define in2 45 //PWM pin.

Servo myservo;
int lowSpeed = 2; 
int maxSpeed = 250; 

void setup() {
  //set the pins for output
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);

void loop() {
  analogWrite(in1, maxSpeed);
  analogWrite(in2, maxSpeed);

Because I’m also using the CNC shield, I only have 44/45/46 left.

Please advice on how I can drive both,

Need to see a wiring diagram.

I’m using pin 44 45 (to drive DC motors) 46 (for the servo)

DC motors are connected to a 12V power supply
Servo is connected to a 5V power supply

Separately it’s working, but together it’s not. (in both cases the wiring is the same, only the servo is programmatically disabled)

It’s not a diagram, but does it give enough insights?

All grounds connected together? What pins on the L298N are 44 and 45 connected to and what are the other L298N input pins connected to?

What are the EXACT symptoms of "together it's not working"? Does nothing move? Do the motors run but the servo doesn't? Something else? Just saying "not working" tells us absolutely nothing.