Stepper Motor
Stepper Motor
What I wanted to learn, and why it interested me: I wanted to learn about the stepper motor because it seemed like a challenge compared to the hobby servo motor we covered before. It interested me because its structure seemed more complex, and the electrical components seemed fun to play with. Moreover, it has a smoother and more delicate movement compared to the hobby servo motor.
Final outcome: I got to my final build through a wiring process that was very challenging for me, along with learning how to code with the stepper motor. Wiring up the stepper motor was quite complex for me, matching all of the respective wires with their regions and learning what all the pins do. I decided on my final creative/exploratory output due to how I saw the stepper motor moves, along with the potentiometer controls.
Images of final creative/exploratory output
Close-up on the wiring of the stepper motor and the A4988 driver.
The final mechanism for my project; the stepper motor swings from left to right in a speed based on the potentiometer reading.
Process images from development towards creative/exploratory output
The process of wiring up the stepper motor with the A4988 driver.
Studying up on the corresponding pins for the wires and the 4988 driver on the breadboard.
Process and reflection:
For this project, I began by focusing on correctly wiring the A4988 stepper motor driver to the Arduino Uno. Before connecting anything to power, I carefully reviewed the driver’s pinout and compared it to both the datasheet and example schematics. I first connected the logic side pins to the Arduino and ensured that RESET and SLEEP were tied together and pulled high so the driver would remain enabled. After that, I connected the motor coils to the output pins, which required identifying the correct wire pairs. Throughout the process, I learned how sensitive the driver is to correct wiring and power management. One of the biggest surprises was how easily the motor would vibrate instead of rotate if even one connection was incorrect. I also gained a stronger understanding of the difference between logic power and motor power, which initially confused me. Overall, I now feel much more confident working with stepper motors and external drivers.
Technical details
Electrical schematic: The 12V power came from an external powersupply connected to a barrel jack, not drawn.
/*
60-223 Intro to Physical Computing, fall 2025
Domain-specific Skill Building exercise: Stepper Motor
This sketch mainly connects to stepper motor through the A4988
motor driver. We first read in the inputs through the step and direction
pins from the A4988 driver, and then the motor knocks from side to
side. Then, the potentiometer is able to read in input values to
control the speed of the swinging.
Pin mapping:
Arduino pin | role | details
------------------------------
2 input step pin
3 input direction pin
A0 output potentiometer reading
Released to the public domain by the author, January 2024
Robert Zacharias, rzachari@andrew.cmu.edu
*/
#include <AccelStepper.h>
const int STEP_PIN = 2;
const int DIR_PIN = 3;
const int POT_PIN = A0;
AccelStepper myMotor(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);
// metronome swings
const long SWING_STEPS = 30;
long targetPos = SWING_STEPS;
void setup() {
Serial.begin(9600);
// defaults
myMotor.setAcceleration(800);
myMotor.setMaxSpeed(600);
myMotor.setCurrentPosition(0);
myMotor.moveTo(targetPos);
}
void loop() {
// read pot
int potRaw = analogRead(POT_PIN);
// map pot to speed range
int speedVal = map(potRaw, 0, 1023, 150, 1200);
speedVal = constrain(speedVal, 150, 1200);
myMotor.setMaxSpeed(speedVal);
// metronome behavior
if (myMotor.distanceToGo() == 0) {
targetPos = -targetPos;
myMotor.moveTo(targetPos);
}
myMotor.run();
// debugging
static unsigned long lastPrint = 0;
if (millis() - lastPrint > 250) {
lastPrint = millis();
Serial.print("pot=");
Serial.print(potRaw);
Serial.print(" maxSpeed=");
Serial.print(speedVal);
Serial.print(" pos=");
Serial.println(myMotor.currentPosition());
}
}