October 14th, 2015
  • Stepper motor
  • 28BYJ-48
  • ULN2003

Stepper motor library

I've made it my quest to create OOP classes for Arduino. Read about that in this blogpost if you need some more context.

This library is created using the 28BYJ-48 stepper motor in combination with the ULN2003 driver board, but it will work with every 4 pin driver board for stepper motors.

  Scheme stepper motor  

This library lets you choose between three different step modes:

  • Half step mode
  • Full step single phase mode
  • Full step dual phase mode

If you want to know the differences between these modes, please look at this YouTube video explaining all three modes.

The 28BYJ-48 motor needs 512 full (inner) engine rotations to rotate the drive shaft once. In half-step mode these are 8 x 512 = 4096 steps for a full rotation, and in full-step mode these are 4 x 512 = 2048 steps for a full rotation.

Between every step the engine needs some time to move. You can specify this time as the step delay in milliseconds, which is 1ms by default. So when you increase the step delay, the motor turns slower.

When you've initialized the engine with a mode you can choose to let the motor run clockwise or counter clockwise continuously or only for a certain amount of steps. The library can call a method in the event the motor turned the requested amount of steps. See the example code which is in a subdirectory of the library.

Please note that full step mode may need an external power source in order to work. I use a PC power supply for this. You can use a red (5V) and a black wire from the power supply to power the driver board. To turn on a PC power supply you have to connect the green wire to one of the black wires. See my library to control a power supply here.

The stepper motor library can be found here on GitHub.
It uses the BaseTimedElement base class which can be found here on GitHub.

< Previous post Overview posts Next post >
Thanks so much for the forum post.Much thanks again. Really Cool. Shindo
Williampr, May 11th, 2016
Nice and clean work. Subsidiary question: In Arduino/libraries/BaseTimedElement/BaseTimedElement.cpp on line 60 (as of today) you have "|| millis() > _endMillis + 10000)". I can't see the reason yet? Would it be an additional 'mode/condition' to check in the calling program for "~uncatched/unprocessed during 10s"?
Jean Fontaine, January 21st, 2016
Hi Anders, rotateClockwise() and rotateCounterClockwise() can also be called without the number of steps (without any parameters); this will let your motor run continuously. I've also created a capacitive button library and a LCD keypad shield library that solves a lot of issues detecting a single click, double click or button hold with capacitive butons and with the buttons on your LCD keypad shield.
Laurens Huizer, December 6th, 2015
I found this after reading a comment on instructables.com. I have set up the arduino and made everything work with your code. The stepper turns clockwise and counterclockwise. I learned a lot from your code! Next I want to be able to use buttons to make it run continuously and stop it. As a start I'm thinking to use three buttons; clockwiserotation, counterclockwiserotation and stop. Do you have suggestions on how to set this up? You mention continous running, but it's not in your example code.
Anders, December 6th, 2015

Leave a comment

= Thanks for your comment =