28BYJ-48 Stepper Motor with Arduino and ULN2003 Driver

Learn how to control the 28BYJ-48 stepper motor using Arduino and the ULN2003 driver board.

What is the 28BYJ-48 Motor and ULN2003 Driver?

The 28BYJ-48 is a unipolar stepper motor with a built-in reducer, offering high precision with 4096 steps per revolution. The ULN2003 is a driver board with Darlington transistors for controlling this motor efficiently.

28BYJ-48 and ULN2003 Components

Features

Assembly Diagram

Connect the 28BYJ-48 to the ULN2003 driver board and wire it to Arduino as follows:

Connection Diagram

Code Example

This code demonstrates how to control the 28BYJ-48 motor using half-step sequences:


// Pin definitions
const int motorPin1 = 8; // IN1
const int motorPin2 = 9; // IN2
const int motorPin3 = 10; // IN3
const int motorPin4 = 11; // IN4

int motorSpeed = 1200; // Speed in microseconds
int stepCounter = 0; // Current step
const int stepsPerRev = 4076; // Steps per revolution

// Half-step sequence
const int numSteps = 8;
const int stepsLookup[8] = { B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001 };

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
}

void loop() {
  for (int i = 0; i < stepsPerRev; i++) {
    clockwise();
    delayMicroseconds(motorSpeed);
  }
  delay(1000);
  for (int i = 0; i < stepsPerRev; i++) {
    anticlockwise();
    delayMicroseconds(motorSpeed);
  }
  delay(1000);
}

void clockwise() {
  stepCounter++;
  if (stepCounter >= numSteps) stepCounter = 0;
  setOutput(stepCounter);
}

void anticlockwise() {
  stepCounter--;
  if (stepCounter < 0) stepCounter = numSteps - 1;
  setOutput(stepCounter);
}

void setOutput(int step) {
  digitalWrite(motorPin1, bitRead(stepsLookup[step], 0));
  digitalWrite(motorPin2, bitRead(stepsLookup[step], 1));
  digitalWrite(motorPin3, bitRead(stepsLookup[step], 2));
  digitalWrite(motorPin4, bitRead(stepsLookup[step], 3));
}