Using Opto-Isolators to Prevent Interference
Noise Interference From Motors
Often when trying to drive a DC motor with a PIC using transistors or H-Bridges, you will notice that the PIC is doing strange and unpredictable things. When this happens, the culprit is likely to be electrical noise being injected into your circuit by the brushes and spinning magnets in the motor. Unfortunately, this noise is very difficult to remove. One way of dealing with it is to try to filter out the noise by decoupling your motor driver circuit and logic circuit with ferrite beads, inductors, capacitors, and resistors. Unfortunate, the Mechatronics Lab stocks neither inductors nor ferrite beads, which limits your options for passive filtering.
A better method to isolate circuits is to use opto-isolators. This allows you to completely separate your circuits, including the grounds. While this scheme may add come complexity to your circuit, it is extremely effective.
An example circuit is provided below, where a 9V motor is being driven with a single MOSFET. Note that this circuit will actually invert your signal (i.e. when your input is high, the output will be low, and vice versa). Be aware that different opto-isolators will have different characteristics and pin-outs; you should check the specifications of your test different values of resistors and make sure the output waveform is acceptable while remaining withing the voltage and current specifications.