Level 2Unit C · Session 20
20

Unit C · Telling a machine what you want

Exactly one turn

What we are making: a motor that turns exactly once round, then exactly once back. Not roughly — exactly.

The third kind of motor

You have met two already. A plain DC motor spins while you give it power. A servo goes to an angle between 0 and 180 and stops there. A stepper is the third kind, and it does something neither of the others can: it turns in tiny fixed increments, and it can keep going round forever while still knowing exactly how far it has gone.

This one takes 2048 steps to complete a full turn. Ask for 1024 and you get exactly half a turn. Ask for 4096 and you get exactly two. That precision is why steppers are in printers, in scanners, and in every 3D printer — including the one you will use in Unit E.

New words
StepperA motor that moves in fixed steps and counts them.
ULN2003The small driver board it plugs into. The motor cannot run without it.
Steps per revolution2048 for this motor. Not a round number, and not negotiable.
RPMRevolutions per minute. Ask for too many and it stalls.
Arduino UnoUSB to the laptop8910115VGNDULN2003 driverthe board with four red lightsIN1IN2IN3IN4+−The motor's white plug only fits the driver one way round. Push it home.
The stepper and its driver. Four signal wires, in order, plus power.
One turn, then one turn back
1#include <Stepper.h>
2 
3const int stepsPerTurn = 2048; // this motor takes 2048 steps to go round once
4 
5Stepper motor(stepsPerTurn, 8, 10, 9, 11); // note the order: 8, 10, 9, 11
6 
7void setup() {
8 motor.setSpeed(10); // revolutions per minute. Keep it slow.
9}
10 
11void loop() {
12 motor.step(stepsPerTurn); // one full turn, clockwise
13 delay(1000);
14 motor.step(-stepsPerTurn); // one full turn, back again
15 delay(1000);
16}

Do it

  1. Plug the motor's white connector into the driver board. It only fits one way.
  2. Wire the four IN pins to 8, 9, 10 and 11, and the driver's power to 5V and GND.
  3. Upload. Watch the four red lights on the driver chase each other round.
  4. Put a paper arrow on the shaft with tape, and check it really does come back to where it started.
💡 Tip
Look at the order in the Stepper line: 8, 10, 9, 11. That is not a typo and it is not a mistake. The library expects the coils in a different order from the way the pins are numbered on the driver. Put them in numerical order and the motor will buzz and shudder instead of turning. This catches nearly everybody.

Change it

  1. Change step(stepsPerTurn) to step(512). How far does it go?
  2. Set the speed to 25. Then try 60. Find the point where it stops turning properly.
  3. Make it do a quarter turn, pause, quarter turn, pause, all the way round.
What you seeWhyWhat to do
It buzzes and shudders but does not turnThe coil order is wrong.The Stepper line must read 8, 10, 9, 11. Not numerical order.
It turns very slowly whatever I do2048 steps is a lot of steps.Normal for this motor. Raise the speed a little, but see the next row.
It stalls when I speed it upSteppers lose torque as they go faster.A real limit. Find the speed where yours stops coping and stay below it.
Only two of the four lights come onA wire is loose.Check all four IN wires, and push the white plug fully home.

What you learned

  • A stepper trades speed for precision, and counts every step it takes.
  • Wiring order can be correct and still not be numerical order.
  • This is the motor inside the 3D printer you will meet in Unit E.
Challenge optional — only if you finish early
  • Make the stepper turn exactly the number of degrees somebody types on the keypad.
19. From across the room