Tuesday, March 26, 2013

Control Arduino LED using Python with GUI

This example demonstrate how to control Arduino LED using Python with GUI, using tkinter library.


#tkinter for Python 3.x
#Tkinter for Python 2.x

import serial
import time
import tkinter

def quit():
    global tkTop
    tkTop.destroy()

def setCheckButtonText():
    if varCheckButton.get():
        varLabel.set("LED ON")
        ser.write(bytes('H', 'UTF-8'))
    else:
        varLabel.set("LED OFF")
        ser.write(bytes('L', 'UTF-8'))

ser = serial.Serial('/dev/ttyACM1', 9600)
print("Reset Arduino")
time.sleep(3)
ser.write(bytes('L', 'UTF-8'))

tkTop = tkinter.Tk()
tkTop.geometry('300x200')

varLabel = tkinter.StringVar()
tkLabel = tkinter.Label(textvariable=varLabel)
tkLabel.pack()

varCheckButton = tkinter.IntVar()
tkCheckButton = tkinter.Checkbutton(
    tkTop,
    text="Control Arduino LED",
    variable=varCheckButton,
    command=setCheckButtonText)
tkCheckButton.pack(anchor=tkinter.CENTER)

tkButtonQuit = tkinter.Button(
    tkTop,
    text="Quit",
    command=quit)
tkButtonQuit.pack()

tkinter.mainloop()

For the code in Arduino side, refer to the post "Communicate with Arduino in Python programmatically".

1 comment:

  1. Thank you for the helpful example, I was wondering if a gui developed for an arduino in python could be made executable in such a way that regular users could make use of it.

    ReplyDelete