DIY H-Bridge motor control

Thread Starter

OkeiPro

Joined Jul 7, 2022
9
Hello!
I am making a steering wheel and pedals for a racing simulator. The steering wheel requires a motor for feedback, and the motor requires a driver. I already ordered a Double BTS7960 driver module from china, but since I want to improve my electrical engineering skills, I decided to try to make a driver myself.

My DIY driver needs to be able to drive a 12v 3a constant motor. It also needs to be pwm-able at 20kHz from an STM32 mcu. So I watched a few Lewis Loflin videos on youtube, and made up this circuit:
Screenshot_4.pngIt works nicely if I just apply the 3.3 volts to an optocoupler(through 100ohm resistor). Motor spins, everything is fine. When I try to switch it at 20kHz, or even just 3kHz, things start to go wrong. I saw that it starts to draw about 0.8a at 12v when I try to switch it. So I hooked up my oscilloscope to the gate pins of the transistors.
Here is how they look at about 5% duty cycle. Here, it doesn't draw any current, motor doesn't spin:
20220707_192446.jpg

Heres how they look at larger duty cycle. Starts to draw a lot of current, Nmos gets REALLY hot, so I haven't tested with motor attached yet:
20220707_192542.jpg

I don't understand why this is happening, maybe someone can spot an error in my circuit. I see that the other gate pin sags when the switched one tries to rise up. I understand that the rise/fall times are too large, but I don't understand why is that. Should I try to place lower resistance resistors on the gate-source pins? But then it will have an even harder time at lower dutycycles with the fall time. Is the optocoupler not the right choice for me in this situation?

I am a beginner in electrical engineering, but I am hopeful I can learn to do this.
Thanks!
 

ag-123

Joined Apr 28, 2017
259
it would probably be easier with gate drivers
https://www.infineon.com/cms/en/product/power/gate-driver-ics/ir2104/

it seemed possible that both the IRF540 (n-channel) and IRF5210 (p-channel) could be on at the same time on either 1/2 bridge (left and/or right) side?
IRF540
https://www.vishay.com/docs/91021/irf540.pdf
it seemed could have a Vgs_on as little as 2-4v, and Id goes higher as Vgs goes higher, while
IRF5120
https://www.infineon.com/cms/en/product/power/mosfet/p-channel/irf5210/
has a Vgs_on as little as -2 to -4v.
So that if the resistances of the optocoupler and 5.1k pull up brings voltages between the 2 ranges 2-10v, the transistors on either side of the bridge could be both on, effectively shorting 12v to GND

and I think large power mostfets has pretty high switching capacitances, in effect, they may even look like an RC circuit on the input at higher frequencies. Then what is assumed to be a "square" wave, may instead look like a PWM DC level that floats in the middle that switches both transistors on as like described.
 
Last edited:

Thread Starter

OkeiPro

Joined Jul 7, 2022
9
Yes use buffer transistors instead.
Welp, I changed them to transistors, and changed the Gate-Source resistors to 680ohms to make the rise time faster. Now the gate pins look like this, which I guess is better. But the switched Pmos gets hot, as it is drawing 100mA at no load (better than the 800mA before). Any ideas? Im guessing ag-123 is right, and the bottom Nmos gets turned on somehow.
20220707_225027.jpg
 

tindel

