DC Motor crashing uController - Page 1
Forum section: electronics / projects

author: elkayem
author_rank: Contributor
timestamp: February 18, 2018, 02:46:54 am
content: I have a fairly simple circuit, and what I'm sure is a common problem. I am using a MOSFET to drive a DC motor, and using a 10 kHz PWM output of an STM32F103 microcontroller to control the speed. Both are running off of the same 20V, 3.5A power supply. The microcontroller is powered using a 7805 using the recommended capacitors.I am able to run just fine with a 50% duty cycle (1A through motor). When I start stepping up, the microcontroller crashes when I get above 70 or 80%.I have noticed that if I connect to the motor using a long pair of parallel wires (~3 feet), the problem seems to almost completely go away. I can run for minutes at a time at 100%, though occasionally I do get a reset. It makes me wonder whether the extra resistance or capacitance in the wires is suppressing this issue. I do have a scope, and have looked at the 5V power going into the microcontroller. It does have a fair amount of high frequency hash (> 50 MHz) that can peak at a few volts.I have included a schematic showing all of the parts I am using. The motor draws about 2A at 20V.Any tips or best practices? Any thing I should look at to troubleshoot? I'm sure I could drive the microcontroller with a different power supply, though would prefer not to and anyway they will share the same ground.

author: ataradov
author_rank: Super Contributor
timestamp: February 18, 2018, 03:08:56 am
content: Is 100nF at the output of 7805 the only capacitor on the MCU supply? Or do you have proper decoupling at the MCU?

author: elkayem
author_rank: Contributor
timestamp: February 18, 2018, 03:31:50 am
content: You're right, I should have included that detail as well.I am using the "Blue Pill" board, schematic here:http://wiki.stm32duino.com/images/c/c1/Vcc-gnd.com-STM32F103C8-schematic.pdf. It is powered off of its own 3.3V regulator, with a 0.1 uF and 1 uF on the 5V side, and a 0.1 uF and 10 uF on the 3.3V side.

author: Siwastaja
author_rank: Super Contributor
timestamp: February 18, 2018, 10:56:59 am
content: Minimize the loop area of the motor mosfet source node (GND) and the +20V motor supply node. Then, add a combination of a big electrolytic cap (say, 470-1000uF 30V) and smaller ceramic caps (say, a few 1 uF 35V) between those nodes, physically as close as possible.Note that while the motor may be further away, D1 should be in the tight loop, right next to the MOSFET and the capacitors. Although I understand that schematic is not a layout picture, the schematic shows the diode in the "wrong place", suggesting a possible layout issue here.The small ceramic caps provide path for high-frequency pulse currents quickly, so that the currents don't need to flow through long parasitic inductances. The big electrolytic cap damps oscillations by providing enough capacitance (Celectrolytic >> Cceramic) so that parasitically stored energy cannot much change its voltage, but high enough ESR so that it doesn't contribute to back-and-forth oscillating currents, but instead eats them.Doing the motor power stage layout right with the right caps will solve the problem 99% surely.An additional elcap (such as 47uF) at the 7805 input would be a plus.

author: Benta
author_rank: Super Contributor
timestamp: February 18, 2018, 11:21:27 am
content: I'm missing caps for brush suppression at the motor. Try with 22 nF across the motor and 2 x 10 nF from each terminal to the motor casing. They should be mounted directly at the motor.Edit: or mount something like this:https://www.conrad.de/de/entstoerfilter-passend-fuer-modellbau-motor-500-600er-234559.html

