Bird
0
0
Arduinoprogramming~10 mins

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

Choose your learning style9 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.