Arduino L298N problem

I am using an arduino uno and two DC motors to control some drawers. There are 3 'templates' of moving the drawers, based on inputs received from relays. The motors are controlled with a L298N module and powered by a 12v power supply. The arduino is powered by a different supply via USB. It usually works the way it should, except that sometimes (maybe 1 in 20) the arduino 'crashes' (when receving input1 or input2) and the motor keeps spinning in one direction and further inputs won't work. I get a "C" in serial monitor when this happens. When controlling dc motors, i need to take the jumpers off and connect in the place 5v from arduino. Could the module be sending back some noise on the 5v wire and cause the arduino to crash? Any idea what else could cause this?

Here is the code:

//Motor A
const int motorPin1  = 9;  // Pin 14 of L293
const int motorPin2  = 10;  // Pin 10 of L293
//Motor B
const int motorPin3  = 6; // Pin  7 of L293
#define input1 3
#define input2 2
#define input3 7
#define senzor 12
#define lock 4


void setup() {
  
Serial.begin(9600);
    pinMode(motorPin1, OUTPUT);
    pinMode(motorPin2, OUTPUT);
    pinMode(motorPin3, OUTPUT);
    pinMode(input1, INPUT_PULLUP);
    pinMode(input2, INPUT_PULLUP);
    pinMode(input3, INPUT_PULLUP);
    pinMode(lock, OUTPUT);
    pinMode(senzor, INPUT_PULLUP);
    digitalWrite(lock, LOW);
}

void loop() {

  
    if(digitalRead(input1)==0&&digitalRead(senzor)==0&&digitalRead(input2)==1)
    {
      Serial.println("open drawer");
                 digitalWrite(lock, HIGH);
      analogWrite(motorPin1, 215);
      delay(1600);
      analogWrite(motorPin1, 0);
      digitalWrite(lock, LOW);
    }

   
    else if(digitalRead(input2)==0&&digitalRead(senzor)==1&&digitalRead(input1)==1)
{
   Serial.println("close drawer");
      digitalWrite(lock, LOW);
      analogWrite(motorPin2, 200);
      delay(1600);
      analogWrite(motorPin2, 0);
}

   else if(digitalRead(input3)==0&&digitalRead(input1)==1)
    {
      Serial.println("vibrating drawer");
    analogWrite(motorPin3, 255);
          delay(1600);
    analogWrite(motorPin3, 220);
    delay(1500);
            analogWrite(motorPin3, 255);
    delay(1000);      
      analogWrite(motorPin3, 0);
    
   }
}

The sensor tells me when the drawer is closed and the lock is locking the drawer when it's closed.

Post a link to the actual controller module, not a Google search link. Also post your circuit schematic. It's possible you have noise on the power lines from the motors.

http://www.instructables.com/id/Arduino-Modules-L298N-Dual-H-Bridge-Motor-Controll/

https://imgur.com/a/cCBeK

MK1888: Post a link to the actual controller module, not a Google search link. Also post your circuit schematic. It's possible you have noise on the power lines from the motors.

Any idea of a possible solution to this? Diodes between arduino and module?

It's common to have capacitors (at least one) on the motors to handle noise, like the image shown near the bottom of this page:

https://learn.adafruit.com/adafruit-motor-shield/faq

Also, make sure everything is sharing a common ground.

Also, try some experimenting. Disconnect some things to get a minimum setup that causes the problem.

Not that it's to do with your problem, but there's (at least) one huge error in that Instructable.

That author says this of the 298:

EnA: Enables PWM signal for Motor A EnB: Enables PWM signal for Motor B Specifications:

And on the pic of the wiring says:

The red wirings are used to control DC motors by PMW [sic], if not used this can be removed

In fact, those EnA/B pins do exactly as their abbreviation implies: they enable the outputs A and B.

The 298 datasheet says that a L on either Enable, will disable the respective bridge.

Even if you're not using PWM (or PMW ;) ), you need to enable one or both of the bridges for it to work.

(He also refers to the In pins as enables....)

Hi, OPs Fritzy.. |500x276

Tom.... :o :o

Hi, What are your motors, spec/data please.

A 298 is not the best to control motors with, because you loose up to 2V across them, which means the 298 gets very warm when driven.

That module is only good to 2A.

Tom... :)

TomGeorge:
Hi,
What are your motors, spec/data please.

A 298 is not the best to control motors with, because you loose up to 2V across them, which means the 298 gets very warm when driven.

That module is only good to 2A.

Tom… :slight_smile:

I’m not sure about specs, it’s a dc motor with reduction, and it needs less than 2A for sure, and i’m never using both motors simultaneously.
Here’s a photo of the motor:
https://magazin.unda.tech/motoare-dc/763-motor-reductor-d37-5-300-rpm-12v.html

Since the problem doesn’t occur on the second motor (which spins in only one direction) i’m guessing it has something to do with changing the motor direction, maybe creating an electricity spike when reversing + and gnd.

Hi,
Do you STOP between changing directions?
OPs motor.

What are you using as power supplies?

Tom… :slight_smile: