How to Use Stepper Library in Arduino: Simple Guide
To use the
Stepper library in Arduino, first include it with #include <Stepper.h>, then create a Stepper object with the number of steps per revolution and pin connections. Use methods like step() to move the motor by a specific number of steps.Syntax
The basic syntax to use the Stepper library involves including the library, creating a Stepper object, and calling its methods to control the motor.
- #include <Stepper.h>: Adds the Stepper library to your sketch.
- Stepper myStepper(steps, pin1, pin2, pin3, pin4);: Creates a Stepper object where
stepsis the number of steps per revolution andpin1topin4are the Arduino pins connected to the motor driver. - myStepper.setSpeed(rpm);: Sets the speed of the motor in revolutions per minute.
- myStepper.step(stepsToMove);: Moves the motor by the specified number of steps (positive or negative).
arduino
#include <Stepper.h> const int stepsPerRevolution = 200; // change this to fit your motor // initialize the stepper library on pins 8 through 11: Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); void setup() { // set the speed at 60 rpm: myStepper.setSpeed(60); } void loop() { // step one revolution in one direction: myStepper.step(stepsPerRevolution); delay(500); // step one revolution in the other direction: myStepper.step(-stepsPerRevolution); delay(500); }
Example
This example shows how to rotate a stepper motor one full revolution clockwise, pause, then one full revolution counterclockwise, repeatedly.
arduino
#include <Stepper.h> const int stepsPerRevolution = 200; Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); void setup() { myStepper.setSpeed(60); Serial.begin(9600); Serial.println("Stepper motor control started"); } void loop() { Serial.println("Rotating clockwise one revolution"); myStepper.step(stepsPerRevolution); delay(1000); Serial.println("Rotating counterclockwise one revolution"); myStepper.step(-stepsPerRevolution); delay(1000); }
Output
Stepper motor control started
Rotating clockwise one revolution
Rotating counterclockwise one revolution
Rotating clockwise one revolution
Rotating counterclockwise one revolution
...
Common Pitfalls
- Incorrect pin connections: Make sure the motor wires match the pins declared in the Stepper constructor.
- Wrong steps per revolution: Use the exact number of steps your motor requires for one full turn.
- Not setting speed: Always call
setSpeed()before moving the motor to avoid unexpected behavior. - Power supply issues: Stepper motors need sufficient current; powering from Arduino pins alone may not work.
arduino
// Wrong way (missing setSpeed): #include <Stepper.h> const int stepsPerRevolution = 200; Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); void setup() {} void loop() { myStepper.step(stepsPerRevolution); // motor may not move properly delay(1000); } // Right way: #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); }
Quick Reference
Remember these key points when using the Stepper library:
- Include the library with
#include <Stepper.h>. - Create a Stepper object with the correct steps and pins.
- Set the motor speed before moving.
- Use
step()to move the motor by steps. - Ensure your motor has proper power supply.
Key Takeaways
Always include the Stepper library and create a Stepper object with correct pins and steps.
Set the motor speed using setSpeed() before calling step() to move the motor.
Verify your motor wiring and power supply to avoid hardware issues.
Use positive or negative values in step() to control rotation direction.
Test with simple code first to confirm motor behavior before complex projects.