Arduino ProjectsElectronic projects

Controlling a 28BYJ-48 Stepper Motor Using ULN2003 Driver and Arduino Board

Stepper motors are widely used to control many common devices we see every day. From blinds, 3D printers and DVD players to security cameras and CNC machines, stepper motors are closer to our lives than you might think.

The first step in exploring stepper motors is to use the 28BYJ-48 stepper motor. They usually come with a ULN2003 based driver board which makes them very easy to use.

Do you know how these stepper motors work?

These stepper motors use a toothed wheel (which has 32 teeth) and four electromagnets that form a ring around the wheel.

Each high pulse sent energizes the coil, attracting the teeth closest to the gear and rotating the motor in precise and fixed angular increments called stepping.

The way you send pulses to these coils greatly affects the behavior of the motor.

● The pulse train determines the direction of rotation of the motor.

● The frequency of the pulse determines the speed of the motor.

● The number of pulses determines how far the motor will turn.

28BYJ-48 stepper motor

The 28BYJ-48 is a 5-wire unipolar stepper motor that operates at 5V.

The biggest advantage of this motor is that it can position exactly one step at a time. It performs well in projects that require precise positioning, such as opening and closing vents.

Another advantage is that its movement is relatively precise and very reliable since the motor does not use contact brushes.

Considering its size, the motor delivers a decent 34.3mN.m of torque at around 15RPM. It delivers good torque even at standstill, which can be maintained as long as power is supplied to the motor. The only downside is that it’s a bit power hungry, even when it’s not moving.

Pinout

28BYJ-48 is a 5-wire stepper motor. The pinout is as follows:

The 28BYJ-48 consists of two coils, each with a center tap. The two center taps are connected internally and brought out as a 5th wire (red wire).

One end of the coil and the center tap together form a phase. Therefore, 28BYJ-48 has a total of 4 phases.

The red line is always high. When the other lead is pulled low, that phase is energized.

A stepper motor only rotates when the phases are energized in a logical sequence called a step sequence.

Gear reduction ratio

According to the datasheet, when the 28BYJ-48 motor is running in full-step mode, each step corresponds to 11.25° of rotation. This means there are 32 steps per revolution (360°/11.25° = 32).

In addition to this, the motor has a 1/64 reduction gear set. (Actually it’s 1/63.68395, but 1/64 is a good enough approximation for most uses)

This means that there are actually 2038 steps (32*63.68395 steps per revolution = 2037.8864 about 2038 steps).

Power consumption

The 28BYJ-48 typically consumes about 240mA.

Since the motor draws a lot of current, it’s best to power it directly from an external 5V supply, rather than drawing power from the Arduino.

Note that even at standstill, the motor consumes power to maintain its position.

Technical Specifications

Here are the full specs:

Operating Voltage
5VDC
Working current
240mA (typ.)
Phase
4
Gear reduction ratio
64:1
step angle
5.625°/64
frequency
100Hz
traction torque
>34.3mN.m(120Hz)
self-aligning torque
>34.3mN.m
friction torque
600-1200 gf.cm
pull-in torque
300 gf.cm

For more information on this motor, see the following data sheet: 28BYJ-48 data sheet .

ULN2003 driver board

Since the 28BYJ-48 stepper motor consumes high current, a microcontroller like Arduino cannot directly control the motor. It needs a driver IC like ULN2003 to control the motor, so this motor usually comes with a ULN2003 based driver board.

Known for its high current and high voltage capability, the ULN2003 provides higher current gain than a single transistor and enables the microcontroller’s low voltage, low current output to drive high current stepper motors.

The ULN2003 consists of a set of seven Darlington transistor pairs, each capable of driving loads up to 500mA and 50V. Four of the seven pairs are used on this driver board.

The board has a molex connector that perfectly connects the motor wires, making it very easy to connect the motor to the board. There are four control inputs and a power connection.

The driver board has four LEDs showing activity on the four control input lines (to indicate stepper status). They provide good directions when not going in. The board also comes with an on/off jumper to isolate the power to the stepper motors.

ULN2003 stepper driver board pinout

The pins of the ULN2003 stepper driver board are as follows:

IN1 – IN4 pins are used to drive the motor. Connect them to the digital output pins on the Arduino.

GND is the common ground pin.

The VDD pin powers the motor. Connect it to an external 5V power supply. Never use the Arduino’s 5V power supply to drive this stepper motor because the motor will draw a lot of current.

Motor Connector This is where the motor plugs into. The connector is keyed, so it has only one connection.

Connect the 28BYJ-48 stepper motor and ULN2003 driver to the Arduino board

Now that we know everything about the motor, we can start connecting it to the Arduino.

First connect the power supply to the ULN2003 driver. Use a separate 5V power supply to power the stepper motors.

After that, connect the ground of this power supply to the ground of the Arduino. This is very important so that we can have the same voltage reference between the two.

Now connect IN1, IN2, IN3, IN4 of the driver board to Arduino digital pins 8, 9, 10 and 11 respectively. Finally connect the motor cable from the stepper motor to the driver board.

Connect all components as shown below.

Arduino code – using built-in Stepper library

For the first experiment, we will use the Arduino Stepper library installed with the Arduino IDE .

The Stepper library handles the step sequence and makes it easy to control a variety of stepper motors, both unipolar and bipolar.

