Try out 28BYJ-48 Stepper Motor with ULN2003 Driver Board Using Arduino and AccelStepper.

Try out 28BYJ-48 Stepper Motor with ULN2003 Driver Board Using Arduino and AccelStepper
Try out 28BYJ-48 Stepper Motor with ULN2003 Driver Board Using Arduino and AccelStepper

How to use 28BYJ-48 with ULN2003 Driver Board.

Recently, I planned to make a smart lock by myself. My door lock is a deadbolt lock, which means I can add a robotic arm or something to turn the deadbolt.

At first, a 360 continuous servo motor seem like a good option. But, It's difficult to control the shaft to rotate to a specific angle.

I did some research on Google. Many peoples are recommending the stepper motor. I check on Shopee, and many sellers from China sell the 28BYJ-48 stepper motor with ULN2003 driver board extremely cheap. The whole thing only cost RM 4.28, which is more affordable than a servo motor.

Let's try out the 28BYJ-48 stepper motor with the ULN2003 driver board.

Components:

  • 28BYJ-48 Stepper Motor
  • ULN2003 Driver Board
  • LoLin NodeMCU ESP8266 V3
  • Jumper Wires
  • 5V 5A Power Supply Module
  • 9V Power Adapter

The 28BYJ-48 Stepper Motor requires a bit more power to drive it. It's not recommended to draw power from the ESP8266 board. So, I add another power supply module as an external power supply for the stepper motor.

Wire Up

28BYJ-48 Stepper Motor with ULN2003 Driver Board and ESP8266 breadboard schematic
28BYJ-48 Stepper Motor with ULN2003 Driver Board and ESP8266 breadboard schematic

We power the stepper and the ESP8266 from the power supply module. Connect the positive and negative terminal accordingly. The power supply module will get power from the 9V power adapter.

Wire the ESP8266 and ULN2003 Driver Board according to the table below(I also include the LoLin NodeMCU ESP8266 V3 pinout):

 ULN2003 | ESP8266 | Arduino Sketch 
 --------|---------|----------------
   IN1   |    D1   |        5
   IN2   |    D2   |        4
   IN3   |    D5   |       14       
   IN4   |    D6   |       12 

Also, connect the 28BYJ-48 Stepper Motor with the ULN2003 Driver.

Coding

Here I am using Arduino IDE to make a simple sketch to try out the stepper motor.

I saw many guides recommend this AccelStepper library instead of the Arduino Stepper library.

Why AccelStepper is better than Arduino Stepper:

  • Provides an object-oriented interface
  • Supports multiple steppers.
  • Provides stepper acceleration feature.

So, here I will use this library to control a 28BYJ-48 stepper motor with ULN2003 driver in this article. I also use the Arduino IDE to code all the sketches (program).

Get AccelStepper Library

To get this AccelStepper library, you can head to Arduino IDE -> Sketch -> include Libary -> Manage Libraries... -> search "AccelStepper" and install it.

Rotate at Constant Speed

First, let's try to rotate the shaft at a constant speed.

Include the AccelStepper library in the sketch.

#include <AccelStepper.h>

Define the IN1, IN2, IN3, and IN4 constants. All the constants are the pins from ESP8266 that will connect to the ULN2003 driver.

#define IN1 5
#define IN2 4
#define IN3 14
#define IN4 12

Create an AccelStepper object for us to control the stepper. Here I pass in the half-step value and those pins defined in the last line of code. The sequence of the pins passing into the AccelStepper constructor is important. Make sure to follow it.

AccelStepper stepper(AccelStepper::HALF4WIRE, IN1, IN3, IN2, IN4);

Starting with the setup() function, we start the serial communication and set the baud rate to 115200 here.

void setup() {
  Serial.begin(115200);
}

Next, we call the setMaxSpeed() from AccelStepper object and pass in a speed value. Here I'm using 1000. If we don't set a value. By default, the maximum speed to rotate will be 1. It's something really slow, and we don't want it.

