# Microcontroller + DC motors oscillation problem

I have designed a simple circuit for using AVR microcontroller (Atmega328) for controlling few DC motors.

Everyting works perfectly with just one motor connected - but after connecting the second one, circuit gets into strange kind of oscillation (around 15 Hz) of turning on and off. I am pretty sure I have enough current from power supply (500 mA), the motors are very small (cellphone vibrators).

Any ideas?

-
 If anybody cares, I will also try to switch to more powerful regulator, seems like the motors in the end need more than 1A! – mrkva Jun 3 '12 at 18:42 All the points raised may be relevant BUT you MUST take note of what @rocket surgeon said - you must not drive transistor bases directly as shown. It may or may not work well. And doing it with two outputs MAY interact. The port should not be loaded to above it's specified output if you want guaranteed results. In non-professional practice it is common enough to load ports more heavily than they should be and it may often work - but hard port-pin to base connection is an invitation to disaster. – Russell McMahon Jun 4 '12 at 2:45 If regulator is at 1A limit then Vin = 9V, Vout = 5V (or less if current limiting) so dissipation in regulator = V x I = (9-5)V x 1A = 4 Watts. Do you have a heatsink on the regulator. What package is it in. 4 Watts is more than even a TO220 can handle with no heatsink. – Russell McMahon Jun 4 '12 at 2:49

## 5 Answers

I have a very definite idea: the motors cause so much noise on the 5V power supply that the microcontroller gets continuously reset.

The AVR has a brownout detection which causes an internal reset when the supply voltage drops below a preset level.

The level is programmable, see table 28-4 in the datasheet. I wouldn't disable it, but maybe adjust the level.

decoupling!!

Decouple the controller properly: that's 100nF directly on pins 7 and 8. With a noisy +5V you can add a 1$\mu$F and a 10nF, the smallest one closest to the pins.
Decouple the motors, with 100$\mu$F across their connections.

Then, never connect your microcontroller directly to the same supply as a motor. Isolate them with diodes from your voltage regulator, and place a 100$\mu$F capacitor after the microcontroller's diode. If you don't like the diode's voltage drop (it will give you 4.3V instead of 5V) then add a diode between the voltage regulator's ground and ground itself. That will raise the output voltage to 5.7V, so that it compensates for the voltage drop across the other diodes.

Your reset circuit is also unusual. You have no control over the time constant. Most often you'll connect a capacitor from the reset pin to ground, and a resistor to Vcc.

edit
Rocket surgeon and Nathan already told you about the resistors for the transistors. At the moment there's only the internal resistance of the output MOSFETs which limits the base current. This resistance is only 25$\Omega$, so that you'll have 170mA from each output. That's certainly damage to your controller. Add a series resistor

$R < \dfrac{(5V - 0.7V) \times 400 }{I_C \times 2}$

where $I_C$ is the motor's current, $400$ the current gain of the transistors, and $2$ a safety margin.

-
 Thank you very much for answering. I wonder - I've already put protection diodes around the motor - so is isolating of the motor power supply necessary? Uff. Also, I have tested similar circuit with Arduino without any complications, do you maybe have some ideas why it worked there? Power supply wasn't isolated and motors weren't decoupled. – mrkva Jun 3 '12 at 15:45 Arduino may have better decoupling: more caps, closer to the AVR, lower impedance voltage regulator? The diodes don't do a thing when the motor is running; they protect the transistors against a high voltage peak when you turn them off. Without diodes the voltage would rise very high, and now anything higher than 5.7V is drained to the power supply. – stevenvh Jun 3 '12 at 15:54 Ok, I've added 100nF between 7 and 8, next to it 22uF and 100uF across the motor, but I still get the same issue after a while. Unfortunately it would be a pain to do a separate power line for motors (universal board :/), any other ideas which I can try before I do it all over again? – mrkva Jun 3 '12 at 16:11 I suggested this in a comment to Rocketmagnet's answer: let the microcontroller light a LED for say 100ms after reset, and then switch it off. If the LED stays on you have continuous brownout resets. Then you know for sure whether that's the problem (I think it is). Can't you really add a diode in series with the motor's V+? They will be connected through wires, right? You didn't forget the resistors? – stevenvh Jun 3 '12 at 16:15 Ok, I uploaded a code which lights-up a diode for 1 s after reset, and it works, until first motor turn is needed - then reset occurs. So clearly you are correct. I will now try to put diodes on the motor power supplies. Thank you very much for your help, I just got into electronics in beginning of this year and with some things I have to learn predicting. – mrkva Jun 3 '12 at 16:32
show 7 more comments

Great answer from stevenvh. I would just add that you should put a feeder cap on your 5V rail (i.e. put another 100uF after the regulator) because the load impulse response of the regulator is probably not enough to prevent brownouts that will reset your micro during sudden changes in the motor current draw.

Plus, if you're using BJT's, you really should have a current-limiting resistor between the microcontroller and the base.

-

Your outputs from CPU to transistors bases have no resistors. You are shorting the out port to ground.

-

The grounding point for your motors could also be culprit, ground them close to the power supply ground and not close to the uC.

-

Have you tried looking at the 5v supply on an oscilloscope? This would tell you if you were experiencing brownouts.

Don't have an oscilloscope? (you really should have even a cheap one from eBay) Then you can watch the 5v line using a comparator.

Compare the 5v line to a known good 5v line from another 5v regulator. You can adjust the brown out detection level with the pot. Set the pot so that the voltage on the negative comparator input is half the ARV's brown out voltage. If the LED lights (even for a moment) then the ARV probably reset. You may need to do this in a dark room so that you can see tiny flashes of light from the LED. Or use a very low resistance.

-
"Don't have an oscilloscope?" He doesn't even seem to have resistors! ;-) – stevenvh Jun 3 '12 at 15:03
You don't need this circuit: let the microcontroller light a LED for say 100ms after reset, and then switch it off. If the LED stays on you have continuous brownout resets. (Or a bug in your software ;-)) – stevenvh Jun 3 '12 at 15:31
@stevenvh - That's a pretty good idea! – Rocketmagnet Jun 3 '12 at 15:33