How do i use analogWrite to cause a DC brush motor to brake

So for a lab class i have to connect a dc brush motor to arduino and make it turn left, right and brake using a H bridge comprised of 4 transistors. Wiring the components i understand and i understand how to make the motor spin left and right. The problem i have is when i use the different pins for braking it stops the motor completely. Here is the code i used for controlling it.

void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop()
{
//motor spins from left to right
analogWrite(3,200);
analogWrite(9,200);
//delay 10 seconds
delay(10000);
//should brake in the following
analogWrite(3,200);
analogWrite(10,200);
//delay 10 seconds
delay(10000);
//motor spins from right to left
analogWrite(10,200);
analogWrite(11,200);
}

kdeng:
when i use the different pins for braking it stops the motor completely.

Is that not what you want? To stop the motor when you break?


One thing you should be doing is to stop the pwm on one set of pins before enabling it on the other.

Take this H-Bridge:

   +--Vcc--+
A [         ] B
   +--(M)--+
C [         ] D
   +--GND--+

If ONLY A and D are on at the same time, then the motor spins one way. If ONLY B and C are on at the same time the motor spins the other way.
If A and B (or C and D) are on, the motor will break due to decaying magnetic fields in the inductance of the motor.
If none are on, the motor will coast (slow down to a stop due to friction alone).
However...
What happens If A and C are on at the same time? The same is true for if B and D are on at the same time.

To prevent this from happening, when you turn A on, you need to make sure you turn C off. When you turn B on you need to turn D off. When you turn D on, you need to turn B off. When you turn C on, you need to turn A off.

Depending on your circuit, it can be as easy as: analogWrite(,0). But it depends on how your motor is wired.

well it does stop at the point where it should brake but the thing is it suppose to change direction after the delay of ten seconds and it doesnt start up after ten seconds in the other direction.

I wired my circuit following this diagram. The pins go to the arduino pin # and the gnd goes to the gnd in arduino and and gnd with the power supply and Vs comes from the power supply box. The motor is is the middle connect to emitter of pin 3, pin 10, and collector of pin 11, pin 9.

lab #3 sample circuit.jpg

Proper H-bridge circuit.

The problem i have is when i use the different pins for braking it stops the motor completely.

How is this a problem ?

You can't use analogue write on a h bridge like that. This is because pairs of pins only are in phase with each other, you do not get phase intergrety over all the pins.
You need to use a proper h bridge and use the PWM to either enable the bridge for a coasting stop or use PWM on both pins and then put the pins so that both ends of the motor are connected to the same rail for a breaking stop.

kdeng:
well it does stop at the point where it should brake but the thing is it suppose to change direction after the delay of ten seconds and it doesnt start up after ten seconds in the other direction.

There is no delay after you set it to spin right. That means it will start spinning right, but then in a couple of microseconds it is commanded to spin left when the loop starts again.