# dc motor and hung (not stable) pic

I'm running a dc motor using PIC16 family microprocessor. I connect it to my pc, I can send a command to run the motor in certain speed or stop.

It works for small motor (3V, 100 mA). But the moment I use bigger motor (3V, 300 mA), I can only I send 1 command e.g. run fast. After that, the pic refused to process the next command. Even when I turned off the device and turned it on again, the motor still running the last command. It seems like memory hardening or something like that.

I'm not sure what happened - Could it be a kickback current from motor caused the instability? I'm using TIP120 as motor driver. - I have put capacitors, diodes around the motor driver. But didn't help.

Can someone help me?

• Please include a schematic or a sketch of the circuit in question, to aid in our understanding of the issue. – Adam Lawrence Jun 14 '11 at 17:22
• Update: I tried to use 2 power supply based on @leon suggestion. It works well. But is there a way to only use 1 power supply? Having 2 power supply doesn't efficient IMHO. – mlam Jun 18 '11 at 15:43
• A picture is not the same as a schematic, but it does already point out some problems. The most obvious problem is that there is no bypass cap accross the PIC. Put a 100nF ceramic as close as possible accross the PIC Vdd and Vss pins. I'm not following what the top diode is trying to do. Again SHOW A SCHEMATIC. This was requested for a reason. – Olin Lathrop Jun 18 '11 at 22:42

Federico Russo said it well: "decoupling, decoupling, decoupling". ICs like microcontrollers require smooth power supplies, that is without disturbances. Small negative spikes may cause a reset or cause your software to go bananas. Positive spikes may do the same, and even damage the part. You definitely want to get rid of those disturbances. There are two ways to attack the problem, and the best way is to apply both.

First consider the cause of the disturbances. This is often difficult to find out, but in our case it's definitely the motor. Place capacitors between the power supply and ground, close to the motor's connection pins. Have an electrolytic capacitor of $100\mu F$ (actual value depends on the motor current, but this is a good start), and place a $1\mu F$ ceramic capacitor parallel to it. The latter is needed because the elco is not good at high frequencies, and there the ceramic takes over.
This is the first step. It's not just necessary to solve our problem, it also reduces EMI (ElectroMagnetic Interference). There are regulations about the level of EMI you may create.

Then we go to the microcontroller. The power supply will not necessarily be clean yet, there may be other noise sources. Here we do the same: place capacitors on the power pins, between $V_{DD}$ and ground, as close as possible to the pins. The microcontroller doesn't use high current, so we won't need the $100\mu F$ elco. Usually a $100nF$ ceramic will do. To calm your nerves :-) you may add a $1\mu F$.
What else? Some other pins may also be sensitive to noise. Look at the reset pin. It's no good ensuring that your power supply is clean if the microcontroller would reset due to spikes on the reset pin. So also a cap between reset and ground, again as close as possible to the pin. $100nF$ is fine.

• +1 for thinking about the reset pin (though most reset circuits will already include the capacitor) – Federico Russo Jun 19 '11 at 14:32
• Thanks @stevenh. Let me diggest it first. I'm still new. – mlam Jun 20 '11 at 17:53
• I found decoupling article cvel.clemson.edu/emc/tutorials/Decoupling/decoupling01.html – mlam Jun 21 '11 at 5:15
• @mlam: re the article. The 1 nF (0.001 $\mu$F) may be a bit too small. For very low power I suggest at least 10 nF, otherwise 100 nF – Federico Russo Jun 21 '11 at 7:21

Solderless breadboards are hardly ever appropriate for power electronics of any kind. (and especially not switching!)

Compared to a printed circuit board, they have poor parasitic inductance, resistance, capacitance, and noise susceptibility.

Don't expect them to conduct more than 20-50mA without causing undesirable voltage drops somewhere in your circuit.

If you are desperate to get your circuit working, you can try putting bypass capacitors on the board, but otherwise wouldn't bother trying to diagnose the problem, and instead I'd move all your power circuitry off of the solderless breadboard and onto a PCB or at least a vectorboard.

• also they cause ridiculously long ground return paths that can cause all sorts of issues. This really is the best answer, the others are just band-aids for the underlying issue. – Mark Jun 19 '11 at 5:55
• Thanks @Jason. Will take note your suggestions. The reason I'm using breadboard because I'm new in this. – mlam Jun 21 '11 at 11:50

