[Project] Developing a BLDC motor driver (mostly) from scratch

Hello everyone,

Since I’ve graduated high school and I have a good month or two until I have to actually start college, I’ve decided to go with a speedrun sort of project and I thought making a motor controller would be fun. The rules are very simple: I have two months or until I can’t continue, and whatever I end up with gets open sourced for anyone to experiment with. You are welcome to follow along and give feedback, though I can’t guarantee that any particular contribution gets used.
The overall strategy I’ve devised to minimize hiccups (magic smoke) and delays is to start with a low current prototype, create some software, repeat as needed, then modify the design to handle progressively more power. Since PCBs take ages to get made in the context of ~8 weeks, I’ll save them for the end and use various breadboards in the meantime. The goal is to support the 3 phase BLDC motors that we’re all familiar with, trapezoidal commutation, a few sensor options, current sensing, and if I’m feeling fancy, FOC.

Why do it? Because it’s fun, and more importantly I hope it inspires people to go on and do their own shenanigans with electronics.

Anyways, it’s June 29 and the plan is to construct an H-bridge circuit using MOSFETs, an STM32 board, and a gate driver IC (DRV8300). I wasn’t originally planning on using a gate driver but eventually realized that this is fairly tricky.

Why?

Here’s a diagram of an H-bridge cell.

MOSFETs switch to their closed state when the voltage between the gate (pin 1) and source (pin 3) is higher than a known threshold voltage (~2v). Assume a microcontroller is grounded to GND and uses 3.3v as a logic voltage. You can turn on A_L with the microcontroller because its source is at 0v and its gate will be at 3.3v. Turning on A_H is more difficult because its source could be at any voltage between 0v and 12v. In theory, if it is higher than around 1.3 volts, you might not be able to turn it on with 3.3v at the gate. Even worse, current can flow backwards from the source through the gate into the microcontroller, which can hurt it. DRV8300 handles all this for you and even prevents “shoot-through” by never allowing the high and low side to be on at the same time. It also takes strain off the microcontroller in general.

The test subject motor will be whatever I find at home or at the surplus store as I get almost zero access to NEOs during the summer. I’ve ordered a bunch of relevant parts from Digikey and once they arrive I’ll get started. Once again, feel free to share your thoughts, I’m looking forward to this project.

12 Likes

are you aware of https://simplefoc.com/ ?

that might get you going quickly.

10 Likes

You might also find some resources from ODrive useful as reference for your project, most (all?) of their current boards are closed source but the older boards as well as software is open source and should be available I believe.

6 Likes

This is a fantastic project that I’ve always wanted to attempt but have never taken on. Here’s some thoughts on resources off the top of my head:

  • In addition to the already mentioned Odroid, I would look into VESC as well. I believe both projects have some schematics, at least of older versions. IIRC one of them uses some driver in the DRV83xx family.
  • I found the videos from Great Scott! to be incredibly educational in this context. Here’s a video I remember being relevant, and he has a wealth of other videos that will probably be helpful.
3 Likes

Depending on the ST chip you select, some of them have a brushless motor library you can use that will help you a lot

There are also motor controller MCUs. I think Rev uses the STM32F407 family in the spark max and I think CTRE uses some version of Microchip’s dsPIC

You don’t have to totally reinvent the wheel here, although I understand that sometimes is the fun part

3 Likes

Here’s some Arduino code I wrote for a custom esc if it’s helpful

1 Like

This thread may also be of use to you

2 Likes

Just took at look at the resources everyone has posted. Minus SimpleFOC I haven’t seen any of it before so thanks for sharing it. VESC and Odrive were really neat and I’ll probably use them as inspiration later.

2 Likes

I’ll add to the list of recommendations. Have a look at the AM32 firmware project. It’s an open source BLDC motor control firmware for ARM processors.

AM32 ESC Firmware

2 Likes

Really excited to see how this turns out, I’ve looked into a brushed one might work on that over the summer break. Keep going you got this!

1 Like

Since you do not have access to NEOs from your team, you may want to get some small,low-power, and inexpensive BLDCs from a supplier to the hobby market and design your motor controller around it. These would be easier to build a controller for since they do not rely on encoder feedback. The parts you have already ordered may or may not be usable for this.

Other people have provided you with many links to good resources relating to the software aspects.

For the hardware, it is easy to underestimate the difficulty of getting the hardware right, especially at higher power levels, and especially if one has no prior experience working with power electronics. That is why I suggested starting with a small, low-power motor. This is based on working on the development of 3-phase motor controller circuitry for a few decades.

The physical layout of the components and the conductor paths is very important and will be difficult to get right without a PCB. At low power levels, you are more likely to get away with using a breadboard.

Since this is likely to be your first such project, it would be best to use slower edge rates when turning the MOSFETs on and off, regardless of the switching frequency, to avoid oscillations, ringing, and other such phenomena that can lead to destruction of the switching devices. It would also be best to mount the MOSFETs on a heatsink that is bigger than you think will be necessary. Be sure to use insulators between the MOSFETs and the heatsink.

