Arduino - Control 28BYJ-48 Stepper Motor using ULN2003 Driver

In this tutorial, we are going to learn:

Arduino ULN2003 28BYJ-48 stepper motor

Stepper motors are great motors for position control. The stepper motors divide a full revolution into a number of equal “steps”. They are used in many devices such as printer, 3D printer, CNC machines, and used industrial automation.

One of the inexpensive way to learn about stepper motors is to use 28BYJ-48 stepper motors. They usually come with a ULN2003 based driver board which makes them super easy to use.

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×28BYJ-48 stepper motor + ULN2003 Driver Module
1×5V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About 28BYJ-48 Stepper Motor

According to the data sheet, when the 28BYJ-48 motor runs in full-step mode, each step corresponds to a rotation of 11.25°. That means there are 32 steps per revolution (360°/11.25° = 32).

In addition, the motor has a 1/64 reduction gear set. It means it that it actually has 32 x 64 = 2048 steps. Each step is equivalent to 360°/2048 = 0.1758°.

Conclusion: if motor do 2048 steps (in full-step mode), the motor rotate one revolution

Pinout

28BYJ-48 stepper motor includes 5 pins. We do not need to care detail about these pins. We just need to plug it to the connector of ULN2003 motor driver.

28BYJ-48 stepper motor

About ULN2003 Stepper Motor Driver Module

The ULN2003 is one of the most common motor driver Module for stepper motor.

  • The module has four LEDs that show activity of four control input lines (to indicate stepping state). They provide a splendid effect when stepping.
  • The module also comes with an ON/OFF jumper to isolate power to the stepper motor.

ULN2003 Pinout

ULN2003 Stepper Motor Driver Pinout

ULN2003 module includes 6 pins and one female connector:

  • IN1 pin: is used to drive the motor. Connect it to an output pin on Arduino.
  • IN2 pin: is used to drive the motor. Connect it to an output pin on Arduino.
  • IN3 pin: is used to drive the motor. Connect it to an output pin on Arduino.
  • IN4 pin: is used to drive the motor. Connect it to an output pin on Arduino.
  • GND pin: is a common ground pin. It MUST connect to both GNDs of Arduino and the external power supply.
  • VDD pin: supplies power for the motor. Connect it to the external power supply.
  • Motor Connector: this is where the motor plugs into.

※ NOTE THAT:

  • The voltage of the external power supply should be equal to the voltage of stepper motor. For example, if a stepper motor works with 12V DC, we need to use a 12V power supply. In case of 28BYJ-48 stepper motor, it works with 5V DC, we will use 5V power supply.
  • Even if a stepper motor requires 5V power supply, Please do NOT connect VDD pin to the 5V pin on Arduino. Instead, connect it to an external 5V power supply. That is because the stepper motor draws too much power.

Wiring Diagram

Arduino stepper motor ULN2003 driver Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Please note that, we do not need to care about wire color of stepper motor. We just need to plug male connector ( in 28BYJ-48 stepper motor) to female connector (on ULN2003 driver)

How To Program to control a stepper motor

There are three methods to control a stepper motor:

  • Full-step
  • Half-step
  • Micro-step

For simple application, we can use full-step method. The detail of three method will be present in the last part of this tutorial. Programming for these methods is complicated. Fortunately, there are many libraries did it for us. We just need to use library.

Arduino IDE has a built-in Stepper library. However, We do not recommend you to use this library because:

  • The library is blocking. It means it blocks Arduino from doing other works while it controlling the stepper motor.
  • It does not have sufficient functions.

Instead, we recommend you using the AccelStepper library. This library supports:

  • Acceleration
  • Deceleration.
  • Full-step and half-step driving.
  • Multiple simultaneous steppers, with independent concurrent stepping on each stepper.
  • Disadvantage: NOT support micro-step driving

Arduino Code

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-controls-28byj-48-stepper-motor-using-uln2003-driver */ // Include the AccelStepper Library #include <AccelStepper.h> // define step constant #define FULLSTEP 4 #define STEP_PER_REVOLUTION 2048 // this value is from datasheet // Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence AccelStepper stepper(FULLSTEP, 11, 9, 10, 8); void setup() { Serial.begin(9600); stepper.setMaxSpeed(1000.0); // set the maximum speed stepper.setAcceleration(50.0); // set acceleration stepper.setSpeed(200); // set initial speed stepper.setCurrentPosition(0); // set position stepper.moveTo(STEP_PER_REVOLUTION); // set target position: 64 steps <=> one revolution } void loop() { // change direction once the motor reaches target position if (stepper.distanceToGo() == 0) stepper.moveTo(-stepper.currentPosition()); stepper.run(); // MUST be called in loop() function Serial.print(F("Current Position: ")); Serial.println(stepper.currentPosition()); }

