Deploy code to control a stepper motor using a joystick
Stepper motors provide good position control. One revolution of a stepper motor shaft is completed in a specific number of angular increments or steps. The motor moves one step by applying a specific sequence of digital pulses to the electromagnets of the motor. If the sequence is reversed the motor rotates in the opposite direction. By specifying the number of steps the shaft can be moved to a desired position.
In this project we will use a joystick to specify the number of steps and the direction of rotation of a 28BYJ48 stepper motor. An Adafruit Trinket microcontroller will apply the required sequence of pulses based on the joystick measurement. The pin mapping of the microcontroller and the pulse sequence of the stepper motor is described in an external library. The code in the external library as well as generated code will be used to achieve the open-loop control of the stepper motor.
Open-loop controller design
The complete revolution of 512 steps can be divided into smaller increments.
Let us choose 8 steps per increment.
Let the forward motion of the joystick above 3 V result in one increment of the motor.
Let a value below 1 V result in one increment in the opposite direction.
Assemble the open-loop controller.
The joystick input.
The output is handled by the external library and uses pins 0-3 of the Trinket.
The complete microcontroller specification.
The trinket uses the usbtiny programmer.
The location of the library.
Load the package.
Upload the code. You may have to disconnect the joystick input to pin 4 before uploading.
We see that by creating a suitable model to obtain the number of steps from the joystick motion, we can deploy code to do open-loop control of a stepper motor.
More things to try
- Are there ways in which the controller can be improved? Use a model that sets the number of steps that is proportional to the joystick motion. At the default location the joystick reading is generally noisy. So some filtering will be needed close to the default position.
- Instead of a joytick use a potentiometer, and base the model on the difference in the potentiometer readings.
- Move to a microcontroller board with an additional anaolog pin to spare, and use it to control the speed of rotation using a potentiometer.