Acquiring a simple oscilloscope and learning how to use it will be helpful.

Best of luck and enjoy the journey.

1 Like

I have a big post about this here:

Feel free to ask me for a schematic or layout review. I also recommend using an STM32G474 as your processor, as it is both powerful and common enough to have docs on motor controller applications. If you can use an integrated power stage from TI, that will help even more at the cost of power output (I think they max out at 8A?). There’s 3-phase mosfet inverter stages on JLCPCB that cost like $10 and will do 600V 20A with a heatsink if you are okay with it being a larger. Not that you should run 600V, but it means a 50V ESC will be extremely unlikely to break on you.

Don’t underestimate the importance of a good power stage layout/routing. It’s the difference between a 40A and 150A motor controller (ask me how I know…).

2 Likes

A significant part of my career was spent fixing issues like this.

If you are really serious about doing work on circuitry like this, it would be very worthwhile to get a current probe for your oscilloscope that will cover your expected output current and a lower current one (15A) for checking your MOSFET gate currents. It is much easier to verify that you don’t have undesirable behavior (oscillation and ringing) with a current probe, especially in the upper switching devices.

2 Likes

It seems like the code google drive link for your esc broke, do you still have it and if so would you send it to me? Thank you for this amazing resource

1 Like

All the parts I mentioned have arrived! I’ve started putting together the H-bridge portion and I’ll be working a bit more on putting together an actual design.

Yeah, that is a can of worms I’m going to try to avoid for the moment. I would like to give it a shot later but definitely not right now…

Looks like the most popular way to do this is with a large(r) gate resistor, is this what you were thinking?

No need to worry, I have one at home and there’s more advanced ones at my local makerspace (plus current probes) :wink:.

I would love to hear this story.

Once again I really appreciate all the resources everyone has shared. And yes, I will probably watch the 8 hours of Rick Hartley…

4 Likes

I have a similar story that ended with a very temporary candle

1 Like

Use two resistors and two ultra-fast recovery diodes. Each resistor is in series with one diode. The gate of the MOSFET cathode of one diode and the anode of the second diode so they are in anti-parallel. The other end of the two resistors are both connected to the output of the gate driver circuit. This allows adjusting the turn-on resistance independently of the turn-off resistance.

Beware of gate resistances that are too high. It can lead to device oscillations due to the Miller Capacitance of the MOSFET and excessive power dissipation.

Gate resistances that are too low can cause large spikes on the Drain-Source voltage. One should be able to adjust the gate resistances so that the spike is 10% of the output voltage. If the spike amplitude cannot be reduced without going to gate resistances that are too high, it means the inductance is too high in the loop from your power supply to the MOSFETs and back. You will need a differential probe to monitor the VDS of the upper MOSFETs.

Make sure you are using some sort of gate driver chip so that there is a low impedance drive for charging the MOSFET gate capacitance and for discharging it. A “passive” drive circuit can also lead to device oscillation and excessive power dissipation.

You are likely to have to add very short ON pulses for all of the lower MOSFETs when the output is inactive or if the output frequency is very low. These pulses charge the bootstrap capacitor that powers the gate drive circuitry for the upper MOSFET.

Yes. I have encountered and fixed all of these issues in real products.

Be sure to design and build your breadboards and PCBs so that you can add a short length of wire to go through the current probe and so that there is sufficient space among the components for the current probe.

You will also want to remove the probe tips to expose the round ground ring at the end of the probe. You can then use short lengths of solid wire to make a coil around the ring and solder it to a ground reference point near the point you want to probe. This eliminates the need for the ground clip wire and the spurious ringing that it introduces.

4 Likes

Got it working with manual switching, I’m going to start adding the gate drivers and Nucleo soon. The thermal performance is obviously atrocious but it can handle enough current to make this hard driver “spin” (twitch).

I almost immediately caught it oscillating at ~3.3 MHz, that’s how the decoupling capacitor ended up there. Current also kept flowing backwards through gates, very rude.

Side note, I’ve been experimenting with the Nucleo board and I have to say, STM32CubeIDE is up there with certain FRC software in terms of sheer jank. It will happily delete your code without telling you. And it doesn’t want to follow my indentation rules half the time. It is actually pretty good though. Still, the name is atrocious.

5 Likes

That type of prototyping board does very poorly with high frequency circuits. There is a lot of capacitance and inductance that make things worse than they would be on a real PCB. The solder type of prototyping board is better but the layout would still need to be done well to avoid oscillations and other undesirable behaviors.

4 Likes

Update for July 7: I managed to fry my Nucleo board, though I suspect it was more from bad handling and ESD than from the circuit itself. The gate driver is installed and works fine. Before the Nucleo got fried I was able to get the motor to spin around slowly with variable speed and a fairly satisfactory amount of torque. However, there’s currently no feedback so the driver constantly runs at full power. Once I get a new board I am going to try using the back-EMF method for syncing.

Also, I’ve created some schematics for the project here:

The original KiCad project is also in the Github repo. Let me know if there are any changes or improvements that you recommend.

4 Likes