Controlling a Stepper Motor using an Arduino and a Rotary Encoder
Welcome to this multi part tutorial on how to control different stepper motors using a rotary encoder.
In this first part we will use the inexpensive and popular stepper motor that comes with it’s own control board. The 28BYJ-48 stepper motor with the ULN2003 board.
The 28BYJ-48 motor is not very fast or very strong, but it’s great for beginners to start experimenting with controlling a stepper motor with an Arduino.
We will write some code to have the motor move in the direction that we turn the rotary encoder, and will also keep track of how much steps we have taken, so that we can have the motor move back to the starting position by pressing down on the rotary encoder switch.
In the next tutorial we will use a Nema 17 stepper motor and the EasyDriver board.
We are using 4 pins to control the Stepper and 3 pins for the rotary encoder module.
Pin 8-11 are controlling the Stepper motor and pin 2-4 are receiving information from the rotary encoder.
We connect the 5V and Ground from to UNO to the rotary encoder and as a precaution, use a breadboard power supply to power the Stepper motor since it can use more power that the UNO can provide.
We also connect the UNO Ground to the breadboard to serve as a reference.
We are using the “Stepper” library to control the stepper motor.
The “Stepper” library is included by default with the Arduino Software install.
We are using some variables to store the current position, since we want to keep track of the position of the stepper motor so we can make it move back to the starting position.
We also included some error checking code to make sure that the rotary encoder is not missing steps, since that would make our motor position inaccurate.
As always, please check out the tutorial video to have more information.
Copy the above Sketch code in your Arduino IDE software to program your Arduino.
We are using the “Stepper” library which is included by default with the Arduino IDE Software install.