Arduino + 28BJY-48 Stepper Motor: Simple and Improved

6,102

19

4

I love these tiny 28BJY-48 stepper motors. They cost only a few dollars and have so many uses. They are compatible with Arduino, however the example code I have found online has either been overly complicated, or doesn't work quite right with the motor. I've been using these motors in projects a lot lately, so I thought I should share the solution to some of these functionality problems and my code fix to make the motor work well with the built-in Arduino stepper library.

This code fix makes it super easy to incorporate into your code, while improving the functionality of the motor, running smoother and quieter without getting hot.

Supplies:

Step 1: What You Will Need

You will need the following things:

1x Arduino (You can use any arduino, I'm using an Arduino UNO R3)

1x USB cable (to connect Arduino to the computer)

1x 28BJY-48 Stepper motor (from ebay)

1x ULN2003 driver (usually comes with motor on ebay)

6x Male to female jumper wires

Arduino IDE (can be downloaded from www.arduino.cc)

Step 2: Understanding the ULN2003 Driver Module

The ULN2003 module is used to control the stepper motor. The chip on the board is a ULN2003A darlington array, which is essentially a bunch of transistors in a single package. This module saves us having to wire up NPN transistors individually. The IN pins (1-4) connect to the Arduino Digital pins (8-11). The VCC pin connects to the Arduino's 5V pin, and the GND pin to theArduino's GND pin.

When digital pins D8-11 on the Arduino are written HIGH, the driver allows current to flow through that motor winding.

There is a metal jumer between the <-> pins which connects the motor to 5V. If you remove this jumper, the motor will not have power.

There are also 4 led lights and resistors connected to visualise the steps, but you can only really see this at low speeds.

Step 3: Wiring It Up

The wiring for this project is simple.

ARDUINO ULN2003 DRIVER

5v---------------------------VCC

GND-----------------------GND

D8--------------------------IN1

D9--------------------------IN2

D10-------------------------IN3

D11-------------------------IN4

Connect the motor to the driver using the supplied connector.

Step 4: Code

The code makes improvement on Michael Schoeffler's tutorial using the ULN2003 driver module and Arduino's built-in stepper library. The problem with the stepper library is that it doesn't write the digital pins LOW after completing the movement. This drains more current, makes a buzzing noise, and causes the motor to heat up rapidly. To stop this, I have included a new function called halt(), which is called immediately after stepper.step()

Before the setup() in the code, a stepper object is define with pins 8,10,9,11. This is not an error, but is rther the correct firing order for the 28BJY-48 motor. This is only a code change, do not change the wiring. Do not change the code back to 8,9,10,11. It will not work properly.

I have also defined an int called 'revolution' as 2038. This is the correct number of steps to complete one revolution of this motor.

You need three functions needed to make the motor move:

stepper.setspeed(); Between the brackets, add a number between 1 and 18. 18 is the maximum speed without a load, but I would recommend a maximum speed of 10, as it is quiet, yet fast.

stepper.step(); Between the brackets, add the number of steps to take, keeping in mind that a full revolution is 2038 steps. For example, to turn 180 degrees, you could use either stepper.step(1019); or stepper.step(revolution/2);
To turn the opposite direction, use a negative number, e.g. stepper.step(-546);

halt(); Call this function after stepper.step() to write all motor pins LOW. Do not put anything between the brackets.

In the main loop() of the code below, we first set the speed to 10, then move forward 2038 steps, then wait 1000 milliseconds, then turn 2038 steps backwards, then wait another 1000 milliseconds, then repeat.

TO UPLOAD CODE TO ARDUINO:

1. Open the Arduino IDE

2. Click FILE > NEW to create new sketch

3. Copy and paste the code into the sketch. You can download the sketch file from www.joeltonks.com/projects

4. Connect your Arduino to the computer via the USB cable.

5. Click TOOLS > BOARD and select your board. For me, thats 'Arduino/Genuino Uno'

6. Click TOOLS > PORT and select your port. For me, that's 'COM1'. If you are using a Mac you may see different port names to a PC.

7. Click FILE > UPLOAD to upload your code. If all was successful, you should see the message 'Done Uploading' after a few seconds.

8. That's it! The motor should begin running the code straight away.

9. (optional) Remove the USB cable and power Arduino directly from an external power source.

// Original stepper code by (c) Michael Schoeffler 2017, http://www.mschoeffler.de
// Updated stepper code by Joel Tonks, http://joeltonks.com/projects

/*----------------------------- ARDUINO | ULN2003A DRIVER BOARD // Arduino pin connections ------------------------------- 5V | VCC // Jumper must be placed between '<->' pins to enable stepper motor GND | GND pin 8 | IN1 pin 9 | IN2 pin 10 | IN3 pin 11 | IN4 -----------------------------*/

#include // include stepper library

int revolution = 2038; // the number of steps in one revolution of your motor (28BYJ-48)

Stepper stepper(revolution, 8, 10, 9, 11); // the correct stepping order for your motor (28BYJ-48)

void setup() { // set driver pins as OUTPUT pinMode(8, OUTPUT); pinMode(10, OUTPUT); pinMode(9, OUTPUT); pinMode(11, OUTPUT); }

void loop() { stepper.setSpeed(10); // 18 = max working speed for this motor. 10 is max recommended speed. stepper.step(revolution); // move motor one revolution anti-clockwise (2038 steps) halt(); // call 'halt()' function to write motor pins LOW delay(1000); // wait one second (optional) stepper.setSpeed(10); // set speed to 5 stepper.step(-revolution); // move motor one revolution clockwise (-2038 steps) halt(); delay(1000); }

void halt() { // This writes all motor pins LOW, preventing the motor from drawing current when idle. digitalWrite(8, LOW); digitalWrite(9, LOW); digitalWrite(10, LOW); digitalWrite(11, LOW); }

Share

    Recommendations

    • Classroom Science Contest

      Classroom Science Contest
    • Woodworking Contest

      Woodworking Contest
    • Fandom Contest

      Fandom Contest

    4 Discussions

    0
    None
    saadqu098

    4 weeks ago

    perfect thank you

    0
    None
    thanatos65

    5 weeks ago

    Keep in mind that setting the pins low after movement shuts off current to that winding and now the motor will not have any holding power and can move under an external force.

    1 reply
    0
    None
    jtonks_projectsthanatos65

    Reply 4 weeks ago

    Yes, thanks for the heads up. There is enough friction in the internal gear ratio to prevent moving under light external forces.

    0
    None
    Penolopy Bulnick

    5 weeks ago

    Nice! Thanks for sharing how to do this :)