Raspberry pi

Raspberry Pi HMI Project using PYQT5 Software tutorial

Description:

Raspberry Pi HMI-The components used in this project are sponsored by the DFrobot. DFrobot is a leading robotics and open source hardware provider. They create innovative, user-friendly hardware & software products that become the building blocks in all kinds of electronic projects. I personally recommend you should definitely visit www.dfrobot.com.

In this tutorial, you will learn how to make a Raspberry Pi based HMI system using PYQT5 Software. As this is my first tutorial on the HMI PYQT5 software using python. In this tutorial, I will only cover the extreme basics. To keep things simple and understandable I did the designing and programming only for one button which is used to control an LED. In my upcoming tutorial, I will do the designing and programming for 4 buttons and will control some relays. if you want to install the pyqt5 and Qt designer then read my article.

Raspberry Pi HMI

Without any further delay let’s get started!!!



Amazon Purchase links:

5.5 inch HDMI touch LCD:
Raspberry Pi 3 B+
Wireless Keyboard and Mouse Combo
Night vision Camera for Raspberry Pi

lm7805 Voltage Regulator:
330-ohm resistors pack:
female DC power jack socket:
Jumper Wires:
Bread Board: 
12v Adaptor: 

DISCLAIMER:

Please Note: these are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

 


Components interfacing:

Raspberry Pi HMI

LED is connected with the GPIO pin 26 and the grey wire is connected with the ground. The touch screen is powered up using the raspberry pi USB port. The LCD is connected with the HDMI port of the Raspberry Pi using the HDMI Adaptor. That’s all about the connections.

PYQT 5 Software Installation:

Steps:

  • Before you continue, make sure that the system is updated to the latest version for this simply write this command on Raspberry Pi terminal and then press enter to continue.

  • Next, you have to update the apt-get package so that you can install the pyqt5 in the next step, for now simply write the below command and press enter on your keyboard.

  • Now you will need to install the pyqt5 development headers using the command and press enter.

  • Now install the pyqt5 qtcreator using the command and press enter.


Raspberry PI HMI/GUI application designing and Programming:

I started off by creating a folder for my project files. While you are on the Raspberry Pi desktop, right-click and select Create New and then click on the folder. Enter the folder name as HMI System or any other name you like. This will create a new folder on the desktop with the name HMI System. I will save all my project files in this folder.

Raspberry Pi HMI

Open the Qt 5 designer software for designing the HMI or GUI application.

Raspberry Pi HMI

The Qt 5 designer software is provided with all the components and tools which can be used to design advanced level Raspberry PI HMI systems. The Qt 5 designer software is very user-friendly; drag and drop the component you want to use. In my case, I am going to use a Pushbutton. On the New Form – Qt Designer select Dialog without Buttons and then click on the create button.

Select a Push Button Drag and drop it on the form. Change the dimensions of the Push Button as per your requirement.

On the right side, you can see the Property Editor, select the objectName and enter btn, then scroll down and set the font size, again scroll down until you see the text, click and delete the text PushButton. This will delete the text written on the Push Button and write OFF. This will display the text OFF on the Push Button.

Now select label from the components “Widget Box” drag and drop it on the form. Repeat the same steps. Set the objectName as the status, change the font size, and delete the default text.

This is how the final application will look like. Now save this with the name design, in folder created on the desktop with the name HMI System. Now we will start the Raspberry Pi HMI programming.

Raspberry Pi HMI

We will do the programming in Thonny Python IDE. After you click on the Thonny Python IDE, this will open the editor where you can write your program. Simply copy and paste the following program into the editor.


Raspberry Pi HMI PYQT 5 Programming in Thonny Python IDE:

Raspberry Pi HMI PYQT 5 Program Explanation:

import sys

// Import sys in python loads the module named sys into the current namespace so that you can use the name of the module to access the functions and anything else defined in the module.

from PyQt5.QtCore import pyqtSlot

// The QtCore module contains the core non-GUI modules, including the event loop and the slot and signal function of Qt.

from PyQt5.QtWidgets import QApplication,QDialog

// The Qt Widgets Module provides a set of UI elements to build user interfaces in the classic desktop style.

from PyQt5.uic import loadUi

//this code is used for loading the design.ui file

import RPi.GPIO as gpio

// Inside RPi. GPIO, there are two ways to count the IO pins on a Raspberry Pi. The first is to use the numbering system of the BOARD. This refers to the Raspberry Pi board’s P1 header pin numbers. The advantage of using this numbering system is that irrespective of the RPi board revision, your hardware will always work. You won’t have to re-wire or update the code. BCM numbers are the second numbering system. This is a way of working at a lower level it applies to the channel numbers on the Broadcom SOC. You always have to deal with a diagram in which channel number goes to which pin on the RPi panel. Your script might break with Raspberry Pi board revisions.


To specify which one you use (mandatory):

GPIO.setmode(GPIO.BOARD)

or

GPIO.setmode(GPIO.BCM)

led=26 // variable initialization for led

gpio.setmode(gpio.BCM) // this code is already define above

gpio.setwarnings(False)

gpio.setup(led,gpio.OUT)

class HMI(QDialog):

def init(self):

super(HMI, self).init()

loadUi(‘design.ui’,self) // calling the main gui design which we create in pyqt5 designer

self.setWindowTitle(‘HMI System’)

self.btn.clicked.connect(self.on_off) // this code is used for calling the button function

@pyqtSlot()

def on_off(self): //this is button function

if gpio.input(led):

gpio.output(led,gpio.LOW) // this code is used for to turn off the the led

self.btn.setText(‘OFF’)

self.status.setText(‘LED Status is OFF’)

else:

gpio.output(led, gpio.HIGH)// this code is used for to turn on the led

self.btn.setText(‘ON’)

self.status.setText(‘LED Status is ON’)

app=QApplication(sys.argv)

widget=HMI() // variable initialization for main HMI class

widget.show()

sys.exit(app.exec_()) // this code is used for closing the window

 

Related Article:

Classes and objects

Python Recursion:

Watch Video Tutorial

Related Articles

5 Comments

  1. I want one help,i executed this program as per upper programming details but in my case only main.py window is showing on screen but button is not showing on screen,please help me i am stucked.

  2. As someone who is passionate about learning and personal growth, I can’t recommend this blog enough. The author’s writing is not only informative and engaging, but also deeply thoughtful and insightful. I appreciate the level of research and attention to detail that goes into each post, and the author’s commitment to providing factual and balanced perspectives. What I love most about this blog is its ability to challenge my assumptions and make me think deeply about important issues. Whether you’re looking to expand your knowledge on a specific topic or just looking for an interesting read, this blog is an absolute must-read.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button