Activity 3 - Python GUI With Tkinter and Arduino
Activity 3 - Python GUI With Tkinter and Arduino
129
130 Internet of Things with Raspberry Pi and Arduino
FIGURE 8.1
Tk version.
TABLE 8.1
The Tkinter Widgets to Develop GUI
Widget Description
Tk() Root widget need in every program
Label() It shows an image or a text
Button() Execute actions
Entry() Text field to provide inputs
Scale() It provides a numeric value by dragging the
slider
Checkbox() It enables toggling between two values
FIGURE 8.2
Circuit diagram to interface LED.
Python GUI with Tkinter and Arduino 131
FIGURE 8.3
Tkinter GUI to control LED with fixed delay.
FIGURE 8.4
LED ON/OFF.
FIGURE 8.5
Tkinter GUI to control LED with variable delay.
FIGURE 8.6
Screenshot for LED ON/OFF with delay.
8.3.1 Recipe
import Tkinter # add Tkinter library
import pyfirmata # add pyfirmata library
import time as wait # add time library
board = pyfirmata.Arduino('/dev/ttyUSB0')
wait.sleep(5) # delay of 5 Sec
led_Pin = board.get_pin('d:7:o') # connect led to pin 7 and used as output
def call_led_blink_pwm():
time_Period = time_Period_Entry.get()
134 Internet of Things with Raspberry Pi and Arduino
time_Period = float(time_Period)
led_Brightness = brightness_Scale.get()
led_Brightness = float(led_Brightness)
button.config(state=Tkinter.DISABLED)
led_Pin.write(led_Brightness/100.0)
print 'LED brightness control' # print on terminal
wait.sleep(time_Period)
led_Pin.write(0) # make led_Pin to LOW
button.config(state=Tkinter.ACTIVE)
TOP = Tkinter.Tk()
time_Period_Entry = Tkinter.Entry(TOP, bd=7, width=30)
time_Period_Entry.pack()
time_Period_Entry.focus_set()
brightness_Scale = Tkinter.Scale(TOP, from_=0, to=100, orient=Tkinter.
VERTICAL)
brightness_Scale.pack()
button = Tkinter.Button(TOP, text="Start", command =
call_led_blink_pwm)
button.pack()
TOP.mainloop()
FIGURE 8.7
Tkinter GUI for LED brigtness control.
Python GUI with Tkinter and Arduino 135
FIGURE 8.8
Screenshot for LED brightness control.
FIGURE 8.9
Circuit diagram to interface multiple LEDs.
136 Internet of Things with Raspberry Pi and Arduino
8.4.1 Recipe
import Tkinter # add Tkinter library
import pyfirmata # add pyfirmata library
import time as wait # add time library
board = pyfirmata.Arduino('/dev/ttyUSB0')
wait.sleep(5) # delay of 5 Sec
red_led_pin = board.get_pin('d:7:o') # connect led to pin 7 and used as
output
green_led_pin = board.get_pin('d:6:o') # connect led to pin 6 and used
as output
def start_button_press():
red_led_pin.write(red_led_Var.get())
green_led_pin.write(green_led_Var.get())
print 'start...' # print on terminal
def stop_button_press():
red_led_pin.write(0) # make pin 7 to HIGH
green_led_pin.write(0) # make pin 6 to HIGH
print 'stop....' # print on terminal
TOP = Tkinter.Tk()
red_led_Var = Tkinter.IntVar()
red_CheckBox = Tkinter.Checkbutton(TOP, text="Red_LED_status",
variable=red_led_Var)
red_CheckBox.grid(column=1, row=1)
green_led_Var = Tkinter.IntVar()
green_CheckBox = Tkinter.Checkbutton(TOP, text="Green_LED_status",
variable=green_led_Var)
green_CheckBox.grid(column=2, row=1)
start_Button = Tkinter.Button(TOP, text="Start_button", command =
start_button_press)
start_Button.grid(column=1, row=2)
stop_Button = Tkinter.Button(TOP, text="Stop_button", command =
stop_button_press)
stop_Button.grid(column=2, row=2)
exit_Button = Tkinter.Button(TOP, text="Exit_button", command=TOP.
quit)
exit_Button.grid(column=3, row=2)
TOP.mainloop()
Python GUI with Tkinter and Arduino 137
FIGURE 8.10
Tkinter GUI for multiple LEDs.
FIGURE 8.11
Screenshot for status window.