WOLFRAM

MICROCONTROLLER KIT

Open-loop control of a stepper motor

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.

Parts

Stepper motor 28BYJ48 with 5-way connector and ULN2003 control board [link]

Joystick [link]

Adafruit Trinket 5V with headers [link]

Micro USB to USB Cable [link]

Mini breadboard [link]

Misc wires

DC power cable female connector plug [link]

Power supply adapter [link]

Electrical wirings

Open-loop controller design

The complete revolution of 512 steps can be divided into smaller increments.

In[1]:=
Click for copyable input
Out[1]=

Let us choose 8 steps per increment.

In[2]:=
Click for copyable input

Let the forward motion of the joystick above 3 V result in one increment of the motor.

In[3]:=
Click for copyable input
Out[3]=

Let a value below 1 V result in one increment in the opposite direction.

In[4]:=
Click for copyable input
Out[4]=

Assemble the open-loop controller.

In[5]:=
Click for copyable input
Out[5]=

Controller deployment

The joystick input.

In[6]:=
Click for copyable input

The output is handled by the external library and uses pins 0-3 of the Trinket.

In[7]:=
Click for copyable input
In[10]:=
Click for copyable input

The complete microcontroller specification.

In[11]:=
Click for copyable input
Out[11]=

The trinket uses the usbtiny programmer.

In[12]:=
Click for copyable input

The location of the library.

In[13]:=
Click for copyable input

Load the package.

Click for copyable input

Upload the code. You may have to disconnect the joystick input to pin 4 before uploading.

In[14]:=
Click for copyable input
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.