Bird
0
0
Arduinoprogramming~5 mins

Stepper motor basics in Arduino

Choose your learning style9 modes available
Introduction

A stepper motor moves in small steps, letting you control its position exactly. This helps when you want precise movement, like turning a wheel to a certain angle.

When you want to turn something by exact amounts, like a robot arm.
When you need to move a camera smoothly to a specific spot.
When building a 3D printer that needs precise control of parts.
When controlling a dial or knob that must stop at set points.
Syntax
Arduino
Stepper stepsPerRevolution(steps, pin1, pin2, pin3, pin4);

void setup() {
  stepsPerRevolution.setSpeed(rpm);
}

void loop() {
  stepsPerRevolution.step(number_of_steps);
}

stepsPerRevolution is an object that controls the motor.

You tell it how many steps make a full turn and which pins connect to the motor.

Examples
This example moves the motor 100 steps forward, waits, then 100 steps backward, repeating.
Arduino
Stepper myStepper(200, 8, 9, 10, 11);

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

void loop() {
  myStepper.step(100);
  delay(500);
  myStepper.step(-100);
  delay(500);
}
This moves the motor 50 steps forward at 30 RPM, then pauses for 1 second.
Arduino
Stepper motor(100, 2, 3, 4, 5);

void setup() {
  motor.setSpeed(30);
}

void loop() {
  motor.step(50);
  delay(1000);
}
Sample Program

This program moves the stepper motor 100 steps forward, waits 1 second, then 100 steps backward, and repeats. It also prints messages to the Serial Monitor to show what it is doing.

Arduino
#include <Stepper.h>

const int stepsPerRevolution = 200;

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

void setup() {
  myStepper.setSpeed(60); // 60 RPM
  Serial.begin(9600);
  Serial.println("Stepper motor test start");
}

void loop() {
  Serial.println("Moving 100 steps forward");
  myStepper.step(100);
  delay(1000);
  Serial.println("Moving 100 steps backward");
  myStepper.step(-100);
  delay(1000);
}
OutputSuccess
Important Notes

Make sure your motor driver can handle the current your stepper motor needs.

Use the correct pins on your Arduino that support the Stepper library.

Delays help the motor finish moving before the next command.

Summary

Stepper motors move in small, exact steps for precise control.

You control them by telling how many steps to move and how fast.

Arduino's Stepper library makes it easy to use stepper motors with just a few commands.