Noise is getting from the motor circuit into the PIC circuit. The most likely way for the noise to get to the PIC is via the power supply as Leon pointed out. Where is the PIC supply coming from? Don't run the motor and the PIC from the same regulated supply. You probably don't need a regulated supply for the motor at all. At the very least, filter the supply to the PIC a bit. Best would be to give the electronics a separate regulator, preferably with a diode and storage cap in front of it. That way the PIC supply will still be stable even if the main supply is glitched to ground for short periods occasionally.

However, there is more to noise immunity than just the power supply. The best attack against noise is to avoid making it in the first place. Do you have a snubber accross the motor, or at least a small cap? You don't want too much else that will put strain on the PWM motor driver, but a little to limit the voltage slope from inductive kickback and commutation is useful.

Does the PIC have a good bypass cap as close as possible accross its power and ground pins? It certainly should. What is keeping MCLR high? In a high noise environment, MCLR must not be too high impedance else it will pick up noise and randomly reset the processor. Does this PIC have a PGM pin? If so, it needs to be kept from picking up noise just like MCLR. Also disable the PGM feature unless you really need it. What about inductive kickback from the motor? Does the current have a path to go without creating high voltage spikes? These could not only damage the motor driver, but get back into the PIC and cause unpredictable operation. What about the ground? Is the motor current kept off the PIC ground? It should be.

• How do you keep the motor current off the PIC ground? Do you need to keep the grounds apart for that? When I drive a DC motor with a micro, I connect source to ground and put the motor between V+ and the drain. Then the ground of the FET needs to be connected to the gound of the micro to be able to drive the gate, right? – i.amniels Jun 14 '11 at 18:11
• Yes they ultimately need to be connected together. However, the motor current will be noisy and large, so you don't want this going accross the ground plane or ground net near the PIC. The PIC circuit and the motor power circuit should each have their own ground nets that are tied together at one point, probably at the main power supply. – Olin Lathrop Jun 14 '11 at 19:15
• Ah I see what you mean. Already did this a few times. Tnx and +1 for the answer :-) – i.amniels Jun 15 '11 at 11:08
• Thanks for your comment @Olin. This is really good, but I'm still new. So I need time to diggest your suggestions. – mlam Jun 15 '11 at 12:58
• @Olin, do you mean like voltage regulator 7805? I attached the picture of my breadboard. I don't use snubber. I put electrolytic capacitors for both motor's legs. The third paragraph is interesting topic. But since I'm new I don't how to do it. Do you have a sample on how to do it? All the article or book I read, never cover the topic that you talked about. Like on how to cover the MCLR and PGM. – mlam Jun 18 '11 at 15:32

Use separate supplies for the PIC and motor and connect the two grounds at only one point. Filtering of the PIC supply should help, with a transient voltage suppressor, if you still have problems.

• Thanks for your comment @Leon. I'm still new. So I have to diggest your suggestion. – mlam Jun 15 '11 at 12:57
• Hi @Leon, after I separated the power. It works well. But having separate power doesn't sound efficient. Is there a way to use 1 power supply but prevent the noise. How do you filter PIC supply? – mlam Jun 18 '11 at 15:14
• @user4185 (don't you have a name?): decoupling, decoupling, decoupling! In the photo I see only two electrolytic capacitors. Place smaller ceramics parallel to them, and place them close to the voltage regulator pins. Place capacitors on the microcontroller's power pins, 1uF//100nF. – Federico Russo Jun 18 '11 at 15:49
• Thanks Federico. I'm new. I heard about decoupling, but I don't know how to do it. I will google it. but if you have good link/article about it, that will be great. – mlam Jun 18 '11 at 17:34
• I found decoupling article, thanks! cvel.clemson.edu/emc/tutorials/Decoupling/decoupling01.html – mlam Jun 21 '11 at 5:15

you should check h21e of the transistor, you should use darlington pair or mosfet, since motor load translates to load on pic pin. when load small pic can drive transistor, when load bigger bipolar transistor will require current = 300mA/h21e to be open.

TIP120: Collector−Emitter Saturation Voltage 4 V ? your supply is 3 V :). Put mosfet instead. Your schematic is wrong.