Author Options:

Control Logic with PWM? Answered

I am currently working on a robotics project, and need a bit of help. I have this H bridge, and would need to control it via PWM. Speed control isn't necessarily what i need, though i wouldn't mind. (In4 i should have just bought an ESC) The whole bot is controlled by PWM (All servos) except for the two DC motors that i have on it -- One for forward and backward, and another for rotating an arm. How would i control the H Bridge with PWM? Which pins would i connect what to? 


The Enable pins are what tell the chip to deliver current; The Inputs only tell the chip what direction to go in.

I see two jumpers on the "Enable" pins. while these are in place all you need to do is pull the "Input" pins you need to 5V (Or possibly ground if it doesn't have an converter chip, depends on your board model)

EG. If you wanted to move the bot forward and back, you would attach two signal wires from the servos to In-1 and In-2. drive the servo to max current and it should work nicely. **Plz remember to connect a ground from the controller to the ground of the 298 board And use a inline 100-Ohm resistor on each servo to limit the current to ~40ma** Provide current to In-1 and the motor will turn one way. provide current to In-2 and it will turn in the opposite direction. HOWEVER do both at once and you will damage your chip ! be sure to turn the other off first :)

**A safe guard I like is use a single signal wire to drive a...(Transistor, Op-Amp, Digital logic IC... Ect, some sort of a switch that uses a small amount of current).

Pull "In-1" to 5V with a 10K resistor to the 5V rail. This will set it in the what I'm calling the forward direction. Then attach your signal wire from you controller to "In-2," again with a 100-Ohm resistor. When activated, this will tell the chip to spin the motor backwards and also drive the transistor, that will pull "In-1" low (and not kill your motor)

And hey... You will Save Two Servo channels, Bounus !

To Enable PWM to control the current, and final speed, to the motor you need to remove the jumpers on Enable A for this first output or B for the second. one of the two pins will be 5v or ground (Depending on board model) You need to connect a 5V pulsed signal from your controller to the other pin, leading back to the 298.

As the PWM signal goes up and down, the chip will only deliver current while the pin is pulled High (or low as mentioned earlier) This will control the Current (Thus Speed) while the "In-X" pins controll the direction.

*Catches Breath...

Hopefully this helps, Reply if you need anything clarified :)

Maybe I missed something in the PDF. I see a PDF table that points out (Fast_Motor_Stop) ie dynamic_braking occurs whenever the In1=In2 high or low !

That leaves the enable pin as the only viable PWM input ?

At that point the enable pin acts like a brake pedal.

setting In-1 and 2 to High / low sets it in the dynamic braking mode, Then a PWM signal to Enable will apply more or less resistance to the motor :)

Thanks for the in-depth answer! I more or less figured a lot of this out through google searching, so thanks for clarifying! I think i will abandon speed control, too much work and inputs/outputs from the controller. Could you explain this bit

"Pull "In-1" to 5V with a 10K resistor to the 5V rail. This will set it in the what I'm calling the forward direction. Then attach your signal wire from you controller to "In-2," again with a 100-Ohm resistor. When activated, this will tell the chip to spin the motor backwards and also drive the transistor, that will pull "In-1" low (and not kill your motor)"

How would this be done? can i control one motor with two inputs, and a few resistors, but still have it work in both directions, plus have it not move at all?

The single wire (for selecting direction) that I suggested will only work if you You also control the speed as well.

The state of the "Enable" Pins are the master switches that will turn Each Half of the H bridge on or off. They can be triggered by a signal from your controller or pulled high from that jumper I mentioned.

The state of the "In-x" Pins will only tell it what direction to turn in.

So Like Steve suggested you can use each of the four input channels for simple forward and back (maybe pulsed too, it's not designed to do it like that (For this application. using PWM to drive the "In-x" Pins is usually reserved for a stepper motor) but , in theory it should work) Even without controlling the current you will still need 2 control pins.

If you try the Single wire here if will spin one way or the other, No Stopping.

The only reason My solution works is because I'm using one servo to the "enable" pin for throttle, and the second one for Direction :)

If your controller has simple on/off channels,Use them to change the direction. This will help free up two valuable PWM Channels!

PWM BTW IS logic. Its what you do with it afterwards that makes it into an analogue level


3 years ago

H-Bridge info from PDF including PMW pin and typical 25KHz frequency, some ICs may be able to run 40KHz !

Keep in mind PWM has a vary narrow pulse at high speed and crawl speed.

That pulse narrow width may exceed the fq spec and may be ignored by the IC.


3 years ago


Yup, I just had a look at the data sheet, and the internal schematic uses some logic very similar, probably identical to steve's description. There are 2 inputs per channel, of which you can drive directly, which will drive the 4 push-pull circuits. Your motor(s) will bridge between 2 of those outputs, and the corresponding pins will be driven with to be either both HIGH, (the motor will be OFF and floating at +Vcc). both LOW (motor will also be OFF, but floating on the low side) or with opposing signals. (motor will spin in some direction, because that difference between the inputs corresponds to a difference in the output voltage difference across the motor)

I would not feed those inputs with analog voltages, just digital, at the input ratings. It would be a bad idea to not fully saturate those transistors. So to get speed control, have one pin either high or low, and the other driven with PWM. Hope that makes sense. You can then set up a sophisticated feedback loop with a tachometer or something to get proper regulated speed control, and you can even make it into effectively a huge servo motor by simply adding a gear box to lower the RPM, and having a potentiometer feedback from the output of the gear box, and have the analog value be maintained with a microcontroller or, if you feel like going some real engineering, with lots of op amps, comparators, and discrete logic!


3 years ago

How does the board interface to a controler? is it a 'dumb' chip that is literally nothing more than a H bridge with 4 inputs? Does it have back EMF feedback and rely on a +-5V analog signal or something for driving a motor, and what type of motor? 3 phase brushless motors require 3 H-bridges to be driven, along with back-EMF feedback to prevent slipping and stuttering.

If it is a 'dumb' H bridge, where you have full control of which of the 4 FETs is on or off, and you are simply trying to use it to drive a standard brushed motor, then do what steve said, assuming the 4 fets are configured so that +V is ON, and 0V is OFF. That may not be the case if they used P-channel FETs for the low side, and N-channel FETs for the high side. In that case, you can connect the high side fets to their opposing P channel fets diagonal to them (when you look at the schematics) Then it becomes easy to drive the now-2 inputs one with PWM, one with inverted signal. That is very confusing in words, but clear of you google schematics of H bridges with the FETs.

You need a little logic to sort out which legs are on at once, usually an AND gate. If your legs are numbered like this:

1 2

3 4

Then for FORWARD you need to drive 1 and 4, for backward, 2 and 3 and for all STOP, 3 and 4 (dynamic braking)

To be something like bombproof you need logic like this.

So. Output 1 = (FORWARD and PWM) AND NOT Backward

Output 2 = (BACKWARD and PWM) AND NOT Forward

Output 3= (backward and PWM) OR (Not (backward or Forward)

Output 4= (FORWARD and PWM) OR (Not(backward or Forward)

ALthough it looks like you can do this in your processor, its a much safer idea to implement the direction control in hard logic, then it can't go wrong.

So would i just give a PWM pulse to pins 1 and 4 to have it move forward? Lets say, i only wanted it to go forward. Would i just connect pins 1 and four together, and provide a PWM pulse? And another thing, The l298n shield has the ability to drive two motors... How would i control each motor separately?

Yes, but you have to be very careful that you never turn on 1 and 3 or 2 and 4 together.

For a 298 you'd need two PWM channels, and four direction selectors.