Joined Sep 16, 2012
889
Your schematic is very basic. There's usually a lot more that goes on behind the scenes in these motor drivers, but you're on the right path.
  • Your P-channels aren't doing anything. Their parasitic zener diodes are forward biased and conducting whenever one of the low-side switches are on. Reverse the drain to source pins to correct the issue.
  • Shoot-thru may be a problem. You need something to ensure both switches are not being turned on at the same time. The STM32 should be able to implement deadtime control, many driver chips do this, as well.
  • You need something to handle the inductive kick from the motor inductance on the transistors when all the transistors are off. Sometimes the parasitic zeners in the transistors are enough, sometimes not. It's not uncommon to see damping resistors and caps on the drain-source of the transistor.
  • Don't forget to decouple the 12V rail and you may possibly need some good power supply input filtering.
  • You need to drive mosfet inputs hard. Use a gate driver that is at least 1A. These transistors have a huge parasitic capacitance on the input, limiting the drive current.
    • IRF540N IRF540NPbF.pmd (infineon.com)
    • This datasheet says the gate charge at 12V is about 55nC. If you limit the current, with a 5.1kohms resistor, to a maximum of 2.3mA, the fastest you'll turn on is ~24us (ton=Qg/Ig). That's much too slow for 20kHz, and is about half the duty cycle, as your scope plots correctly show.
    • Take care not to drive the input too hard (over ~1A) - not damping the input may cause oscillations due to RLC second order ringing. IE. make sure there's a little bit of resistance on the gate ~2-3 ohms.
Bottomline, use a half or full-bridge driver chip made for MOSFETs - there's a ton of them out there.
 
Last edited:

Thread Starter

OkeiPro

Joined Jul 7, 2022
9
Your schematic is very basic. There's usually a lot more that goes on behind the scenes in these motor drivers, but you're on the right path.
  • Your P-channels aren't doing anything. Their parasitic zener diodes are forward biased and conducting whenever one of the low-side switches are on. Reverse the drain to source pins to correct the issue.
  • Shoot-thru may be a problem. You need something to ensure both switches are not being turned on at the same time. The STM32 should be able to implement deadtime control, many driver chips do this, as well.
  • You need something to handle the inductive kick from the motor inductance on the transistors when all the transistors are off. Sometimes the parasitic zeners in the transistors are enough, sometimes not. It's not uncommon to see damping resistors and caps on the drain-source of the transistor.
  • Don't forget to decouple the 12V rail and you may possibly need some good power supply input filtering.
  • You need to drive mosfet inputs hard. Use a gate driver that is at least 1A. These transistors have a huge parasitic capacitance on the input, limiting the drive current.
    • IRF540N IRF540NPbF.pmd (infineon.com)
    • This datasheet says the gate charge at 12V is about 55nC. If you limit the current, with a 5.1kohms resistor, to a maximum of 2.3mA, the fastest you'll turn on is ~24us (ton=Qg/Ig). That's much too slow for 20kHz, and is about half the duty cycle, as your scope plots correctly show.
    • Take care not to drive the input too hard (over ~1A) - not damping the input may cause oscillations due to RLC second order ringing. IE. make sure there's a little bit of resistance on the gate ~2-3 ohms.
Bottomline, use a half or full-bridge driver chip made for MOSFETs - there's a ton of them out there.
Sorry about the Pmosfets being the wrong way around, that was just an error in the schematic. I have it connected correctly in the practical circuit.
As I understood from this video:
I thought I shouldn't need any protection from back emf, as it works as a break when both sides are off. Another point is that the motor will really never turn more than few hundred rpm, and I don't think it will give me any problems. (I might be wrong tho, in that case I will think about that)
I really want to do this without buying any extra chips, as it already cost me quite a lot. So I could just try to switch them with higher current. But the problem with the switches being on at the same time is most troubling at the moment. I cant change the program for STM32 as I am using a ready built one. So I will try to use the two pwm signals, to use them as enable signal for another Pmos switch. That way, the switches don't have anything to short-circuit while they are supposed to be off.
 

Thread Starter

OkeiPro