author: elkayem
author_rank: Contributor
timestamp: February 18, 2018, 05:17:41 pm
content: Benta and Siwastaja, you guys are awesome!I followed your tips methodically, and the problem is fixed. Just for your interest (and that of others struggling with the same problem, I'll show a few before and after shots. Here is the original noise on the 5V input to the uC. This plot is centered on the 5V power, with 1V grid. You can see nearly 7V p-p!Siwastaja, you suggested:QuoteYou are correct that the schematic didn't accurately represent the layout. The diode and MOSFET, 20V and ground were in a fairly tight arrangement, and the motor about 10 inches away. I took your suggestion to add some additional capacitors as close to the MOSFET and diodes as possible. I put a giant 3300 uF capacitor and the largest ceramic I had on hand, a 0.1 uF. The result on the 5V noise is shown below. This still shows around 6V p-p, a slight improvement, though the uC still crashed.QuoteBefore embarking on this morning's debugging, I had already placed a 100 uF on the 7805 input, so the above results include this capacitor.Benta, you suggested:QuoteThis made a dramatic improvement. The largest noise transients on the 5V supply that I could find were about 2V p-p. I let the motor run for 10 minutes at 100% and the uC never crashed.Thank you both! I've updated the schematic to show the changes I made. This now shows the motor as separate from the diode and MOSFET.

author: Benta
author_rank: Super Contributor
timestamp: February 18, 2018, 07:31:03 pm
content: Yeah, brush arcing makes much more trouble than you think.Glad you got it working.

author: SeanB
author_rank: Super Contributor
timestamp: February 18, 2018, 07:38:03 pm
content: I would also add a low value resistor in series with the feed to the regulator, so that at max current draw from the controller you drop around 2v across it, so that there is better isolation of the motor transient current from the digital power supply. As the current drawn is probably under 200mA I would suggest 10R 0.5W as a ballpark start value. Should get the power rails a lot more stable.

author: elkayem
author_rank: Contributor
timestamp: February 20, 2018, 05:08:33 pm
content: Thanks Sean, another great suggestion! I followed your advice. The 7805 current draw is closer to 100 ma, so I bumped up the resistance a bit to 15 ohms for a 1.5V drop. It also helps offload a little bit of the power from the 7805, which is getting a little toasty at just under 100C. (I'm thinking a heat sink is needed, but that is another question...) Anyway, the measured transients on the 5V line are still around 1V p-p, but anything I can do to make the design more robust is great.

author: CopperCone
author_rank: Super Contributor
timestamp: February 20, 2018, 05:24:19 pm
content: Another option may be to use a modern voltage regulator with better high frequency rejection. Also it may help. To drive the transistor with a schmitt trigger or buffer to help isolate the drive signal some.Im not sure about this but you can possibly determine the bandwith of the mosfet drive signal.and put a filter on it to shunt away energy higher then you drive signal (harmonics depend on frequency and rise time) to possibly prevent high frequency energy from being conducted into the mcu through the gate of thr transistor... Though this is probovly rather low and you should focus on the power supply selection.You can also add a rlc filter to your voltage regulator to improve its high frequency rejection capability. They typically perform poorly in the megahertz area where you are having a problem. Since its in the 50MHz region you will want to design a rlc filter that uses inductors with a high self resonant frequency.Ideally you would use a spectrum analyzer on the power rail to see the frequency spectrum of the interference to design a filter that provides appropriate attenuation.Basically there is a relationship between the probability of soft error in the microcontroller and the noise present within it. These details are not well known and complicated and probobly subject to change as the transistors age. Furthermore a high noise level present in a system that seems to operate fine may increase suceptibility to external noise in some caeses due to mixing inside of the circuit and timing coincidences. The probability of hard error from interference is not known to me but i can imagine that it is low by design of the chip and it would really require the perfect storm. This is also dependant on what the chip is doing at the time and how the program is structured. Look at the so called "rowhammer" attack to get some insight on how noise can effect a complex digital system with constantly changing rf impedances.The 7805 is old. There are definatly better parts and i recall some regulators meant for rf use to have very good hf psrr. Nothing but cost prevents you from using them.

author: elkayem
author_rank: Contributor
timestamp: February 21, 2018, 06:21:04 am
content: Great suggestions CopperCone! Can you suggest a "modern" voltage regulator that I should be using? I have a drawer full of 1.5A 7805s that I have been working my way through, but will need to replace them at some point, and might as well bring myself to the 21st century.

author: ogden
author_rank: Super Contributor
timestamp: February 21, 2018, 09:13:43 am
content: Quote from: elkayem on February 21, 2018, 06:21:04 amThose are not "bad" regulators, you just have to know what they can do or can't. Those will leak all the hi-frequency noise through w/o filtering/regulation. -So you must filter high frequencies before regulator. Notice in your waveforms ringing frequency, it's very high. Then check PSRR specs of your regulator and you will see your problem - that your regulator can't regulate/filter such a high frequency noise! Actually even modern LDO's can'tSolution: Do you happen to have some 10uH inductor capable of currents that are needed for 5V rail? Supposedly 100mA for that blue pill shall be more than needed. Put one such inductor and 10 Ohm resistor in series before C2/C7. if no inductor, then just use resistor alone. It together with following caps will do some of filtering job as well.High frequency 2vpp pulsations on 5V bus is still *very* bad. You have to get them down to 100mVpp or so.

author: ZomBiE80
author_rank: Contributor
timestamp: February 21, 2018, 10:39:31 am
content: Just put a 10nF cap between motor poles.

author: Benta
author_rank: Super Contributor
timestamp: February 21, 2018, 10:57:31 am
content: Quote from: ZomBiE80 on February 21, 2018, 10:39:31 amIf you read the previous posts, you'll see that this has already been addressed.

author: ZomBiE80
author_rank: Contributor
timestamp: February 21, 2018, 11:10:28 am
content: Quote from: Benta on February 21, 2018, 10:57:31 amYou mean this?:QuoteToo much capacitance. 2.2nF and 1nF would be better.

author: danadak
author_rank: Super Contributor
timestamp: February 21, 2018, 12:19:52 pm
content: Look at cap datasheets carefully, not all caps are equal for samesized cap in terms of ESR. Polymer caps for bulk have ~ 10x betterF vs Z curves than regular tantalums.Also V vs C curves, especially MLCC -https://www.niccomp.com/resource/files/ceramic/VoltageCoefficientofCapacitors-032012-R1.pdfRegards, Dana.

author: Benta
author_rank: Super Contributor
timestamp: February 21, 2018, 12:37:14 pm
content: QuoteSo why do you suggest 10 nF?I've lost count of the number of PMDC motors I've used for RC models, the normal cap value is 47 nF and 2 x 22 nF for brush suppression. Mind you, this is for switching frequencies around 3...4 kHz. In this case it's 10 kHz, so 22 and 2 x 10 fit quite well.

author: Siwastaja
author_rank: Super Contributor
timestamp: February 21, 2018, 02:13:36 pm
content: Yeah, you would fine-tune the brush suppression cap values for the specific motor.Often, these caps are included by the motor manufacturer, which I kind of assumed, but it's not always true especially with cheap motors.Note that excessive capacitance across the motor will cause current spikes in switching, causing more harm than good. Some designs might include high capacitor values that only apply to DC powering the motor (or linear regulation loops), but don't work with PWM supply. Nevertheless, these capacitors are subjected to high peak ripple currents; many types do not work. They are almost always ceramic.

author: CopperCone
author_rank: Super Contributor
timestamp: February 21, 2018, 05:04:40 pm
content: Check out the lt3042. It has like 60 db of psrr at 10MHz, this is nothing to scoff at. Combined with a rlc filter it will be pretty good, but yes i believe that ogden has a point that 50MHz is a bit high. A rlc filter is better suited for this frequency region then dc regulators but i think its still worth a try.You can design some simple rlc filters in a simulator such as the japanese online one to see how much attenuation you will get depending on where the 3db point of the filter is. Pay attention to self resonant frequency of the inductor, you will want it ahead of your target frequencyHow time and temperature stable are the noise profiles generated by brushed dc motors?Getting some kind of network analyzer capability would be useful here to see the impedance of your power structure.For the sake of redundance and the fact that the motor caps are basically mounted to a vibrating structure that may temp cycle and stuff i would design the circuit to work without them if you can afford to do so. The motor housing does not seem like the most friendly location for electronic components.Can someone elaborate on the procesure for tuning the x/y capacitors on a motor? Can the inductance of it be measured to give some starting values? I am not very familiar with electromechanical devices. There seems to be alot of contention and no procedure regarding capacitor choice here.

author: ogden
author_rank: Super Contributor
timestamp: February 21, 2018, 05:58:00 pm
content: Quote from: CopperCone on February 21, 2018, 05:04:40 pmNo need to look for another regulator just because there's no filter. In circuits with hi-freq noise sources it is must to do extra power supply filtering using (LC) filter. Even lt3042 will need filter so better just add proper filter for existing reg and that's it.

author: Siwastaja
author_rank: Super Contributor
timestamp: February 21, 2018, 06:18:23 pm
content: Note that simply adding an LC filter to the power supply can and often willamplifythe input oscillations/noise.LC filter needs to be designed to be properly damped. Two typical ways to dampen are:1) Parallel resistance to the inductor (either an actual parallel resistor, or high-frequency resistive inductor core material. The idea of ferrite beads is that they are often lossy, but make sure they don't saturate)2) Series resistance to the capacitor (either an actual resistor, or using a high-ESR capacitor, such as your typical cheap electrolytic)Both ways may be needed. It's best if both inductor and capacitor are "lossy" at high frequencies. This loss is irrelevant from the efficiency viewpoint when just filtering noise (i.e., they don't actually carry SMPS ripple currents), but is essential to make it stable.A combination of series R (also sharing the 7805 heat dissipation), a hefty ferrite bead (not saturating at the peak currents), and a poor old aluminium elcap is often simple and effective.

author: ogden
author_rank: Super Contributor
timestamp: February 21, 2018, 06:21:36 pm
content: Quote from: Siwastaja on February 21, 2018, 06:18:23 pmIndeed. I did suggest damped filter:Quote from: ogden on February 21, 2018, 09:13:43 am

author: ez24
author_rank: Super Contributor
timestamp: February 21, 2018, 07:53:41 pm
content: Are you using a 24v motor? I find that 20v are rare. On Aliexpress there are only 3, and they are rated for 6-20v. And there are about 8,000 rated for 24v.Are you using a laptop power supply? My Dell puts out 19.6v. I am just curious about the 20v since it seems odd to me.Interesting topic - lots to learn here and someday I hope to do the same thingthanks

author: Benta
author_rank: Super Contributor
timestamp: February 21, 2018, 08:23:33 pm
content: Quote from: ez24 on February 21, 2018, 07:53:41 pmDoesn't matter. A 24 V PMDC motor will run perfectly on 20 V, just slower. Or on 12 V at half speed. Won't deliver full power, of course.

author: Circlotron
author_rank: Super Contributor
timestamp: February 21, 2018, 08:47:44 pm
content: Those caps across the motor are going to pull a bit of current every time the mosfet turns on, as someone already mentioned. Maybe put 10 ohms in series with each of those caps? Also perhaps increase the mosfet gate resistor to slow down the switching transition, but not so much that the mosfet gets warm.

author: free_electron
author_rank: Super Contributor
timestamp: February 21, 2018, 09:30:14 pm
content: put a diode from 20v to the 100uf capacitor.That way your motor can not suck energy out of that bulk capacitor.the capacitor energy only goes to the regulator. so when 20v collapses due to motor current the 100uf act as 'backup' for the microcontroller.

author: Benta
author_rank: Super Contributor
timestamp: February 21, 2018, 10:03:47 pm
content: Quote from: Circlotron on February 21, 2018, 08:47:44 pmYes, of course they will.The brush suppression caps are a compromise between PWM effects and brush arc damping. There is no "One Perfect Solution", and no closed loop mathematical calculation for this.It's empirical, and my original suggestion of "try 22 nF and 2 x 10 nF" was intended to give ballpark values. As the OP tried this with success, it wasn't far off. I'm certain it can be optimized, the question is whether it's worth the effort.MOSFETs are rugged, and the small current needed for the brush caps have in my experience never caused problems.

author: nuno
author_rank: Frequent Contributor
timestamp: February 21, 2018, 11:41:52 pm
content: Quote from: free_electron on February 21, 2018, 09:30:14 pm+1I've solved uC resets many times with this technique, it isn't even necessary to add anything else. It's good when the power stage pulls alot of current causing a dip in the system's power supply.But the cap across the motor poles is still highly recommended and will reduce noise. Even small 3V toy DC motors can cause quite some excursions on the power supply.

author: elkayem
author_rank: Contributor
timestamp: February 22, 2018, 03:21:45 pm
content: Looks like I need to catch up with this thread...Quote from: ogden on February 21, 2018, 09:13:43 amI do have the series resistor, but no inductor. I think you are right that the next step would be to design a proper LRC filter. I don't doubt that 2V P-P is still bad. They look a lot worse on the screen shots I showed because I zoomed way into a single transient. The transients themselves are very short, and most of the time the line is dead quiet. I can't recall exactly, but seem to remember the transients lasting on the order of a few usec at most. Still, I'm sure it doesn't take much to reset the uC.I really should start looking at the 3.3V power. It isn't just the uC, but also a number if ICs including 3 SPI devices, an I2C device, and an LCD screen. Most (except the LCD) draw from 3.3V, so that is where I need to worry about the transients. Sum total current draw is around 100 mA. I've now run for up to an hour at a time with no issues so tempted to leave the circuit as is. Now that I have everything soldered in place on a strip board, it's a pain every time I need to pull out the solder wick...

author: elkayem
author_rank: Contributor
timestamp: February 22, 2018, 03:24:48 pm
content: Quote from: CopperCone on February 21, 2018, 05:04:40 pmThanks for the tip!QuoteUnknown. I'll need to continue to monitor.

author: elkayem
author_rank: Contributor
timestamp: February 22, 2018, 03:27:23 pm
content: Quote from: ez24 on February 21, 2018, 07:53:41 pmIndeed it is a 20V motor. It has 20.5V stamped on the side of it. It is repurposed from an old appliance, so I'm sure it isn't available from aliexpress. Also, yes I am using a laptop power supply.

author: Siwastaja
author_rank: Super Contributor
timestamp: February 22, 2018, 03:34:56 pm
content: DC motor voltage ratings are rather arbitrary. The same motor may be stamped with 24V or 12V or 48V for different customers.Current ratings are way more important (and more exact!), but often omitted from cheap motors.You can't just apply x volts to most "x volt" DC motors, which is quite funny when they lack the current spec :-). Then, the voltage spec may be read as: "it's safe to apply this voltage when there is no significant mechanical load and hence no significant current".

author: elkayem
author_rank: Contributor
timestamp: February 22, 2018, 03:42:35 pm
content: Quote from: Circlotron on February 21, 2018, 08:47:44 pmI should point out that these transients are not caused by the mosfet switching. They are present even when I am running at 100% duty cycle. They are almost certainly caused by arcing with in the motor, as another poster pointed out.Incidentally, the mosfet switches look really clean now, but when I was first prototyping the circuit, I clipped the flyback diode directly to the motor, and the transient rang like a bell! It was an almost textbook lightly damped oscillator.

author: elkayem
author_rank: Contributor
timestamp: February 22, 2018, 03:46:04 pm
content: Quote from: Siwastaja on February 22, 2018, 03:34:56 pmNothing gets past you guys! The motor also has 1.8A stamped on it. It draws pretty close to that at 20V with the current load.

author: Siwastaja
author_rank: Super Contributor
timestamp: February 22, 2018, 04:13:25 pm
content: Quote from: elkayem on February 22, 2018, 03:46:04 pmStamped "nominal currents" are also quite difficult to actually decipher, but far better than nothing. My rule of thumb has been that I tend to exceed that rating by 3x for short time (a few seconds), or by 1.5x for longer pulses (minutes). But never more than 3x-4x, since it usually doesn't offer more output torque due to iron saturation.Note that if you just connect an "X volt" DC motor to "X volts" with stalled axle, it might easily take 10x the "nominal" current - only the resistances limit the current. Back-EMF is 0V, so current is (X volts - 0 V) / R.Also note this is completely normal behavior - every motor is running at 0 rpm when you first power it, thus taking this full current when connected to a dumb, hefty DC source (such as a big battery). Most motors completely saturate their iron in this state, giving out only the same torque they would give out at far lower current - this means efficiency is very poor, extra amps and watts go in with no extra work out!For this reason, proper PWM control should include current sense and feedback which allowsyouto set the output current limit - have the same work done with less heat. Then you can also do both speed and torque control, which is kinda cool!Of course, this is often utterly irrelevant with cheap small motors, and simple use cases. They are inefficient anyway, and have so much resistance in windings and brushes that current is fairly limited even when full DC is applied with stalled rotor, which protects the MOSFETs as well.