Bird
Raised Fist0
Arduinoprogramming~10 mins

Stepper motor basics in Arduino - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Stepper motor basics
Start
↓
Initialize motor pins
↓
Set motor speed
↓
Loop start
↓
Step motor one step
↓
Delay for speed control
↓
Repeat loop
This flow shows how an Arduino program controls a stepper motor by initializing pins, setting speed, and stepping the motor repeatedly.
Execution Sample
Arduino
#include <Stepper.h>
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  myStepper.setSpeed(60);
}

void loop() {
  myStepper.step(stepsPerRevolution);
  delay(1000);
}
This code makes the stepper motor turn one full revolution at 60 RPM, then waits 1 second, and repeats.
Execution Table
StepActionVariable/FunctionValue/ResultNotes
1Initialize steps per revolutionstepsPerRevolution200Defines steps for one full turn
2Create Stepper objectmyStepperStepper(200, 8, 9, 10, 11)Assigns pins and steps
3Setup: set speedmyStepper.setSpeed(60)Speed set to 60 RPMMotor speed configured
4Loop startloop()Start loopBegin repeating steps
5Step motormyStepper.step(200)Motor turns 1 revolutionMotor moves 200 steps
6Delaydelay(1000)Wait 1000 msPause before next turn
7Loop repeatsloop()Repeat steps 5-6Continuous rotation
💡 Loop runs forever, motor keeps turning one revolution every 2 seconds
Variable Tracker
Variable/FunctionStartAfter Step 3After Step 5After Step 7 (Loop)
stepsPerRevolution200200200200
myStepper speedundefined60 RPM60 RPM60 RPM
myStepper position0 steps0 steps200 steps400 steps
Key Moments - 3 Insights
Why does the motor turn exactly one full revolution?
Because we call myStepper.step(200), and 200 is the number of steps per revolution (see execution_table row 5).
What controls how fast the motor turns?
The speed is set by myStepper.setSpeed(60) in setup (execution_table row 3), which sets the motor speed to 60 RPM.
Why is there a delay after stepping the motor?
The delay(1000) pauses the program for 1 second (execution_table row 6), so the motor waits before turning again, making the motion visible.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what value is assigned to stepsPerRevolution at step 1?
A200
B60
C1000
D8
💡 Hint
Check the first row of the execution_table where stepsPerRevolution is initialized.
At which step does the motor complete one full revolution?
AStep 3
BStep 5
CStep 6
DStep 7
💡 Hint
Look at the action 'Step motor' in the execution_table.
If we change setSpeed(60) to setSpeed(120), what happens to the motor speed?
AMotor speed stays the same
BMotor speed halves
CMotor speed doubles
DMotor stops
💡 Hint
Refer to the variable_tracker row for myStepper speed and execution_table row 3.
Concept Snapshot
Stepper motor basics in Arduino:
- Define steps per revolution (e.g., 200)
- Create Stepper object with pins
- Set motor speed with setSpeed(RPM)
- Use step(n) to move motor n steps
- Use delay() to control timing
- Loop to repeat motion continuously
Full Transcript
This visual execution trace shows how an Arduino program controls a stepper motor. First, it defines the number of steps per revolution as 200. Then, it creates a Stepper object with four control pins. In setup, it sets the motor speed to 60 RPM. The loop repeatedly commands the motor to step 200 steps, which equals one full revolution, then waits one second before repeating. Variables like stepsPerRevolution and motor speed remain constant, while the motor position increases by 200 steps each loop. Key moments include understanding how step count controls rotation, how speed affects turning rate, and why delay is needed to see the motion clearly. The quiz checks understanding of these steps and effects of changing speed.

Practice

(1/5)
1. What is the main advantage of using a stepper motor in Arduino projects?
easy
A. It moves in precise, small steps for accurate positioning.
B. It runs faster than all other motors.
C. It uses less power than a servo motor.
D. It requires no programming to control.

Solution

  1. Step 1: Understand stepper motor movement

    Stepper motors move in small, fixed steps, allowing precise control of position.
  2. Step 2: Compare with other motors

    Unlike regular motors, stepper motors do not rotate freely but in exact increments.
  3. Final Answer:

    It moves in precise, small steps for accurate positioning. -> Option A
  4. Quick Check:

    Stepper motor precision = Accurate small steps [OK]
Hint: Stepper motors move in fixed steps for precision [OK]
Common Mistakes:
  • Thinking stepper motors run faster than all motors
  • Assuming no programming is needed
  • Confusing power usage with motor type