Joined Jul 7, 2022
9
Your schematic is very basic. There's usually a lot more that goes on behind the scenes in these motor drivers, but you're on the right path.
  • Your P-channels aren't doing anything. Their parasitic zener diodes are forward biased and conducting whenever one of the low-side switches are on. Reverse the drain to source pins to correct the issue.
  • Shoot-thru may be a problem. You need something to ensure both switches are not being turned on at the same time. The STM32 should be able to implement deadtime control, many driver chips do this, as well.
  • You need something to handle the inductive kick from the motor inductance on the transistors when all the transistors are off. Sometimes the parasitic zeners in the transistors are enough, sometimes not. It's not uncommon to see damping resistors and caps on the drain-source of the transistor.
  • Don't forget to decouple the 12V rail and you may possibly need some good power supply input filtering.
  • You need to drive mosfet inputs hard. Use a gate driver that is at least 1A. These transistors have a huge parasitic capacitance on the input, limiting the drive current.
    • IRF540N IRF540NPbF.pmd (infineon.com)
    • This datasheet says the gate charge at 12V is about 55nC. If you limit the current, with a 5.1kohms resistor, to a maximum of 2.3mA, the fastest you'll turn on is ~24us (ton=Qg/Ig). That's much too slow for 20kHz, and is about half the duty cycle, as your scope plots correctly show.
    • Take care not to drive the input too hard (over ~1A) - not damping the input may cause oscillations due to RLC second order ringing. IE. make sure there's a little bit of resistance on the gate ~2-3 ohms.
Bottomline, use a half or full-bridge driver chip made for MOSFETs - there's a ton of them out there.
Hello! I got a TC4426 and TC4427 mosfet gate driver ic's. Switching now looks good, but I have this problem on the motor that has something to do with back emf. This is how the voltage across the motor looks like. The motor doesn't spin. What should I do? Thanks!
20220708_171607.jpg
 

LowQCab

Joined Nov 6, 2012
2,342
What is the "Locked-Rotor" Current for your Motor ?

We need an accurate Schematic-Diagram of your Circuit,
including how You are hooking-up your 'Scope,
and exactly how You are generating your PWM-Signal,
and driving the Inputs of your Gate-Drivers.

Your Gate-Drivers are probably way too small to drive a Motor.
Your selected Gate-Drivers have 12-Ohms of Output-Impedance,
which is almost guaranteed to be way too high.
.
.
.
 

Thread Starter

OkeiPro

Joined Jul 7, 2022
9
What is the "Locked-Rotor" Current for your Motor ?

We need an accurate Schematic-Diagram of your Circuit,
including how You are hooking-up your 'Scope,
and exactly how You are generating your PWM-Signal,
and driving the Inputs of your Gate-Drivers.

Your Gate-Drivers are probably way too small to drive a Motor.
Your selected Gate-Drivers have 12-Ohms of Output-Impedance,
which is almost guaranteed to be way too high.
.
.
.
The motor with locked rotor draws 15A at 5V.
A random online store spec sheet says it is rated for 3.34A (locked rotor) @ 12V.
I have a 795 DC motor, and the original place I bought it from didn't specify the locked rotor current.
Here is the schematic:
Screenshot_7.png
PWM comes from a STM32F103 mcu at 20kHz.
 

tindel

Joined Sep 16, 2012
889
If you're connecting your CH1 scope probe to the motor using the alligator clip and another part of the circuit with the CH2 alligator clip then you are shorting out those two nodes. To properly measure the voltage over the motor use CH1 and CH2 and the math subtract function to measure the voltage over the motor accurately or remove the CH2 alligator clip from the circuit and also make sure nothing else is connected to earth ground, shorting things out.
 

Thread Starter

OkeiPro

Joined Jul 7, 2022
9
If you're connecting your CH1 scope probe to the motor using the alligator clip and another part of the circuit with the CH2 alligator clip then you are shorting out those two nodes. To properly measure the voltage over the motor use CH1 and CH2 and the math subtract function to measure the voltage over the motor accurately or remove the CH2 alligator clip from the circuit and also make sure nothing else is connected to earth ground, shorting things out.
Used just 1 probe at a time, with alligator to one motor wire, and probe to other. Once burned my atmega32u4 when I didn't know you cant do that.
It seems that placing an electrolytic capacitor between ground, and one side of the motor removed the spike to other side.
 

tindel

Joined Sep 16, 2012
889
To PWM in the left direction you want to turn ON Q3 and then only PWM Q2.
This maintains the M+ node at 12V and the M- node is either 0V or ~12.7V depending on if the motor inductance is charging or discharging.
To PWM in the right direction you want to turn ON Q4 and then only PWM Q1.
This maintains the M- node at 12V and the M+ node is either 0V or ~12.7V depending on if the motor inductance is charging or discharging, but in the opposite direction.

