L298N Brake state

I'm trying to write a simple program that runs a 6v dc motor when a button is pushed and puts the motor in a brake state when the button is released. The program works as intended to run the motor when the button is pressed but when it's released, the motor still feels as if it's not in a brake state.

My understanding of brake state is the high torsional resistance felt on the dc motor when you connect the two leads of the dc motor together. I think I'm sending the right info to the L298N driver but maybe I'm incorrect in my understanding on how the driver is supposed to work.

I thought that making the two inputs receive the same signal as well as making the Enable high would essentially short the connection and put the dc motor into what I think is brake state.

L298N info

Stopping
To remove power from the motors, simply set ENA=LOW for Motor A and ENB=LOW for Motor B. This will result in the motors stopping slowly and naturally from friction. To perform a quick braking operation, set ENA=LOW, IN1=LOW and IN2=LOW for Motor A and ENB=LOW, IN3=LOW and IN4=LOW for Motor B. The motors will come to an instant stop. Here are some handy tables to show the various modes of operation.

Motor Driver Truth Tables
Here are some handy tables to show the various modes of operation.

Motor A truth table
ENA	IN1	IN2	Description
0	N/A	N/A	Motor A is off
1	0	0	Motor A is stopped (brakes)
1	0	1	Motor A is on and turning backwards
1	1	0	Motor A is on and turning forwards
1	1	1	Motor A is stopped (brakes)

My code

const byte BUTTON = 13;

int buttonstate1 = 0;

int motSPD1 = 11;
int motDIR1 = 10;
int motDIR2 = 9;



void setup() {
  pinMode(BUTTON, INPUT_PULLUP);
  pinMode(motSPD1, OUTPUT);
  pinMode(motDIR1, OUTPUT);
  pinMode(motDIR2, OUTPUT);


}

void loop() {
  buttonstate1 = digitalRead(BUTTON);
  if (buttonstate1 == LOW) {
    analogWrite(motSPD1, 255);
    digitalWrite(motDIR1, HIGH);
    digitalWrite(motDIR2, LOW);
  }
  else {
    digitalWrite(motSPD1, HIGH); //Is this line correct or even necessary?
    digitalWrite(motDIR1, HIGH);
    digitalWrite(motDIR2, HIGH);
  }
}

Push comes to shove, I can always add a relay that connects the two leads during the else state but I'm hoping that I find a better solution.

Vampyrewolf:
Push comes to shove, I can always add a relay that connects the two leads during the else state but I'm hoping that I find a better solution.

You might want a double-throw relay so it can disconnect the motor from the driver before shorting across the leads. If not, your driver would go BANG if you accidentally shorted the motor leads while the driver was driving the motor. :frowning:

The brake effect is relative to the motor's RPM, zero at zero RPM and increases with increasing RPM, lots of braking torque @ 5000 RPM, but declining @ lower speeds and 0 @ 0 speed. The motor has to be moving to create a moving magnetic field for the stator magnets to drag against.
I think your understanding of "brake state" applies to step motors.

The "braking" (Enable, In1 and IN2 all HIGH) only slows the DC motor more quickly than leaving it free-running (Enable LOW). It doesn't prevent the stopped motor from being manually turned. To prevent motion when the motor is stopped you will need an external brake, like a magnetic clutch that can connect the shaft to a stationary chassis.

It is frustrating that this motor driver doesn't have that function...i use it in a lot of projects :- D

My work around was to use your suggestion and employ a small double-throw relay to "short" the motor terminals together in the off state.

Are their any motor drivers out there that have something like this already embedded?

The L298 DOES have that capability, if you want to hold the motor at one position, you will need a position encoder on the motor and a PID loop to force it back to that position if you try to turn it away from that position. You are confusing braking and holding.

I don't need to hold the motor at a specific position, I just want to prevent it from turning further when it's deactivated. I should have been more clear. The function I'm looking for isn't fancy or precise. I just need the motor to be held wherever it is when it's stopped, regardless of degrees of position.

Hope that clears it up.

Having a motor controller in brake mode does not prevent further rotation.
It resists rotation, with more resistance the faster the motor is turning.
If you want to prevent any rotation, you need a physical brake.
I guess another solution would be to use enough gearing, like with a work gear, that will make it very difficult to rotate the motor by applying force to the output shaft.

There is no way to make a DC motor hold a specific position using a simple H-bridge (or relay), without position feedback and ACTIVE CONTROL of the H-bridge. There is NOTHING the H-bridge itself can do to hold a static position. As already explained, the "brake" function of an H-Bridge works ONLY when the motor is spinning. That is not a limitation of the L298N, it is a limitation of DC motors.

Regards,
Ray L.