Quick Steps

  • Navigate to the Libraries icon on the left bar of the Arduino IDE.
  • Search “AccelStepper”, then find the AccelStepper library by Mike McCauley
  • Click Install button to install AccelStepper library.
Arduino AccelStepper library
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • See motor rotating. It should:
    • Rotate one revolution in clockwire direction, and then
    • Rotate two revolution in anti-clockwire direction, and then
    • Rotate two revolution in clockwire direction.

    That preccess is repeated infinitely.

    • See the result in Serial Monitor
    COM6
    Send
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

How to control a multiple 28BYJ-48 stepper motors

Let's learn how to control two stepper motor independently at the same time.

Wiring Diagram for two 28BYJ-48 stepper motors

Arduino two stepper motor ULN2003 driver Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Arduino Code for two 28BYJ-48 stepper motors

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-controls-28byj-48-stepper-motor-using-uln2003-driver */ // Include the AccelStepper Library #include <AccelStepper.h> // define step constant #define FULLSTEP 4 #define STEP_PER_REVOLUTION 2048 // this value is from datasheet // Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence AccelStepper stepper_1(FULLSTEP, 11, 9, 10, 8); AccelStepper stepper_2(FULLSTEP, 7, 5, 6, 4); void setup() { Serial.begin(9600); stepper_1.setMaxSpeed(1000.0); // set the maximum speed stepper_1.setAcceleration(50.0); // set acceleration stepper_1.setSpeed(200); // set initial speed stepper_1.setCurrentPosition(0); // set position stepper_1.moveTo(STEP_PER_REVOLUTION); // set target position: 64 steps <=> one revolution stepper_2.setMaxSpeed(1000.0); // set the maximum speed stepper_2.setAcceleration(50.0); // set acceleration stepper_2.setSpeed(200); // set initial speed stepper_2.setCurrentPosition(0); // set position stepper_2.moveTo(STEP_PER_REVOLUTION); // set target position: 64 steps <=> one revolution } void loop() { // change direction once the motor reaches target position if (stepper_1.distanceToGo() == 0) stepper_1.moveTo(-stepper_1.currentPosition()); if (stepper_2.distanceToGo() == 0) stepper_2.moveTo(-stepper_2.currentPosition()); stepper_1.run(); // MUST be called in loop() function stepper_2.run(); // MUST be called in loop() function Serial.print(F("stepper_1# current position: ")); Serial.println(stepper_1.currentPosition()); Serial.print(F("stepper_2# current position: ")); Serial.println(stepper_2.currentPosition()); }

Additional Knowledge

1. Stepper motor vibrates while moving

Do NOT worry if the stepper motor vibrates while moving. This is a characteristic of the stepper motor. We can reduce vibration by using the micro-stepping control method.

Also, because of this characteristic, if we control properly, the stepper motor can produce musical sounds as if it is a musical instrument. You can see this project on Arduino Project Hub.

2. Method of controlling stepper motors

  • Full-step: The unit of moving is one step, which is equivalent a value of degree specified in stepper motor's datasheet or manual.
  • Half-step: divides each full step into two smaller steps. The unit of moving is half of the full step. This method allows the motor move with double resolution.
  • Micro-step: divides each full step into many smaller steps. The unit of moving is a fraction of the full step. The fraction can be 1/4, 1/8, 1/16, 1/32 or even more. This method allows the motor move with higher resolution. It also make motor move smoother at low speeds. The bigger dividend is, the higher resolution and the smoother motion is.

For example, If the motor's datasheet specifies 1.8 degree/step:

  • Full-step: The motor can move with 1.8 degree/step <⇒ 200 steps/ revolution
  • Half-step: The motor can move with 0.9 degree/step <⇒ 400 steps/ revolution
  • Micro-step: The motor can move with 0.45, 0.225, 1125‬, 0.05625 degree/step <⇒ 800, 1600, 3200, 6400... steps/ revolution

The above code used the full-step control method.

3. Resonance Issue

This is the advanced usages. The beginners do NOT need to pay attention to it. This happens in a speed range, in which the step rate equals the motor’s natural frequency. There may be an audible change in noise made by the motor, as well as an increase in vibration. In real applications, the developer SHOULD pay attention to this issue.

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

WARNING

Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.

The Best Arduino Starter Kit

※ OUR MESSAGES