Deploy code to control a stepper motor using a joystick
Introduction
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.
Electrical wirings
Open-loop controller design
The complete revolution of 512 steps can be divided into smaller increments.
In[1]:= |
Out[1]= |
Let us choose 8 steps per increment.
In[2]:= |
Let the forward motion of the joystick above 3 V result in one increment of the motor.
In[3]:= |
Out[3]= |
Let a value below 1 V result in one increment in the opposite direction.
In[4]:= |
Out[4]= |
Assemble the open-loop controller.
In[5]:= |
Out[5]= |
Controller deployment
The joystick input.
In[6]:= |
The output is handled by the external library and uses pins 0-3 of the Trinket.
In[7]:= |
In[10]:= |
The complete microcontroller specification.
In[11]:= |
Out[11]= |
The trinket uses the usbtiny programmer.
In[12]:= |
The location of the library.
In[13]:= |
Load the package.
Upload the code. You may have to disconnect the joystick input to pin 4 before uploading.
In[14]:= |
Out[14]= |
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.