how to make brakes with geared motors that don't have brakes

hello everyone.
I spent a lot of time searching for a solution to my problem which is I have a four-wheel robot that able to move in different directions, however,I wanted to make my robot stop for saying if an obstacle 10cm away, I tried different codes depends on my simple logic like reverse the direction of the movement but it didn't help me!
the problem when I cut the power off of my robot motors

digitalWrite(leftPin,LOW);
digitalWrite(rightPin,LOW);
analogWrite(left,0);
analogWrite(right,0);

the robot keep going and hit the wall!I know it is because the weight of the robot (>1Kg) may affect but I can't solve this.
any help will be appreciated.

Use the brake function of the motor driver.

jremington:
Use the brake function of the motor driver.

I am using L298N motor drive! does it able to make brakes to my motors?

Yes, see Figure 6 in the L298N data sheet.

jremington:
Yes, see Figure 6 in the

thank you, i found the solution and it is working as you lead me to do other searches
FOR FUTURE VISITS the following link the solution
L298 - Fast stop and free running stop - Motors, Mechanics, Power and CNC - Arduino Forum.

Like a form of dynamic brakes? I’ve never used the module as listed above but I have experience with electric braking. You will need a “load” for when you are in braking situation. when in dynamic braking it turns the motor into a generator and that generation needs load. you can use it to charge batteries if needed.

what type of motor are you using? If its a two wire then I don't think a dynamic braking will work, However, you could pulse a reverse voltage to the motor.
what I mean by pulsing would be something slower than PWM... a function with a digitalWrite, HIGH; delay(50); and a digitalWrite, LOW; delay(variable);

If its a two wire then I don't think a dynamic braking will work

The motor driver effectively short circuits the motor terminals to produce a reasonable braking action.

Gotcha. I’ve only experienced motor braking with 4 wire DC motors