Custom GUI controller
Find out how you can control real-world objects using Python code, via a custom user interface, in less than 20 lines of code!
Using the Raspberry Pi GPIO (General Purpose Input Output) can seem scary at first. But fear not: using just 19 lines of Python code, you can create a graphical user interface (GUI) to control a real-world LED and buzzer.
Building the circuit for this project is relatively straightforward: just follow our diagram and check each connection as you go. When all of the connections are made, connect your accessories and boot your Raspberry Pi to the desktop.
We will need to install one piece of software before we start coding. Open a terminal (the icon is in the top-left of the desktop) and type: $ sudo pip3 install guizero
Close the terminal when installation is complete. We are using the latest Raspbian image, which comes with Thonny, an easyto‑use Python editor. You can find this in the programming menu. We shall start our project by importing three libraries of pre-written Python code. These are ‘guizero’, used to create the GUI application with images, buttons, slider controls and text; GPIO Zero, which links our code to electronic components; and from the time library we import ‘sleep’, used to pace our code. from guizero import App, Picture, PushButton, Slider, Text from gpiozero import PWMLED, Buzzer from time import sleep
To use the LED and buzzer in our project we need to tell GPIOZero which pins they are connected to: blue = PWMLED(17) bz = Buzzer(27)
We now create two functions to contain the code that will control the blue LED and the buzzer. Our first function gets the value from a slider, which we shall create later, then divides it by 10 and passes the value to the ‘blue’ variable, which controls the LED brightness: def led_brightness(x): blue.value = (brightness.get() / 10) The second function is used to beep the buzzer five times in a second (0.1 second on, then off) before turning the buzzer off. def buzzer_beep():
bz.beep(0.1,0.1) sleep(1) bz.off() Next is the dialog box that will contain our application. Our ‘app’ has a title to identify it, and we set the height and width, in pixels, to contain the elements that make our app: app = App(title=“GPIO Zero Control Panel”, height=300, width=500)
Our first two elements in the app are a picture, in this case the Linux Format logo. Images must be in the GIF format. The second element is plain text, to indicate the instructions. We style the font and colour: picture = Picture(app, image=“lxf.gif”) instructions = Text(app, text=“Instructions:”, size=20, font=“Times New Roman”, color=“red”)
Now we add some extra text, to advise the user on how to control the LED; and then create a slider, ‘brightness’ with a function called ‘led_brightness’. The value of the slider, between 0 and 10 is passed to the function, where PWMLED is used to control the brightness of the LED, between 0 and 1.0. led_text = Text(app, text=“Use the slider to control the LED brightness”) brightness = Slider(app, command=led_ brightness, start=0, end=10)
We then create a similar explanation on how the buzzer works, and create a button using the earlier ‘buzzer_beep’ function. buzzer_text = Text(app, text=“Press the button to beep”) button = PushButton(app, command=buzzer_ beep, text=“Buzzer”)
Our last line of Python code updates the application to show all the elements. app.display()
Save your work and click the Run icon in the main menu. Your application will come to life, ready for you to control.