This keeps a path for current to flow in the motor at all times. A motor is an inductive load. The current through the motor will continue flowing until the magnetic energy stored in the motor is dissipated out completely.
You obviously want some sort of transition time and sequence to go from left to right also.
 

tindel

Joined Sep 16, 2012
889
A picture of the current flow when turning left. Above is the charge cycle (12V across motor), and below the discharge cycle (-0.7V across motor).
1657312912620.png
 
Last edited:

tindel

Joined Sep 16, 2012
889
Hello! I got a TC4426 and TC4427 mosfet gate driver ic's. Switching now looks good, but I have this problem on the motor that has something to do with back emf. This is how the voltage across the motor looks like. The motor doesn't spin. What should I do? Thanks!
View attachment 271033
If you're not doing the switching as described in post #16 then the off cycle is floating and you have current through your inductor (motor) that doesn't know where to go. So that current tries to go into the 12V supply, through diode D4, and return to the GND path, but that won't happen because most power supplies cannot sink current. Since this is the case, the current has to go somewhere and it's clear that a parallel parasitic capacitance is oscillating at a low frequency until all of the energy is dissipated due to parasitic resistance (which can take a while) and you'll get this second order resonance that you see.

At least I'm 90% sure this is what is happening.

BTW Some batteries can source and sink current at 12V. You could try this with a 12V lead acid battery probably if you want to have some fun. This is essentially how regenerative breaking works - as a result of that current being dumped back into the battery whenever you stop the wheels from turning. You essentially send a reverse motor command that charges the battery rather than using a brake to stop the wheels from turning, dissipating the stored energy as heat.

Anyway - took a few posts to get all of my thoughts on where you're at described, but you hopefully have a better understanding of why it's doing what it's doing, and how to fix it.
 

Thread Starter

OkeiPro

Joined Jul 7, 2022
9
If you're not doing the switching as described in post #16 then the off cycle is floating and you have current through your inductor (motor) that doesn't know where to go. So that current tries to go into the 12V supply, through diode D4, and return to the GND path, but that won't happen because most power supplies cannot sink current. Since this is the case, the current has to go somewhere and it's clear that a parallel parasitic capacitance is oscillating at a low frequency until all of the energy is dissipated due to parasitic resistance (which can take a while) and you'll get this second order resonance that you see.

At least I'm 90% sure this is what is happening.

BTW Some batteries can source and sink current at 12V. You could try this with a 12V lead acid battery probably if you want to have some fun. This is essentially how regenerative breaking works - as a result of that current being dumped back into the battery whenever you stop the wheels from turning. You essentially send a reverse motor command that charges the battery rather than using a brake to stop the wheels from turning, dissipating the stored energy as heat.

Anyway - took a few posts to get all of my thoughts on where you're at described, but you hopefully have a better understanding of why it's doing what it's doing, and how to fix it.
This is what I thought was happening. I minimised it by adding an electrolytic capacitor connected to ground and one of the motor pins. I don't know if it is the right way of doing it, but it seemed to be working.
I cant switch it as you described in post #16, as I didn't make the firmware for the STM32, and I can't really change it. I'll contact the firmware creator, maybe he can help me to get 2 extra "Enable" signals.
Although my TC4426 driver, which switches the Pmos's died. I'll get a new driver tomorrow, and add a small resistor to the output pin, to limit the current. I don't really know how else it could have died. Any ideas? They cost money, and it isn't ideal to burn one after another :D.
Thanks!
 

tindel

Joined Sep 16, 2012
889
I cant switch it as you described in post #16, as I didn't make the firmware for the STM32, and I can't really change it. I'll contact the firmware creator, maybe he can help me to get 2 extra "Enable" signals.
Who said anything about needing enable signals? You might be able to do this with only two inputs [Hint: See your original schematic.] although shoot-thru might still be a concern.
 
Top