Controlling stepper motors
A stepper motor is a motor that moves in steps instead of constantly rotating. They are used a lot in things like printers, where a computer program needs to move something by an exact amount.
The simplest stepper motor you may have in your house is the little motor that drives a battery powered quartz clock. It has only one coil (the stepper motors in printers have two), and since it is designed to run from a 1.5 volt AA cell, it is easy to drive it from the Launchpad instead.
When we drive the clock with our computer, we can make time go as fast or as slowly as we wish. The project described here is one I call Ridgemont High. It drives the clock 50 times faster than normal, to give us "Fast Times".
It runs so fast that sometimes when it starts up, the clock gets confused and runs backwards. I can keep pushing the RESET button on the Launchpad until I see the clock running backwards. Sometimes it only takes two or three tries, other times it takes ten or more to get it to run backwards.
Building the project is fairly simple. We use a pair or wire cutters to carefully remove part of the plastic back from the clock movement. We don't want to completely open the back, since the clock gears are very hard to get back into place if we do that. But we can cut open the back until we find the coil of wire that is the heart of the motor, and keep cutting until we find the two wires where the fine motor wire is connected to the rest of the circuit.
Once we find those two wires, we solder longer wires to them, so we can connect them to Port 1 pin 4 and Port 1 pin 5 of the Launchpad.
I like to do all this cutting with the battery in the clock, and the clock running, so I know if I have damaged the clock or not.
Once the wires are soldered in place, we remove the battery from the clock, and connect the wires to the Launchpad.
The program for running the clock at any speed we like is very simple:
To make the motor turn, we first run the current one way through the coil, and then the other. We do this by setting one pin high and the other low, and then reversing the pattern (so the high pin is now low, and the low pin is now high). Each time we reverse the current, the second hand moves by one tick.
We use the exclusive-or operator to flip the bits:
P1OUT ^= 0b110000;
That line says to take any bit in pin 4 or 5 that has the value one and set that to zero. At the same time, any bit that was zero is set to 0.
One of the great things about the little motor in the clock is that it uses very little power. It can run from the AA cell for a year or more. So if you have something you want to make move that does not need a powerful motor, such as a paper sculpture or a faceted crystal you want to place in a sunny window to make rainbows move around the room, you can modify the clock motor to run it, and set the speed with the Launchpad.
That said, when we are running the motor at 50 times its normal speed, it will use more power. To save power when running at normal speed, you could change the program so that the motor was only powered on for 20 milliseconds each second. But 20 milliseconds seems to be close to the minimum time needed for this particular motor.
A more elaborate program could change the speed of the motor depending on some change in the environment, or simply on the time of day. You could build a clock that ran normally until 9:00 am, whereupon it would speed up, until lunchtime. Then it slows down a lot, until the clock reads 1:00 pm, when it would race forward again until 5:00 pm. The perfect clock for the clock-watching worker who wants a long lunch hour.