0
0
AutocadHow-ToBeginner · 4 min read

How to Use Stepper Motor with Arduino: Simple Guide and Example

To use a stepper motor with Arduino, connect the motor wires to a driver or directly to Arduino pins, then use the Stepper library to control steps and speed. Initialize the motor with the number of steps per revolution and call step() to rotate it by a specific number of steps.
📐

Syntax

The Stepper library in Arduino helps control stepper motors easily. You create a Stepper object by specifying the number of steps per revolution and the pins connected to the motor. Use setSpeed(rpm) to set how fast the motor turns, and step(steps) to move the motor by a number of steps (positive for one direction, negative for the other).

  • Stepper(stepsPerRevolution, pin1, pin2, pin3, pin4): Creates motor object.
  • setSpeed(rpm): Sets motor speed in revolutions per minute.
  • step(steps): Moves motor by steps.
arduino
#include <Stepper.h>

Stepper myStepper(200, 8, 9, 10, 11);

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

void loop() {
  myStepper.step(100); // move 100 steps forward
  delay(1000);
  myStepper.step(-100); // move 100 steps backward
  delay(1000);
}
💻

Example

This example shows how to rotate a stepper motor forward and backward continuously using the Arduino Stepper library. It assumes a 4-wire stepper motor connected to pins 8, 9, 10, and 11.

arduino
#include <Stepper.h>

const int stepsPerRevolution = 200; // depends on 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);
  // initialize serial communication:
  Serial.begin(9600);
}

void loop() {
  Serial.println("Rotating forward 200 steps");
  myStepper.step(stepsPerRevolution); // rotate one full revolution forward
  delay(1000);

  Serial.println("Rotating backward 200 steps");
  myStepper.step(-stepsPerRevolution); // rotate one full revolution backward
  delay(1000);
}
Output
Rotating forward 200 steps Rotating backward 200 steps Rotating forward 200 steps Rotating backward 200 steps ...
⚠️

Common Pitfalls

  • Wrong wiring: Connecting motor wires incorrectly can cause no movement or erratic behavior. Always check your motor datasheet for correct pin order.
  • Missing driver: Some stepper motors need a driver board (like ULN2003 or A4988) to handle current; connecting directly to Arduino pins can damage the board.
  • Incorrect steps per revolution: Using the wrong number of steps per revolution in the code will cause inaccurate rotation angles.
  • Speed too high: Setting speed too high can cause the motor to skip steps or stall.
arduino
/* Wrong way: Missing driver and wrong pins */
// Stepper myStepper(200, 2, 3, 4, 5); // pins not connected properly

/* Right way: Use correct pins and driver */
Stepper myStepper(200, 8, 9, 10, 11); // pins connected to driver controlling motor coils
📊

Quick Reference

Here is a quick summary to control a stepper motor with Arduino:

ActionFunction/MethodDescription
Create motor objectStepper(steps, pin1, pin2, pin3, pin4)Defines motor with steps per revolution and pins
Set speedsetSpeed(rpm)Sets motor speed in revolutions per minute
Move motorstep(steps)Moves motor by given steps (positive or negative)
Delaydelay(ms)Waits to allow motor to complete movement

Key Takeaways

Use the Arduino Stepper library to control stepper motors easily with setSpeed() and step() methods.
Connect motor wires correctly and use a driver board if needed to protect your Arduino.
Set the correct number of steps per revolution for your motor to ensure accurate rotation.
Avoid setting speed too high to prevent missed steps or motor stalling.
Test your setup with simple forward and backward rotations before complex movements.