Stepper Motors
Overview
Program a stepper motor to enter a combination on a paper dial. Build relative moves, track position, then move to specific dial numbers.
Builds on
Before You Start
Read the motor reference and StepStick pin and current-limit guidance. Have Mr. Willis set the driver’s current limit before attaching the motor. Use full stepping throughout this lab.
Materials
- Arduino Uno R3, USB cable, and Arduino IDE
- StepperOnline 17HS19-2004S1 four-wire motor and HiLetgo A4988 StepStick with heat sink
- 12 V barrel jack screw-terminal adapter
- 6 male-to-female jumper wires and 1 female-to-female jumper wire
- One 10 kΩ resistor (four-band color code: brown–black–orange–gold)
- Teacher-approved regulated 12 V DC supply (at least 1 A capacity for this unloaded trial)
- Paper combination-lock dial sheet (PDF): print on US Letter at 100% / Actual Size; cut out one 40 mm dial per motor
- Tape, scissors, and a tool for making the center hole
- Multimeter and insulated adjustment tool for teacher use
Safety
Disconnect both power sources before rewiring
Disconnect USB and 12 V power before changing connections. Never connect 12 V to Arduino 5V or driver VDD. Secure the motor and keep clear of its moving shaft. If it stalls or the driver becomes excessively hot, disconnect power before investigating.
You've got it when…
- Your wiring and teacher-checked current limit match the documented hardware.
- You explain why each dial-number interval takes five pulses.
- Your
moveDialfunction accepts a distance in dial numbers and a direction. - Your
currentPositionvariable tracks the expected dial position from 0–39. - Your
goToDialfunction usesmoveDialto reach a target number in the requested direction. - Your sketch enters the combination with the required turns and pauses.
Collaboration & AI
Build and troubleshoot with a partner. Each student must explain the circuit and paste their own completed code into their notebook.
AI — AIAS Level 1, No AI: Complete the code changes and explanations yourself without AI assistance. AI Assessment Scale
Build the Circuit
-
Disconnect all power. Wire the circuit below; use the Arduino Pinout to locate pins. Match the driver’s printed pin labels; its potentiometer faces the bottom in the diagram.
Click to enlarge. Wire crossings without dots are not connections.
From To Uno D2 STEP Uno D2 / STEP line Unused Uno GND in the top digital header through a 10 kΩ resistor Uno D3 DIR Uno 5V VDD RESET SLEEP (jumper between the two pins) Uno GND Driver GND beside VDD ENABLE Leave unconnected; the HiLetgo board pulls it LOW (outputs enabled) MS1, MS2, MS3 Leave unconnected; internal pull-downs select full stepping 12 V barrel adapter positive (+) VMOT only 12 V barrel adapter negative (−) Driver GND beside VMOT Motor black (A+) / green (A−) 2B / 2A Motor red (B+) / blue (B−) 1A / 1B Fit the motor connector directly over
2B,2A,1A,1B, with black, green, red, blue wires from top to bottom. The driver’s GND pins connect internally; no jumper between them is needed.Connect the 10 kΩ resistor between the STEP pin and a GND pin on the Arduino. The resistor can go either direction.
Why the pull-down resistor?
During startup, reset, and uploading, STEP can float and pick up noise that causes unwanted steps. The resistor holds STEP LOW until the Arduino takes control. Only STEP needs this resistor in our tested circuit.
-
Cut out the paper dial, make a center hole, slide it over the shaft, and tape it to the motor face. Add a small tape pointer to the shaft; keep it free to rotate.
Make a Full Turn
The motor needs 200 pulses per revolution; the dial has 40 positions (0–39). Each number interval therefore takes 200 ÷ 40 = 5 pulses, which is why the loop uses count * 5.
moveDial(count, dir) moves a distance from the current position: 40 intervals make a full turn; 10 make a quarter turn. Use a nonnegative count. dir selects the direction, and HALF_PERIOD_US controls speed.
-
With motor power off, upload the sketch below as
stepper_motor.inoand wait for it to finish running. Turn on motor power, let the shaft settle, note the pointer’s position, and press the Reset button on the Arduino once.stepper_motor.inoconst int STEP_PIN = 2; const int DIR_PIN = 3; const int HALF_PERIOD_US = 5000; void moveDial(int count, bool dir) { digitalWrite(DIR_PIN, dir); delayMicroseconds(5); for (int i = 0; i < count * 5; i++) { digitalWrite(STEP_PIN, HIGH); delayMicroseconds(HALF_PERIOD_US); digitalWrite(STEP_PIN, LOW); delayMicroseconds(HALF_PERIOD_US); } } void setup() { pinMode(STEP_PIN, OUTPUT); pinMode(DIR_PIN, OUTPUT); moveDial(40, true); } void loop() { // put your main code here, to run repeatedly: }
-
Check for a full turn (40 dial numbers). Change the direction from
truetofalseand repeat step 3. Record which value produces CW and CCW when viewed from the shaft end. -
With the motor stopped but powered, gently grip the shaft and try to turn it. Feel the holding torque: the energized windings resist movement. Release the shaft before continuing.
Do not force the shaft
Do not press Reset while gripping it or disturb the tape pointer.
Add Position Tracking and Absolute Moves
-
Add these global variables above the functions. Set
CWto the value you identified in step 4;CCWis its opposite.Add this code after the pulse loop, inside
moveDial. The remainder operator%keeps the position within 0–39, even after full revolutions.int change = count % 40; if (dir == CW) { currentPosition = (currentPosition + change) % 40; } else { currentPosition = (currentPosition - change + 40) % 40; }Expected position
This variable records the expected position, not a sensor measurement. Before running a positioning test, align the paper dial’s 0 with the pointer as described in the trial procedure below. Resetting the Arduino sets the variable to 0; it does not physically move the pointer to 0.
-
Add this
goToDialstarter belowmoveDial. Complete the two TODOs: use(currentPosition - target + 40) % 40for CCW distance, then callmoveDial(count, dir)after theif/else.void goToDial(int target, bool dir) { int count; if (dir == CW) { count = (target - currentPosition + 40) % 40; } else { count = 0; // TODO: Replace 0 with the CCW distance calculation } // TODO: Call moveDial with count and dir }From 0, test
goToDial(35, CCW), thengoToDial(5, CW), with a one-second pause between calls. Expect 5 intervals CCW followed by 10 CW. Use the trial procedure below.Distance or destination?
moveDial(40, CW)makes a full turn.goToDial(5, CW)stops at 5, moving zero intervals if already there. OnlymoveDialsends pulses and updatescurrentPosition; do not update it again ingoToDial.
Open the Paper Lock
-
Program 10–30–20 in
setup(). UsemoveDialfor full revolutions andgoToDialfor combination numbers. Pause one second at each number, keeploop()empty, and comment the purpose of each move.Stage Paper-pointer algorithm Establish the start Align the paper dial’s 0 with the pointer before the run; initialize currentPositionto 0.Clear and reach the first number Turn CCW three full revolutions (120 intervals), then continue CCW to 10. Reach the second number Turn CW one full revolution, then continue CW to 30. Reach the third number Turn CCW directly to 20, with no extra revolution. Finish Leave the pointer stopped at the third number. A real lock would now be ready to open. -
Run each positioning test and the final sequence using this procedure. Repeat the final sequence to verify all three stops.
Order Action Upload Leave motor power off. Upload and wait for the sketch to finish running. Power Apply motor power and let the shaft settle. Align Adjust the paper dial so 0 aligns with the pointer; secure it again. Run Press Reset once. Check the stops and directions. Establish zero before every trial
Adjust the paper, not the energized shaft. If steps are missed or the pointer slips, fix the problem and realign zero before rerunning.
Turn It In
- Disconnect motor power and USB. In a notebook entry titled Stepper Motors, paste your complete Arduino code that unlocks the paper lock using
moveDial,currentPosition, andgoToDial.
- Complete sketch, including
moveDial,currentPosition,goToDial, and the combination sequence insetup() - Code comments identifying the combination, direction convention, position tracking, and required full revolutions
How It's Graded
One score from 0–4 covers the submitted code.
| Score | Evidence |
|---|---|
| 4 Excellent | Complete sketch correctly converts relative moves to pulses, tracks position with wraparound, and uses goToDial through moveDial for absolute moves. The combination sequence includes the required revolutions, directions, and pauses; comments explain the position tracking and sequence. |
| 3 Above Average | Complete, working relative and absolute movement functions, position tracking, and combination sequence, with minor omissions in comments or pauses. |
| 2 Average | Some controlled dial motion is implemented, but significant errors remain in pulse conversion, position tracking, absolute moves, directions, or clearing revolutions. |
| 1 Below Average | Code is largely incomplete or nonfunctional, with little evidence of the required function or combination sequence. |
| 0 Failing | No submission or no evidence of the required work. |
Wiring illustration uses the Fritzing Uno R3 artwork under CC BY-SA 3.0, from fritzing-parts. The wiring illustration is shared under the same license. Motor and driver information is linked in the reference pages.