W. Durfee, Dec-04
One of the simplest ways to get a motor to turn in both directions is by using a double-pole, double-throw (DPDT) relay. Along with the relay, this hookup requires two transistors and two Stamp pins, one for on/off control and the other for direction control.
The diagram below contains the schematic for this setup. Essentially, the DPDT relay is switching the direction of current flow through the motor to get it to turn in either direction. The direction control transistor can be a 2N3904 because most relays require much less than 100 mA through the coil to trip the contacts. A TIP120 can also be used for the direction control. A TIP120 is required for on/off control because it must be able to handle the motor currents. For very high current motors, replace the TIP120 on/off control with a SPST relay.
The next diagram illustrates how to lay this circuit out. Pay attention to the markings on your relay, or look at the relay pinout chart. (Pinouts for relays commonly used in ME2011 are available online.) The pinouts shown in this figure are correct for the Jameco 174377 (sold in the robot store) and Radio Shack 275-249A relays. Thin wire (marked "BL" in the diagram) can be used for all connections to the Stamp board and connections to the coil since these are all low-current pathways. All other wiring should be appropriate to handle the current flowing through the motor.
And, here is a diagram showing how pins on the schematic relate to the actual pins on the Jameco 174377 relay.

If your program hangs when you switch the motor off or change directions, you might be getting spurious voltage spikes that are causing your program to jump into never-never land. You'll know this is happening when the motor keeps spinning when you expect it to stop or change direction. After confirming that this is indeed what is going on and that you don't have any wiring errors. Try putting short pauses after each line where you manipulate either the on/off or the direction control. For example, here is a program that runs the motor for 2 seconds in one direction and 2 in the other with pauses added. Assume pin 5 controls on/off and pin 6 controls direction.
high 5 'motor on pause 2000 low 5 'motor off pause 20 high 6 'reverse direction pause 20 high 5 'motor on pause 2000 low 5 'motor off pause 20 low 6 'relay off
If things are still going haywire, try adding an 0.47 micro-farad, 100 V filter capacitor right across the motor terminals. If things are still bad, add a flyback diode between the two common pins on the relay (making sure that the plus or band end of the diode points toward the plus voltage of the battery). Consult the Controlling motors technote for additional info on noise suppression.
If things are still not working, you may have to opto-isolate the circuit. With two SPDT relays and two 4N33 isolators, you can construct an isolated, bi-directional control circuit for your motor. Here's how.

