(Note: images are intentionally grainy to reduce load time)
There are many ways to implement on-off control of small DC motors using a BASIC Stamp. Each method requires construction of an interface circuit.
The motor can be controlled by a switch....but this is of little use for Stampers
The most common way to control a motor is through a transistor. The TIP120 is an NPN darlington transistor. Touch the transistor while the motor is on. If it's too hot to touch, you need a different way of controlling the motor.
You can have several motors powered with one battery or each with it's own battery. For the latter, tie all battery grounds together.
One transistor can control several motors, but check for overheating.
MOSFET power transistors are an excellent alternative to using bipolar power transistors. The IRL510 can be switched on with a logic-level gate signal and has low on-resistance. Low R_on means less power is dissipated in the transistor. Because the gate is voltage controlled, no resistor is needed between the Stamp and the gate.
For high current motors, try using a relay. There is no voltage drop across the relay which means the full supply voltage is available to the motor. Most relays have control coils which must be switched on and off by the Stamp through a transistor.
Some relays have coils which can be controlled directly from the Stamp. Look at the relay specifications. Direct Stamp control requires a coil rated at 5V and 20mA or less. Note that most logic level relays have contact current specs that are too low to handle all but the smallest motors.
With a double-pole, double-throw (DPDT) relay and two transistors, you can make your motor turn in either direction. Two Stamp I/O pins are needed. One turns the motor on and off, the other controls the direction of the motor. When the motor is off, set the direction-control Stamp pin low so that power is not wasted in the relay. For more on bidirectional motor control with a DPDT relay, see the Bidirectional Motor Control document. For a deluxe, isolated
For small motors, you can also use the L293D motor driver chip to implement bidirectional control. For details, consult the L293 Motor Driver document.
When inductive loads are switched on and off, large voltage spikes will be generated. These spikes can cause your Stamp program to jump into never-never land. Sometimes voltage spikes can be tempered by adding a "fly-back" diode across the motor leads. Be careful you have the orientation of the diode correct. A "snubber" capacitor across the motor leads can also help. An 0.1 to 0.5 uF capacitor works fine for most motor filtering applications. These capacitors are not polarized so orientation does not matter. The 1N4004 diode is good for fly-back applications, and a 0.47, 100V mylar capacitor is good for snubbing. Both are sold in the ME2011 Robot Store.
Another solution to voltage spikes is to isolate the motor from your Stamp. The 4N33 is an inexpensive (about $0.40) optical isolator which uses light to couple the on-off signal from the Stamp. Note that the (-) terminal from the battery is not connected to GND on the Stamp. The idea is to keep the whole motor side of the circuit isolated from the Stamp.
For a circuit that isolates the motor and can run the motor in two directions, see the the Bidirectional Motor Control tech note.
To slow your motor down, you can put a 10 to 15 ohm power resistor (rated 3 watts or more) in series with your motor. Note that this will also reduce motor torque. One easy source of low-ohm power resistors are light bulbs. Try several wattages until you get what you want. For this purpose, flashlight bulbs are best as the resistance of car or household bulbs is too low. You could also put a second motor in series with the first to cut the speed in half.
You can also reduce motor speed by pulse-width modulating (PWM) your motor as shown in the following code
loop: high 0 pause 5 low 0 pause 30 if in6 = 0 then action goto loop action: ' rest of code goes here
This creates an square wave, on/off control signal with about a 14% duty cycle. Experiment with the on and off times until you get what you want. The pin check is needed or else there is no way of breaking out of the loop. In this case, Pin 6 would be reading a switch that detects when whatever is moving reaches the end of its travel.
Another option is to have a for loop that runs the on/off code for a pre-determined number of cycles. Here's an example where the motor will run slowly for (5+30)*50 msec. Define i as a variable at the top of your code (i var byte).
for i = 1 to 50 high 0 pause 5 low 0 pause 30 next action: ' rest of code goes here
Because the motor is being switched on and off rapidly, voltage spikes may be a problem with this method so you might have to use a MOSFET control transistor, or optical isolation.