GUI control of Raspberry PI GPIO designed with Qt Creator in Raspbian

After writing about python program for controlling the GPIO pins of Raspberry PI in my previous tutorial- Link , here is the latest tutorial of controlling the GPIO pins of Raspberry PI using Qt based GUI. This is a simple GUI with drop-down and button interfaces to make the GPIO pins of Raspberry PI ON & OFF upon click of a button. The core of the GUI have been developed using Qt-Creator. We will be talking about the Qt creator part and the Python programming part in details.

Download The Complete Source Code >> HERE

Designing the Raspberry PI GUI control application in Qt Creator:

You can even run and use the Qt Creator in Windows platform and can port the same on Raspbian running Raspberry PI. As code generated from Qt Creator in cross platform supported there is no issue of compatibility. For the sake of convenience I have designed the GUI using the Qt Creator running on Windows10 PC and tan have linked/ported the .ui generated file from Qt creator with Python code. This is the very big advantage of cross platform GUI developer i.e. Qt Creator. I have been using Qt4 version of the Qt Creator. And will use the same to explain in this tutorial.

Steps to design the GUI from Raspberry PI GUI Controller:

Open the Qt Creator Desktop App, and this will open up the welcome screen of the Qt creator as given below.

Qt-Creator-RsPiLab

Now navigate to File > New File Or Project

This will open up a pop-up window. Select General from the Files and Classes list and select the Choose button below to proceed. Enter the name of your file depending upon your preferences and click Next and Finish to complete the wizard. Now click on File > Save File As and enter the file name with Save as type: selected as Qt Designer file (*.ui) and press Save. The complete steps are shown in the snaps given below:

Qt-Creator01-RsPiLab

Qt-Creator02-RsPiLab

Qt-Creator03-RsPiLab

This will create the file ledONOFF.ui ( I have used the file name as ledONOFF) file in Qt Creator.

Next copy paste the following code in the editor and save the ledONOFF.ui file.

Once file have been saved click on the Design tab as shown below:

Qt-Creator04-RsPiLab

On clicking the Design Tab this will open up the GUI core on which we have to work on further as shown below:

Qt-Creator05-RsPiLab

We will not go deep into the designing of this GUI. Qt Creator is very user friendly framework you can just drag and drop the modules like PushButton, Label, ComboBox etc from the Module selector into the working area and can even customize the basic look Property Windows pane as well. The details as given below:

Qt-Creator06-RsPiLabQt-Creator07-RsPiLab

I have not explained the complete step by step design of GUI which we are using for this project, but I will recommend you to follow some useful tutorials online for GUI development using Qt Designer. Try making some easy to use GUI in Qt Creator, this will make you familiar with most of the useful tools in Qt Creator. Above all be noted that the core file of your GUI design in Qt Creator is the one being followed by .ui extension. We are for this project is concerned with this file only for our GUI interface.

For better understanding on how to develop and run GUI application using Qt Creator in Raspbian based Raspberry PI2 board visit my previous article Link which have detail step by step tutorial including all the necessary installation instructions as well.

Python code to gain action control from the GUI

The main role of python script is to bind the ledonoff.ui file generated from Qt Creator designer into python program. Python program is used to initialize the Raspberry GPIO pins in output and make them logical High/Low depending upon the GUI button click. When button “HIGH” is clicked the selected GPIO pin in the ComboBox will turn High in output mode and when the button “LOW” is clicked the GPIO pin will turn Low.

Each time the respective button in the GUI is clicked the function is fired as per the python script and the set of action is performed. Here in this case we are turning GPIO Pins of Raspberry PI2 pins ON/OFF in output mode. To demonstrate this we will be connecting a LED with any of the GPIO pins and will make it ON/OFF upon button click.

Anode pin of the LED is connected with the +5VDC on Pin 2 of PI 2 board through a 1K Resistor for current limiting and the Cathode pin is connected to any one of the GPIO pin (Please ensure to connect a resistor of value greater than 1K in the circuit otherwise this will fry your PI board).

Now power on your Raspberry PI board and open the terminal. Enter sudo -i to gain permission and run sudo idle3 to open up the python shell as shown below.

DSCN0581

DSCN0582

In python shell click File > New and this open up the python editor. Just copy paste the following code in the editor and click save as to save the file as test.py (in my case I have named the python script file as test.py you can choose any name).

Now run the script by clicking on Run > Run Module in the Run tab. Make sure that the ledonoff.ui and test.py both the files are in the same directory.

DSCN0819

DSCN0820

GPIO21 turned to ON condition in Output Mode. In this case LED will be OFF as shown below

DSCN0827

DSCN0821

GPIO21 turned to ON condition in Output Mode. In this case LED will be ON as shown below

DSCN0832

This completes the tutorial. Just give this a try in your up-coming projects and keep commenting for any queries.

17 Comments

Add a Comment

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