Developing a PyQt5 Application to Control Steppers on Raspberry Pi with Ubuntu 22.04

Protonest IoT
3 min readJul 25, 2023

--

Stepper motors are important in robotics and automation. They help control how things like robotic arms and 3D printers move. This guide will show you how to make a simple application using PyQt5. The application can control these motors on a Raspberry Pi with Ubuntu 22.04. It works by talking to a Python script that connects with the motors.

Wiring and Setup

Before we dive into the software part, make sure that your motors, TB6600 driver, and Raspberry Pi are correctly wired. On your TB6600 driver, set the micro-step to 1 and set 200 steps per revolution. Connect the stepper motor with the TB6600 driver as follows,

Circuit Diagram

Software Environment Setup

Your Raspberry Pi should be running Ubuntu 22.04. Make sure that your package list is updated by running,

sudo apt-get update

You’ll need Python3 and pip3 installed. If they’re not, install them using,

sudo apt-get install python3
sudo apt-get install python3-pip

Install PyQt5, and RPi.GPIO using pip,

pip3 install pyqt5
pip3 install RPi.GPIO

For the C++ part of your application, you’ll need gcc, g++, and the Qt libraries,

sudo apt-get install gcc
sudo apt-get install g++
sudo apt install -y qtcreator qtbase5-dev qt5-qmake cmake

Developing the Application

The following is a simple Qt5 application with a single button. When this button is clicked, it writes to the standard input of the Python script, triggering the motor to rotate one full cycle.

Create a folder for the project and navigate to it and type,

nano main.cpp

Then paste the below code inside main.cpp,

#include <QApplication>
#include <QPushButton>
#include <QProcess>
#include <QWidget>

int main(int argc, char *argv[]) {
QApplication app(argc, argv);

QWidget *window = new QWidget;
QPushButton *button = new QPushButton("Rotate Stepper", window);

QProcess *process = new QProcess();
QString pythonScriptPath = "/path/to/your/motor_script.py";
process->start("python3", QStringList() << pythonScriptPath);

QObject::connect(button, &QPushButton::clicked, [=]() {
QString steps = "200\n"; // one full rotation
process->write(steps.toUtf8());
});

window->show();

return app.exec();
}

Then create another file named script.py using the command,

nano script.py

Paste the code,

import sys
import RPi.GPIO as GPIO
import time

DIR = 27
STEP = 17
ENA = 22

GPIO.setmode(GPIO.BCM)
GPIO.setup(DIR, GPIO.OUT)
GPIO.setup(STEP, GPIO.OUT)
GPIO.setup(ENA, GPIO.OUT)
GPIO.output(DIR, GPIO.HIGH) // Can turn the opposite direction when GPIO.LOW

delay = 0.0208

def move_motor(steps):
GPIO.output(ENA, GPIO.HIGH)
for x in range(steps):
GPIO.output(STEP, GPIO.HIGH)
time.sleep(delay)
GPIO.output(STEP, GPIO.LOW)
time.sleep(delay)
GPIO.output(ENA, GPIO.LOW)

def main():
for line in sys.stdin:
steps = int(line.strip())
move_motor(steps)
GPIO.cleanup()

if __name__ == "__main__":

main()

Then create another file named project.pro,

nano project.pro

Add the code,

TEMPLATE = app
TARGET = stepper_motor_control
INCLUDEPATH += .

# Input
HEADERS +=
SOURCES += main.cpp

# Specify the C++ standard
CONFIG += c++11

# Qt Modules
QT += widgets

To build the C++ application, navigate to the directory containing the source files and run,

qmake project.pro
make

To run the PyQt5 application, use

sudo ./app

During testing, you might need to adjust the steps per angle value for all four stepper motors based on your specific setup and requirements.

Conclusion

Building a PyQt5 application to control stepper motors involves a combination of hardware understanding, Python scripting, and C++ programming. However, the flexibility and control it offers make it an exciting project, whether for a hobbyist or a professional.

This setup provides a powerful platform for developing more complex robotics applications. As always, be sure to follow best practices when working with hardware to ensure the longevity of your components.

Happy coding!

Contact us for any consultations or projects related to IoT.

Protonest for more details.

Protonest specializes in transforming IoT ideas into reality. We offer prototyping services from concept to completion. Our commitment ensures that your visionary IoT concepts become tangible, innovative, and advanced prototypes.

Our Website: https://www.protonest.co/

Email: udara@protonest.co

If you enjoyed this article and would like to show some support, consider buying me a coffee on Ko-Fi.

Your support not only helps me keep creating content like this but also fuels me! ☕️📚

Thank you for being an amazing reader!

Here’s my Ko-Fi link: https://ko-fi.com/udarakumarasena

Cheers!

--

--

Protonest IoT
Protonest IoT

Written by Protonest IoT

We make your IoT ideas a reality

No responses yet