Below is a simple sketch to make a stepper motor rotate slowly clockwise and then quickly counterclockwise.

  1. //Includes the Arduino Stepper Library
  2. #include <Stepper.h>
  3. // Defines the number of steps per rotation
  4. const int stepsPerRevolution = 2038;
  5. // Creates an instance of stepper class
  6. // Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
  7. Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);
  8. void setup() {
  9.         // Nothing to do (Stepper Library sets pins as outputs)
  10. }
  11. void loop() {
  12.         // Rotate CW slowly at 5 RPM
  13.         myStepper.setSpeed(5);
  14.         myStepper.step(stepsPerRevolution);
  15.         delay(1000);
  16.         // Rotate CCW quickly at 10 RPM
  17.         myStepper.setSpeed(10);
  18.         myStepper.step(-stepsPerRevolution);
  19.         delay(1000);
  20. }

code description

Start by including the built-in Stepper library in your sketch code.

  1. #include <Stepper.h>

Next, we define a constant stepsPerRevolution that holds the number of steps the motor takes to complete one revolution. In the example, its value is 2038.

  1. const int stepsPerRevolution = 2038;

The step sequence of 28BYJ-48 unipolar stepper motor is IN1-IN3-IN2-IN4. We’ll use this information to drive the motor by creating an instance called myStepper with the pin order 8, 10, 9, 11.

Make sure you do it right or the motor won’t work properly.

  1. Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);

There is nothing to set up in the setup() function because the Stepper library internally sets the four I/O pins as outputs.

  1. void setup() {
  2. }

In the loop() function, we use the setSpeed() function to set the speed at which we want the stepper motor to move, and then use the step() function to tell it how many steps to rotate. Passing a negative number to the step() function reverses the direction of rotation of the motor.

The first code snippet makes the motor spin clockwise very slowly. The other makes the motor spin counterclockwise at a very high speed.

  1. void loop() {
  2.         // Rotate CW slowly at 5 RPM
  3.         myStepper.setSpeed(5);
  4.         myStepper.step(stepsPerRevolution);
  5.         delay(1000);
  6.         // Rotate CCW quickly at 10 RPM
  7.         myStepper.setSpeed(10);
  8.         myStepper.step(-stepsPerRevolution);
  9.         delay(1000);
  10. }
Arduino code – using the AccelStepper library

The AccelStepper library is ideal for simple single motor applications. But when you want to control multiple stepper motors, you need a better library.

Therefore, for the next experiments, we will use an advanced stepper motor library called the AccelStepper library. It is much better than the standard Arduino Stepper library in several ways:

● It supports acceleration and deceleration.

● Support half-step stepping.

● It can support multiple stepper motors at the same time, and each stepper motor has independent concurrent stepping.

This library is not included in the Arduino IDE, so you need to install it first.

library installation

To install the library, navigate to Sketch > Include Libraries > Manage Libraries… Wait for the library manager to download the library index and update the list of installed libraries.

Filter your search by entering ” accelstepper “. Click on the first entry and select Install.

Arduino code

Below is a simple sketch that accelerates a stepper motor in one direction and then decelerates to a stop. Once the motor makes one revolution, it changes the direction of rotation. The code does this over and over again.

  1. // Include the AccelStepper Library
  2. #include <AccelStepper.h>
  3. // Define step constant
  4. #define FULLSTEP 4
  5. // Creates an instance
  6. // Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
  7. AccelStepper myStepper(FULLSTEP, 8, 10, 9, 11);
  8. void setup() {
  9.         // set the maximum speed, acceleration factor,
  10.         // initial speed and the target position
  11.         myStepper.setMaxSpeed(1000.0);
  12.         myStepper.setAcceleration(50.0);
  13.         myStepper.setSpeed(200);
  14.         myStepper.moveTo(2038);
  15. }
  16. void loop() {
  17.         // Change direction once the motor reaches target position
  18.         if (myStepper.distanceToGo() == 0)
  19.                 myStepper.moveTo(-myStepper.currentPosition());
  20.         // Move the motor one step
  21.         myStepper.run();
  22. }

code description

First include the newly installed AccelStepper library.

  1. #include <AccelStepper.h>

Now, since we want the motor to run in full steps, we will define a constant for it. If you want the motor to run in half steps, set the constant to 8.

  1. #define FULLSTEP 4

Next, we create an instance called myStepper with a pin sequence of 8, 10, 9, 11 (remember, these motors have a step sequence of IN1-IN3-IN2-IN4).

  1. AccelStepper myStepper(FULLSTEP, 8, 10, 9, 11);

In the setup() function, we first set the motor’s maximum speed to 1000, which is about the same speed as these motors. Then we set an acceleration factor for the motor, adding acceleration and deceleration to the movement of the stepper motor.

Then we set the regular speed to 200 and the step count to 2038 (since the 28BYJ-48 and its gearing require 2038 steps per revolution).

  1. void setup() {
  2.         myStepper.setMaxSpeed(1000.0);
  3.         myStepper.setAcceleration(50.0);
  4.         myStepper.setSpeed(200);
  5.         myStepper.moveTo(2038);
  6. }

In the loop() function, we use an if statement to check how far the motor needs to be driven (by reading the distanceToGo property) until it reaches the target position (set by moveTo ). Once distanceToGo reaches zero, we will move the motor in the opposite direction by changing the moveTo position to the negative of its current position.

Now at the bottom of the loop() function, you will see that we call a run() function. This is the most important function because the stepper does not move until this function is executed.

  1. void loop() {
  2.         // Change direction once the motor reaches target position
  3.         if (myStepper.distanceToGo() == 0)
  4.                 myStepper.moveTo(-myStepper.currentPosition());
  5.         // Move the motor one step
  6.         myStepper.run();
  7. }

Related Articles

Leave a Reply

Back to top button
error: Content is protected !!