Arduino resetting, causing motors and servos to behave erratically

I find myself puzzling over a project that just; well, isn’t working. Or at least it works sporadically as and when it feels like it. I have two 12v DC motors hooked up to an L298N (powered by 12v DC Supply). Its 5v in/out is then powering the Arduino and two micro servos. The speed of the motors is set by a 10k Potentiometer, and the servos controlled by a thumbstick.

Individually the two systems seem to function well, the potentiometer controls the motors, the thumbstick the servos, without any issue, but when I use a sketch that has both systems in place they all seem to behave with lives of their own, the potentiometer seems to control the motors but they don't come on together, the servos sit and chatter to themselves without any input from the thumbstick, until the potentiometer is turned up to full, then suddenly everything seems to behave perfectly fine. Motors and servos running together, until then the Arduino will reset and do so more or less continuously.

At first I thought this might be a current issue, so I upped it from 2A to 5A, but that hasn’t done anything to help at all and I can’t see any reason why the code would be doing this.

I have also tried isolating the servos and Arduino from the 12v and giving them their own 5v power source instead, with no change, and even powering the Arduino, servos and motors all separately, still with no luck

It has been suggested that perhaps there is a short somewhere, the potentiometer might need to be a higher resistance and even that the breadboard may not be able to supply enough current, and that I should just wire it up and see if it works then. So I'd love some other input is there something obvious I should be doing first that I haven't?

Any insight would be greatly appreciated.

#include "Servo.h"

//DC MOTOR PINS 
#define enA 9
#define enB 8
#define in1 5
#define in2 4
#define in3 3
#define in4 2

//THUMB STICK
const int thumbX = A1;
const int thumbY = A2;
//const int thumbSW = 12;

//MICRO SERVOS
int left = 11;
int right = 10;

Servo leftServo;
Servo rightServo;

const int MaxReadings = 10;

int Xreadings[MaxReadings];
int XreadIndex = 0;
int Xtotal = 0;
int X_Pos = 0;

int Yreadings[MaxReadings];
int YreadIndex = 0;
int Ytotal = 0;
int Y_Pos = 0;

int Servo_Pos = 0, Pos = 0;

void Smoother(int x_pin, int y_pin)
{
  Xtotal = Xtotal - Xreadings[XreadIndex];
  Ytotal = Ytotal - Yreadings[YreadIndex];
  delay(1);
  Xreadings[XreadIndex] = analogRead(x_pin);
  Yreadings[YreadIndex] = analogRead(y_pin);
  delay(1);
  Xtotal = Xtotal + Xreadings[XreadIndex];
  Ytotal = Ytotal + Yreadings[YreadIndex];
  delay(1);
  XreadIndex = XreadIndex + 1;
  YreadIndex = YreadIndex + 1;
  if (XreadIndex >= MaxReadings) XreadIndex = 0;
  if (YreadIndex >= MaxReadings) YreadIndex = 0;
  delay(1);
  X_Pos = Xtotal / MaxReadings;
  Y_Pos = Ytotal / MaxReadings;

}


void setup() {
//Enable Motors
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
//Motor A
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
//Motor B
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
//Set Rotation
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  
  Serial.begin(9600);
  

  leftServo.attach(left);
  rightServo.attach(right);
  
  for (int i = 0; i < MaxReadings; i++) {
    Xreadings[i] = 0;
    Yreadings[i] = 0;
}
}

void loop() 
{
//Control speed of motors using potentiometer
  int potValue = analogRead(A0);                      // Read potentiometer value
  int pwmOutput = map(potValue, 0, 1023, 0 , 255);    // Map value from 0 to 255
  analogWrite(enA, pwmOutput);                        // Send PWM signal to L298N Enable pin
  analogWrite(enB, pwmOutput);                        // Send PWM signal to L298N Enable pin

// Control Servo motion

  Smoother(thumbX, thumbY);
  Servo_Pos = map(X_Pos, 0, 1023, 0, 180);
  leftServo.write(Servo_Pos);
  rightServo.write(Servo_Pos);

  delay(20);


}

Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

It appears your servos are are being powered by the Arduino.

If so, your Arduino is not a power supply.

Never power inductive loads with the Arduino.

Continuing doing so will destroy your Arduino.

Hi there, I did upload a photo showing the wiring as clear as I can, please confirm it is showing for you?
The power is all coming from the 5v in/out from the L298N as described above.

Does the 5v regulator on the motor controller board get hot ?

The power leads for the Arduino, servos and motor driver should be connect in a star configuration.

Daisychain vs Star

Is there any drop in voltage on the driver power terminals when the two motors are running ?

The photo is useless as a circuit diagram, since no connections or pins are labeled. But the photo does show us the main problems:

  1. The Arduino is not a power supply. Motors and servos need to be separately powered, but all grounds need to be connected.

  2. Breadboards are for temporary experiments with low power logic circuitry, and cannot handle motor or servo currents. The tracks will burn.

You need to solder connections to motors and motor power supplies. These protoboard PCBs work well for that.

As far as I am aware I am not using the Arduino to power anything; it, however is being powered from the 5v line from the L298N board.

I did this quick diagram to hopefully clear up any confusion the photo may have caused.

Edit: I messed up the diagram, the Potentiometer IS connected to the Ground, unlike in my picture.

What is the motor controller power supply voltage ?

12V




What is a resistance of these motors ?

That’s a good a good schematic.

  • You are wiring the components in a daisy chain configuration, use star.
  • Keep wires short.
  • does the 5v regulator get hot when everything is running ?

Does your pin8 support PWM ?
check this

Thank you, I hope it has helped.
I can't seem to find any resources that explain Star Configuration vs Daisy Chain?
Wires will be shortened where I can, for sure.
Nothing gets hot when it is running, but then again it probably resets before it has a chance!

Another thing I don't know I'm afraid, there is no information in the Manufactures specs, and I wouldn't know where to begin working it out myself.

I assume so? Might it not? Pin 8 is currently allocated PWM for Motor B and Pin 9 for Motor A

Thank you, I have adjusted that in the sketch, PWM for Motor B is now assigned to pin 6.

Wire the GND and 5v power distribution as in the lower portion of the image.


Edit

  • If you have access to a DMM, measure the resistance of the motors.

  • What is the current rating of the 12V supply.

Reading on a DMM across the Motor is 6 Ohms

I have both a 12v 2A and a 12v 5A power supply, both have the same issues detailed above.

A 12V, 6 ohm motor needs 12V ÷ 6R = 2 Amps maximum (locked rotor).

You have 2 motors, that’s 4 Amps.

Your L298N motor diver can handle 2A absolute maximum.

Look on Polulu for more up to date options.

There are two servos in your diagram, connected to the Arduino 5V. The L298 module is not intended to power servos through its 5V pin, either.

It is not a good idea to connect motors to the 5V pin the Arduino, regardless of where the 5V power comes from.

Their Arduino and 2 servos get 5v from the voltage 5v regulator on L298N motor driver board.

Which is why the Arduino is resetting.

The servos need a 4.8-6V power supply capable of providing 2 Amperes for small servos like SG-90, 5 Amperes for larger ones like the MG996R.

Definitely is not the desirable way of wiring this. :woozy_face:

So would I need therefore to have a separate power source for each part? 12V 5A for the motors, a separate 5v for the servos and a third one, like a 9v battery for the Arduino?

I had tried basically that in my attempting to troubleshoot before I posted, but it didn't seem to help the problem, but what I am understanding from replies to this post is there is a lot of other problems that need addressing.