Arduino stepper motor – a tutorial

This tutorial shows how to explore an inexpensive stepper motor and driver board using an Arduino and included stepper library.

The stepper motor and driver board can be purchased on eBay at very low cost, and is an ideal educational project for the budding Arduino practitioner.

Fig01
Fig 1

Fig 1 shows the test setup:

  • at the top is a Arduino Pro 328 with Opti bootloader and FTDI adapter for programming;
  • middle is a Logic Shrimp for logic tracing;
  • bottom left is a 28BYJ-48 stepper motor; and
  • bottom right is a driver module based on UNL2003.

The stepper motor and driver module were purchased on eBay for less than $2 including post, but price seems to have crept a bit lately. The motor has a 5.6° step angle and 64:1 gear box, and is a unipolar (five wires). Some of the driver modules being sold do not have the four diagnostic LEDs, but they are worth having.

The motor is powered from +5V with a link on the driver board. The whole thing will run off USB power, but it is probably better to power the Arduino board as is done in this case.

The jumper wires are readily available on eBay and from some robotics shops.

The project uses the stepper library module in Arduino. The algorithms in that module will achieve only 32 steps per turn of the motor shaft, 32*64 per turn of the output shaft. The program code is derived from a sample file provided with Arduino.

Fig 2
Fig 2

Fig 2 shows the Arduino IDE window. For this exercise, the rotation is set to one eight of a turn for convenience in capture of the drive waveform.

Importantly, note the order that the pins are listed in the statement that creates myStepper. Physically, the wires are connected 2-IN1, 3-IN2, 4-IN3, 5-IN4. Other connections may not work properly.

Fig 3
Fig 3

Fig 3 shows the input signals to the driver module, captured using a Logic Shrimp and displayed on OLS. The capture includes a clockwise rotation, the 100ms rest time, and then the anti-clockwise rotation.

Fig 4
Fig 4

Fig 4 zooms in on the clockwise drive waveforms.

The code

#include "Stepper.h"

const int stepsPerRevolution = 64/2*64;

// initialize the stepper library on pins:
Stepper myStepper(stepsPerRevolution,2,4,3,5);            

void setup() {
  myStepper.setSpeed(6);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
   Serial.println("clockwise");
  myStepper.step(stepsPerRevolution/1);
  delay(100);
   // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution/1);
  delay(100); 
}

Links

Changes

Version Date Description
1.01 24/09/2013 Initial.
1.02  20/07/2015 Copied from VK1OD.net
1.03
1.04
1.05