Bird
0
0
Arduinoprogramming~7 mins

Stepper motor with driver module in Arduino

Choose your learning style9 modes available
Introduction

A stepper motor lets you move things in small, exact steps. Using a driver module helps control the motor easily and safely.

When you want to turn a wheel or arm to a specific position, like in a robot.
When you need to control the speed and direction of a motor precisely.
When building a 3D printer or CNC machine that moves in exact steps.
When you want to open or close a valve or door in small controlled moves.
Syntax
Arduino
1. Connect stepper motor wires to the driver module.
2. Connect driver module pins to Arduino (power, ground, control pins).
3. Use Arduino code to send step and direction signals to the driver.

Example Arduino code structure:

const int stepsPerRevolution = 200;
const int stepPin = 8;
const int dirPin = 9;

void setup() {
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
}

void loop() {
  // Forward full revolution
  digitalWrite(dirPin, HIGH);
  for (int i = 0; i < stepsPerRevolution; i++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1000);
  }
  delay(1000);
  // Backward full revolution
  digitalWrite(dirPin, LOW);
  for (int i = 0; i < stepsPerRevolution; i++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1000);
  }
  delay(1000);
}

Stepper motors move in fixed steps, so you control how many steps to turn.

The driver module handles the power and switching for the motor coils.

Examples
This example moves the motor 100 steps forward and then 100 steps back repeatedly.
Arduino
const int stepsPerRevolution = 200;
const int stepPin = 8;
const int dirPin = 9;

void setup() {
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
}

void loop() {
  // Move half revolution forward
  digitalWrite(dirPin, HIGH);
  for (int i = 0; i < stepsPerRevolution / 2; i++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
  }
  delay(500);
  // Move half revolution backward
  digitalWrite(dirPin, LOW);
  for (int i = 0; i < stepsPerRevolution / 2; i++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
  }
  delay(500);
}
This example uses the AccelStepper library for smoother acceleration and deceleration with a driver module.
Arduino
#include <AccelStepper.h>

AccelStepper stepper(AccelStepper::DRIVER, 3, 4);

void setup() {
  stepper.setMaxSpeed(1000);
  stepper.setAcceleration(500);
}

void loop() {
  stepper.moveTo(200);
  stepper.runToPosition();
  delay(1000);
  stepper.moveTo(0);
  stepper.runToPosition();
  delay(1000);
}
Sample Program

This program controls a stepper motor using a driver module connected to two pins: one for step pulses and one for direction. It moves the motor 200 steps forward, waits, then 200 steps backward, and repeats.

Arduino
#include <Stepper.h>

const int stepsPerRevolution = 200;
// Pins connected to the driver module inputs
const int stepPin = 3;
const int dirPin = 4;

void setup() {
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("Stepper motor control start");
}

void loop() {
  // Rotate motor 200 steps forward
  digitalWrite(dirPin, HIGH); // set direction forward
  for (int i = 0; i < 200; i++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1000);
  }
  Serial.println("Moved 200 steps forward");
  delay(1000);

  // Rotate motor 200 steps backward
  digitalWrite(dirPin, LOW); // set direction backward
  for (int i = 0; i < 200; i++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1000);
  }
  Serial.println("Moved 200 steps backward");
  delay(1000);
}
OutputSuccess
Important Notes

Make sure your power supply matches the motor and driver requirements to avoid damage.

Use delays carefully; too fast stepping can cause the motor to miss steps.

Check wiring twice before powering up to prevent short circuits.

Summary

Stepper motors move in small, exact steps controlled by pulses.

Driver modules make controlling stepper motors easier and safer.

Arduino code sends step and direction signals to move the motor as needed.