2. Which of the following is the correct way to create a Stepper object in Arduino code?
easy
A. Stepper myStepper = new Stepper(pin1, pin2, pin3, pin4);
B. Stepper myStepper = Stepper(stepsPerRevolution, pin1, pin2);
C. Stepper myStepper(stepsPerRevolution);
D. Stepper myStepper(stepsPerRevolution, pin1, pin2, pin3, pin4);

Solution

  1. Step 1: Recall Stepper constructor syntax

    The Stepper library requires the number of steps per revolution and four control pins.
  2. Step 2: Match syntax with options

    Stepper myStepper(stepsPerRevolution, pin1, pin2, pin3, pin4); correctly uses Stepper(stepsPerRevolution, pin1, pin2, pin3, pin4) to create the object.
  3. Final Answer:

    Stepper myStepper(stepsPerRevolution, pin1, pin2, pin3, pin4); -> Option D
  4. Quick Check:

    Stepper constructor needs steps and 4 pins [OK]
Hint: Stepper needs steps and 4 pins in constructor [OK]
Common Mistakes:
  • Using only 2 pins instead of 4
  • Trying to use 'new' keyword in Arduino
  • Omitting steps per revolution
3. What will be the output behavior of this Arduino code snippet controlling a stepper motor?
Stepper myStepper(200, 8, 9, 10, 11);
void setup() {
  myStepper.setSpeed(60);
}
void loop() {
  myStepper.step(100);
  delay(1000);
  myStepper.step(-100);
  delay(1000);
}
medium
A. The motor rotates 100 steps forward, pauses 1 second, then 100 steps backward, pauses 1 second, and repeats.
B. The motor rotates continuously forward without stopping.
C. The motor rotates 100 steps backward only once.
D. The code will cause a compilation error due to missing pinMode setup.

Solution

  1. Step 1: Analyze step commands in loop()

    The code commands the motor to move 100 steps forward, then 100 steps backward, each followed by a 1-second delay.
  2. Step 2: Understand loop repetition

    Since this is in loop(), the forward and backward steps repeat indefinitely with pauses.
  3. Final Answer:

    The motor rotates 100 steps forward, pauses 1 second, then 100 steps backward, pauses 1 second, and repeats. -> Option A
  4. Quick Check:

    Stepper steps forward and backward with delay [OK]
Hint: Positive steps move forward; negative steps move backward [OK]
Common Mistakes:
  • Thinking motor runs continuously forward
  • Assuming missing pinMode causes error
  • Ignoring the delay pauses
4. Identify the error in this Arduino code controlling a stepper motor:
Stepper myStepper(200, 8, 9, 10, 11);
void setup() {
  myStepper.setSpeed(60);
}
void loop() {
  myStepper.step(50);
  delay(500);
}
medium
A. The stepper motor pins are not set as outputs using pinMode.
B. There is no error; the code will run correctly.
C. The step() function requires a negative number to move forward.
D. The setSpeed() function is called in setup(), which is incorrect.

Solution

  1. Step 1: Check pinMode necessity for Stepper library

    The Stepper library internally handles pin modes, so explicit pinMode calls are not required.
  2. Step 2: Verify setSpeed and step usage

    Calling setSpeed() in setup() and step() in loop() is correct usage for controlling speed and movement.
  3. Final Answer:

    There is no error; the code will run correctly. -> Option B
  4. Quick Check:

    Stepper library manages pins; code is valid [OK]
Hint: Stepper library sets pins automatically; no pinMode needed [OK]
Common Mistakes:
  • Adding unnecessary pinMode calls
  • Misunderstanding setSpeed() placement
  • Confusing step() sign for direction
5. You want to rotate a stepper motor exactly one full revolution at 120 RPM using Arduino's Stepper library. The motor has 200 steps per revolution. Which code snippet correctly achieves this in the loop() function?
hard
A. myStepper.setSpeed(60); myStepper.step(200);
B. myStepper.setSpeed(120); myStepper.step(100);
C. myStepper.setSpeed(120); myStepper.step(200);
D. myStepper.setSpeed(120); myStepper.step(-200);

Solution

  1. Step 1: Calculate steps for one revolution

    The motor requires 200 steps for one full revolution.
  2. Step 2: Set speed and steps correctly

    To rotate at 120 RPM, setSpeed(120) is correct, and step(200) moves one full revolution forward.
  3. Final Answer:

    myStepper.setSpeed(120); myStepper.step(200); -> Option C
  4. Quick Check:

    200 steps = 1 revolution; speed 120 RPM [OK]
Hint: Steps = motor steps/rev; speed = desired RPM [OK]
Common Mistakes:
  • Using half the steps (100) for full revolution
  • Setting speed lower than desired RPM
  • Using negative steps when forward rotation needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard