# Computer controlled motors

Our little computer can put 3.3 volts on any of its output port pins. It can deliver as much as 40 milliamperes on a pin, although that is pushing things a little far, risking overheating the little chip. That's a total of 3.3 volts times 40 milliamperes = 132 milliwatts of power.

While not a lot of power, it can drive small DC motors if they have light loads.

One nice little motor I tried spins nicely when attached to an output port pin, but since it is designed for a higher voltage (probably 5 to 12 volts), it is only drawing 13 milliamperes. That is only 43 milliwatts, which is not a lot of power, so the motor can only spin things like paper disks or small fans, and would not be able to move something like a little robot car.

Motors designed to run on 3 volts or less draw more current. The little motors in pagers and cell phones that make them vibrate are designed for these low voltages, and the one I tested drew 40 milliamperes, and vibrated like crazy, as it is designed to do. Small DC hobby motors designed for 3 volts (such as those you might salvage from a toy car that runs on a couple AA cells) can drive little cars, or move small sculpture parts. Bear in mind that two AA batteries in series provide 3 volts at 700 milliamperes, or 2100 milliwatts, so our little computer will not move the car very fast all by itself.

Nonetheless, sometimes you don't need a lot of power, and the simplicity of driving the motor directly from the computer's output pins makes life a lot easier.

The little motor will run if one of its wires is connected to 3 volts or more, and the other is connected to ground (0 volts). This means we can connect one wire to ground, and the other wire to any output pin, and run the motor by turning that pin on, just like lighting an LED.

But DC motors can run in either direction. If we connect the wires to two output pins, and arrange for one of them to be on, and the other off, the current will flow from the one that is on, through the motor, to the one that is off. This will make the motor spin. Reversing the states of the pins will make the motor spin the other way. Turning both pins on, or both pins off, will stop the motor.

That might be enough control for many projects, but we can do better. We can use Pulse Width Modulation on the pins to control the motor speed. Just like making an LED dim and brighten, we can make the motor slow down or speed up, using PWM. We can do the PWM in software and use any output pins, or we can use the output pins that support hardware PWM.

Motor Forward and Reverse

Left Right Off

Left Right Off using digitalWrite

Faster and Slower