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.
Without any further delay let’s get started!!!
Amazon Purchase links are given below:
5.5 inch HDMI touch LCD: https://amzn.to/2ZKOMCu
Raspberry Pi 3 B+ https://amzn.to/2MCEf8v
Wireless Keyboard and Mouse Combo “Black Color”: https://amzn.to/356ufL4
Night vision Camera for Raspberry Pi: https://amzn.to/2ZwkCEi
lm7805 Voltage Regulator: https://amzn.to/2ERYoTJ
330-ohm resistors pack: https://amzn.to/2Qj1Igg
female DC power jack socket: https://amzn.to/377Pg9M
470uf capacitors: https://amzn.to/2MrN3xR
5×7 cm Vero board: https://amzn.to/37b7KWO
female headers: https://amzn.to/350w6RE
connection wires: https://amzn.to/2MvOJXd
Super Starter kit for Beginners: https://amzn.to/398dliF
Jumper Wires: https://amzn.to/2SrnBwo
Bread Board: https://amzn.to/2MxV5FM
12v Adaptor: https://amzn.to/2MuOlZk
PCB plate: https://amzn.to/2MuwNMB
Variable Supply: https://amzn.to/39d0KdP
Digital Multimeter: https://amzn.to/34WbVoa
Vero Board / stripboard: https://amzn.to/39nL9Zg
Soldering iron kit: “best” You guys should definitely purchase this: https://amzn.to/2PVwexF
Solder wire: https://amzn.to/2QlOvTS
Wire Stripper: https://amzn.to/353tYJa
PCB small portable drill machine: https://amzn.to/2MvQqnx
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!
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:
- 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.
sudo apt-get upgrade
- 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.
sudo apt-get update
- Now you will need to install the pyqt5 development headers using the command and press enter.
sudo apt-get install qt5-default
- Now install the pyqt5 qtcreator using the command and press enter.
sudo apt-get install qtcreator
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.
Open the Qt 5 designer software for designing the HMI or GUI application.
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.
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:
import sys from PyQt5.QtCore import pyqtSlot from PyQt5.QtWidgets import QApplication,QDialog from PyQt5.uic import loadUi import RPi.GPIO as gpio led=26 gpio.setmode(gpio.BCM) gpio.setwarnings(False) gpio.setup(led,gpio.OUT) class HMI(QDialog): def __init__(self): super(HMI, self).__init__() loadUi('design.ui',self) self.setWindowTitle('HMI System') self.btn.clicked.connect(self.on_off) @pyqtSlot() def on_off(self): if gpio.input(led): gpio.output(led,gpio.LOW) self.btn.setText('OFF') self.status.setText('LED Status is OFF') else: gpio.output(led, gpio.HIGH) self.btn.setText('ON') self.status.setText('LED Status is ON') app=QApplication(sys.argv) widget=HMI() widget.show() sys.exit(app.exec_())
Raspberry Pi HMI PYQT 5 Program Explanation:
// 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):
led=26 // variable initialization for led
gpio.setmode(gpio.BCM) // this code is already define above
loadUi(‘design.ui’,self) // calling the main gui design which we create in pyqt5 designer
self.btn.clicked.connect(self.on_off) // this code is used for calling the button function
def on_off(self): //this is button function
gpio.output(led,gpio.LOW) // this code is used for to turn off the the led
self.status.setText(‘LED Status is OFF’)
gpio.output(led, gpio.HIGH)// this code is used for to turn on the led
self.status.setText(‘LED Status is ON’)
widget=HMI() // variable initialization for main HMI class
sys.exit(app.exec_()) // this code is used for closing the window