Control a Stepper Motor in Python

Control a Stepper Motor in Python

View the app user interface

View the code on GitHub

From 3D printers, to mirror mounts, to machine tools – stepper motors are ubiquitous. Using Dash DAQ, we created a GUI to control a Silverpak 17C Lin Engineering stepper motor.

Check out the demo app online. Keep reading to see how easy Dash DAQ makes writing control panel GUIs in Python!

About the device

The Silverpak 17C stepper motor is controlled by sending UTF-8 encoded strings to the motor. The motor has two modes:  position and velocity mode. Velocity mode allows for constant rotation of the motor. Position mode allows for absolute positioning of the motor axis. You can learn more information about the Lin Engineering commands and responses here.

About the setup

PySerial facilitates the serial communication between the computer and the stepper motor’s integrated driver.

Connecting this app required the use of a power supply, a USB 485 converter, and a USB connection from the computer to the motor driver, seen below:

The Dash DAQ app

Before creating this app, we import the PySerial (serial), dash, dash_daq, and plotly libraries:

We then set the serial port settings, and open the serial port connected to the motor. Using PySerial, we create a function containing the motor’s specified serial communication settings:

For this app, we chose to utilize CSS Bootstrap nested columns to stack our components. Here is an example of one of these nested columns stacking a DashDAQ StopButton component above a text area:

The first callback in this program sets the motor settings by sending a string command determined by the user. This call back is activated by the Dash DAQ BooleanSwitch component and has input ID pre-settings. When this switch is pressed, a command is sent that sets the address, motor current, hold current, acceleration, and step size of the stepper motor.

It also sets the baud rate and COM port (in the global variable version of the application) for serial communication via PySerial. The output of the callback is an empty HTML div with ID div-one, which stores the serial response of the stepper motor in children.

The output of the first call back ID div-one is the input of another callback, seen below, which sends the serial response stored in “children” to the Dash text box. In our app, this is done in multiple callbacks (div-two, div-three, and div-four), allowing the user to visualize the serial response of the motor from multiple components in real time.

This next callback utilizes the stop button component to flush the serial port and terminate any commands that may damage the stepper motor while in use. Every press of the stop button sends a terminate string to the stepper motor.

The position mode callback is controlled using a Boolean switch component, and the Dash DAQ Knob component. For this callback, both the velocity and position knobs are used. The velocity knob sets the maximum speed, and the position knob sets the absolute position.

The position knob, input ID stepper position, has a value within a 360 degree radius that is mapped by interpolating it with the motor’s set step size revolution. This value is used in the posvelo string and sent to the motor.  When the position knob is at zero or 360 degrees, a serial response is sent and displayed in the serial monitor.

The input ID stepper-position is linked to another callback, where it updates parameter figure of output ID position-gauge. The Plotly polar chart shows the angular position of the motor when position mode is activated.

Using the Dash DAQ ColorPicker component, we created several callbacks that took the input of the color picker, and returned its value to the color parameters of the Dash DAQ, Plotly, and Dash components. A simple example:

Our app explores only a tiny fraction of what this stepper motor can do. With Dash DAQ, it’s easy to create interactive GUIs for all your device control and measurement needs.