Arduino Nano - 28BYJ-48 Stepper Motor ULN2003 Driver

This tutorial instructs you how to use Arduino Nano to control 28BYJ-48 Stepper Motor using ULN2003 Driver. In detail, we will learn:

Arduino Nano ULN2003 28BYJ-48 stepper motor

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
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 Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of 28BYJ-48 Stepper Motor

Stepper motors are excellent for position control. They divide a complete revolution into a number of equal "steps". These motors are used in many devices, such as printers, 3D printers, CNC machines, and industrial automation.

One of the budget-friendly methods to gain knowledge about stepper motors is to utilize 28BYJ-48 stepper motors. These typically come with a ULN2003 based driver board, which makes them incredibly easy to use.

As indicated in the data sheet, the 28BYJ-48 motor operates in full-step mode with each step corresponding to an 11.25° rotation. Therefore, there are 32 steps per revolution (360°/11.25° = 32).

Furthermore, the motor has a 1/64 reduction gear set. This translates to 32 x 64 = 2048 steps. Each step is equivalent to 360°/2048 = 0.1758°.

Conclusion: If the motor is set to full-step mode, it will take 2048 steps for it to complete one revolution.

The 28BYJ-48 Stepper Motor using ULN2003 Driver Pinout

The 28BYJ-48 stepper motor has 5 pins. It is not necessary to be concerned with the specifics of these pins; they simply need to be connected to the ULN2003 motor driver connector.

28BYJ-48 stepper motor

Overview of ULN2003 Stepper Motor Driver

The ULN2003 is a widely used motor driver Module for stepper motors.

  • It has four LEDs that demonstrate the activity of the four control input lines, which give a great visual effect when the motor is stepping.
  • Additionally, it includes an ON/OFF jumper to separate power from the stepper motor.

ULN2003 Pinout

ULN2003 Stepper Motor Driver pinout

The ULN2003 module has 6 pins and a female connector:

  • IN1 pin: this is used to drive the motor. It should be connected to an output pin on the Arduino Nano board.
  • IN2 pin: this is used to drive the motor. It should be connected to an output pin on the Arduino Nano board.
  • IN3 pin: this is used to drive the motor. It should be connected to an output pin on the Arduino Nano board.
  • IN4 pin: this is used to drive the motor. It should be connected to an output pin on the Arduino Nano board.
  • GND pin: this is the common ground pin. It must be connected to both the GNDs of the Arduino Nano board and the external power supply.
  • VDD pin: this supplies power for the motor. It should be connected 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 the case of 28BYJ-48 stepper motor, it operates with 5V DC, so we will use a 5V power supply.
  • However, even if the stepper motor requires a 5V power supply, please do NOT connect the VDD pin to the 5V pin on the Arduino Nano. Instead, connect it to an external 5V power supply, as the stepper motor draws too much power.

Wiring Diagram

The wiring diagram between Arduino Nano and stepper motor ULN2003 driver

This image is created using Fritzing. Click to enlarge image

It is not necessary to pay attention to the colour of the wires on the stepper motor. All that is required is to connect the male connector on the 28BYJ-48 stepper motor to the female connector on the ULN2003 driver.

How To Program to control a stepper motor

There are three ways to regulate a stepper motor:

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

For basic applications, we can use the full-step approach. The specifics of the three methods will be outlined in the final section of this tutorial. Programming these techniques can be complex. Fortunately, there are many libraries that have done the work for us, so we just need to utilize them.

The Arduino IDE features a built-in Stepper library. However, we do not suggest you to utilize this library as:

  • It is a blocking library, meaning it prevents the Arduino Nano from performing other tasks while controlling the stepper motor.
  • It does not provide enough functions.

Instead, we suggest that you utilize the AccelStepper library. This library offers:

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

Arduino Nano Code

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-28byj-48-stepper-motor-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()); }

Detailed Instructions

  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “AccelStepper”, then locate the AccelStepper library created by Mike McCauley.
  • Click the Install button to add the AccelStepper library.
Arduino Nano AccelStepper library
  • Copy the code above and open it with the Arduino IDE.
  • Click the Upload button on the Arduino IDE to upload the code to the Arduino Nano.
  • You should then see the motor rotating. It should make one revolution in a clockwise direction, followed by two revolutions in an anti-clockwise direction, and then two revolutions in a clockwise direction.

The procedure is carried out continuously.

  • Check out the output in the Serial Monitor.
COM6
Send
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

How to control a multiple 28BYJ-48 stepper motors

Let us discover how to manage two stepper motors separately yet simultaneously.

Wiring Diagram for two 28BYJ-48 stepper motors

The wiring diagram between Arduino Nano and two stepper motor ULN2003 driver

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code for two 28BYJ-48 stepper motors

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-28byj-48-stepper-motor-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 be concerned if the stepper motor shakes while in motion. This is a characteristic of the stepper motor. We can lower the vibration by using the micro-stepping control technique.

Additionally, due to this feature, if managed correctly, the stepper motor can create music as if it were a musical instrument. An example of this can be found here on Hackster.io.

2. Method of controlling stepper motors

  • Full-step: The unit of movement is one step, which is equal to the degree value specified in the stepper motor's datasheet or manual.
  • Half-step: Splits each full step into two smaller steps. The unit of movement is half of the full step. This method allows the motor to move with double resolution.
  • Micro-step: Divides each full step into many smaller steps. The unit of movement 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 to move with higher resolution. It also makes the motor move smoother at low speeds. The bigger the divisor, the higher the resolution and the smoother the motion.

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

  • Full-step: The motor will move in increments of 1.8 degrees per step, resulting in 200 steps per revolution.
  • Half-step: The motor will move in increments of 0.9 degrees per step, resulting in 400 steps per revolution.
  • Micro-step: The motor will move in increments of 0.45, 0.225, 1125‬, 0.05625 degrees per step, resulting in 800, 1600, 3200, 6400... steps per revolution.

The control method used in the code above was the full-step approach.

3. Resonance Issue

This is for advanced users. Beginners do not need to be concerned with it. It occurs in a range of speeds, where the step rate is equal to the motor's natural frequency. There could be a noticeable change in the sound made by the motor, as well as an increase in vibration. In practical applications, developers should take this into consideration.

Video Tutorial