void setup() {
  ...
  stepper.setMaxSpeed(1000);
}

Set the actual rotation speed by calling the setSpeed() with a speed value. Here I passed in 1000. A positive value input into the setSpeed() will rotate clockwise, and a negative value will be anti-clockwise.

void setup() {
  ...
  ...
  stepper.setSpeed(1000);
}

We call the runSpeed() to make it rotate in the loop().

void loop() {
  stepper.runSpeed();
}

Full Sketch:

After the coding part is done, we can upload the sketch and wire up all the components to test it.

Bounce at Constant Speed

I rewrite the AccelStepper example bounce sketch with a constant speed instead of acceleration.

Include the AccelStepper library.

#include <AccelStepper.h>

Declare a variable called stepsPerRevolution and initialize the 2048 value to it. The 2048 value is the steps for the 28BYJ-48 stepper to rotate one circle. Additional Note: Many articles say this stepper rotates 360° takes 4096 steps for half step mode and 2048 steps for full step mode. When I tried it, the stepper behaved differently.

const int stepsPerRevolution = 2048;

Define all the pins from ESP8266 that will be used to connect to the ULN2003 driver board.

// Stepper constant
#define IN1 5
#define IN2 4
#define IN3 14
#define IN4 12

Create an object of AccelStepper. Initialize the AccelStepper object with the half-step value and all the defined pins.

AccelStepper stepper(AccelStepper::HALF4WIRE, IN1, IN3, IN2, IN4);

Start the serial communication in the setup().

void setup() {
  Serial.begin(115200);
}

Continue, we call and pass a value into setMaxSpeed().

void setup() {
  ...
  stepper.setMaxSpeed(1000);
}

Call the moveTo() and pass in the stepsPerRevolution variable.

void setup() {
  ...
  ...
  stepper.moveTo(stepsPerRevolution);
}

Call the setSpeed() and pass in 1000 as the speed value.

void setup() {
  ...
  ...
  ...
  stepper.setSpeed(1000);
}

Here need to take notes a bit. The moveTo() will reset the speed. So, we should call the moveTo() first. Then, we only call the setSpeed().

In the setup(), we need an if statement to control the shaft rotation in the opposite direction. The condition in the if clause checks whether the stepper motor reaches the step count goal. Once it is met, we call the moveTo() again with the opposite value. Also, set the rotation speed using setSpeed().

void loop() {
  if(stepper.distanceToGo() == 0) {
    stepper.moveTo(-stepper.currentPosition());
    stepper.setSpeed(1000);
  }
}

We call runSpeedToPosition() instruct the stepper rotate.

void loop() {
  if(...) {
    ...
    ...
  }

  stepper.runSpeedToPosition();
}

Full Sketch:

Upload to the ESP8266 and test it out.

Bounce with Acceleration.

As the bounce at a constant speed sketch, we change a bit for acceleration.

The change involve the setup() part and call run() function istead of runSpeed() or runSpeedToPosition() in the loop().

Keep the declaration part.

#include <AccelStepper.h>

// Stepper constant
const int stepsPerRevolution = 2048;

#define IN1 5
#define IN2 4
#define IN3 14
#define IN4 12

AccelStepper stepper(AccelStepper::HALF4WIRE, IN1, IN3, IN2, IN4);

Change setSpeed() to setAcceleration() with a speed value.

void setup() {
  ...
  ...
  stepper.setAcceleration(200)
  ...
}

Call moveTo() after the setAcceleration().

void setup() {
  ...
  ...
  stepper.setAcceleration(200)
  stepper.moveTo(stepsPerRevolution);
}

In loop(), change runSpeed() or runSpeedToPosition() to run().

void loop() {
  if (stepper.distanceToGo() == 0) {
    stepper.moveTo(-stepper.currentPosition());
  }
  stepper.run();
}

Full Sketch:

Flash the sketch to the ESP8266 and try it.

Next Post Previous Post
No Comment
Add Comment
comment url