Skip to content

15. Interface and Application Programming

This week I worked on programming a touch screen with control button to control motor speed and direction.

Group assignment:

Compare as many tool options as possible.

For more detail check the Group Page

Individual assignment

Write an application that interfaces a user with an input and/or output device that you made

Requirements

The minimum requirements for this project are the following

  • Raspberry Pi

  • Touch Screen

  • Connect touch screen to raspberryPi with USB and HDMI cable.

  • For Motor Wiring please check my documentation in Output devices week

  • Open Raspberry Pi terminal and install pyfirmata library for python by running the following command

    pip install pyfirmata

Tkinter Programming

Tkinter is the standard graphical user interface (GUI) library for Python. which provides a fast and easy way to create graphical user interface (GUI) applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit

For more detail about tkinter package please check docs.python.org

The Following Code will show how will use tkinter package to program an interface

## Import tkinter library 
from tkinter  import *
import tkinter.font
from time import sleep

# open new Window
win = Tk()

# Set window Title 
win.title("Motor Control")  

# Set font format
myFont = tkinter.font.Font(family = "Helvetica", size =12, weight = "bold")  

# Button Function 
def ledToggle():
    if ledButton["text"] == "Turn Motor off":
        ledButton["text"] = "Turn Motor ON"
    else:
        ledButton["text"]= "Turn Motor off"

# Close Button Function          
def close():
    win.destroy()

# Slider Function 
def speed(val):
    val = float(val)
    if val <0:
        val = abs(val) 
        selection = "Backward = " + str(val)
    elif val >0:
        selection = "Forward = " + str(val)
    else:
        selection = "Stop" 

    label.config(text = selection)    
    sleep(0.01)

# Create Button 
ledButton = Button(win, text="Turn Motor ON", font=myFont, command=ledToggle, bg="bisque2", height=1, width=24)
ledButton.grid(row=0,column=1)

# Create Slider
var = DoubleVar()
speedScale = Scale(win, from_=-0.5, to=0.5, resolution = 0.05, variable = var, command = speed, orient=HORIZONTAL, length = 500, width = 50 )
speedScale.grid(row=1,column=1)

# Create Lable
label = Label(win)
label.grid(row=2, column = 1)

# Create Exit Button 
exitButton = Button (win,text = "Exit", font = myFont, command = close, bg = "red", height = 1, width = 10)
exitButton.grid(row=10,column=1)

win.protocol("WM_DELETE_WINDOW", close)
win.mainloop()

You can download and test the code from here

Run the python code and you should got the following screen

Here is the full code for controlling a motor

from tkinter  import *
import tkinter.font
from time import sleep
from pyfirmata import Arduino, util
from pyfirmata import INPUT, OUTPUT, PWM

arduinoPort = "/dev/ttyUSB0"
board = Arduino(arduinoPort)
sleep(2)

pin3 = board.get_pin("d:3:o")                           # LED Pin
motorPin = board.get_pin("d:5:p")                       # PWM pin connected to LED
motorEL = board.get_pin("d:8:o")                        # Motor Enable
motorZF = board.get_pin("d:6:o")                        # Motor Direction

win = Tk()
win.title("Motor Control")
myFont = tkinter.font.Font(family = "Helvetica", size =12, weight = "bold")

def ledToggle():
    if ledButton["text"] == "Turn Motor off":
        pin3.write(0)
        motorEL.write(0)
        ledButton["text"] = "Turn Motor ON"
    else:
        pin3.write(1)
        motorEL.write(1)
        ledButton["text"]= "Turn Motor off"

def close():
    win.destroy()

def speed(val):
    val = float(val)
    if val <0:
        val = abs(val) 
        motorZF.write(0)
        selection = "Backward = " + str(val)
    elif val >0:
        motorZF.write(1)
        selection = "Forward = " + str(val)
    else:
        selection = "Stop" 

    label.config(text = selection)    
    motorPin.write(val)
    sleep(0.01)

ledButton = Button(win, text="Turn Motor ON", font=myFont, command=ledToggle, bg="bisque2", height=1, width=24)
ledButton.grid(row=0,column=1)

var = DoubleVar()
speedScale = Scale(win, from_=-0.5, to=0.5, resolution = 0.05, variable = var, command = speed, orient=HORIZONTAL, length = 500, width = 50 )
speedScale.grid(row=1,column=1)

label = Label(win)
label.grid(row=2, column = 1)

exitButton = Button (win,text = "Exit", font = myFont, command = close, bg = "red", height = 1, width = 10)
exitButton.grid(row=10,column=1)

win.protocol("WM_DELETE_WINDOW", close)
win.mainloop()
  • You can download the full python code from here

  • Run the python code

  • Click on Turn Motor ON and move the slider to control the motor speed and direction

This is how it works:


Last update: October 24, 2021