1+ '''
2+ Upload "e1_led_control.ino" sketch to arduino
3+ Use Pyhton 3.7
4+ Install respective packages if its missing or you get module not found error
5+ Please take care of indenting your code
6+ '''
7+ #Import following packages
8+ import serial
9+ import tkinter
10+ import tk_tools
11+
12+ #Connect to arduino via serial port
13+ #com6 --> Change port accordingly to yours
14+ arduino = serial .Serial ('com6' , 9600 )
15+
16+ #Function call for led_on button
17+ def led_on ():
18+ arduino .write (b'0' ) #write this value to arduino
19+ led .to_green (True ) #Glow the led to green --> Refer to tk-tools docs
20+ button_on .config (state = "disabled" ) #disable button_on
21+ button_off .config (state = "normal" ) # enable button_off to normal state
22+
23+ #function call for led_off button
24+ def led_off ():
25+ arduino .write (b'1' ) #write this value to arduino
26+ led .to_green () #Switch off the led --> Refer to tk-tools docs
27+ button_off .config (state = "disabled" ) #disable button_off
28+ button_on .config (state = "normal" ) #enable button_on to normal state
29+
30+ #exit function call for closing the program
31+ def close_window ():
32+ arduino .close () #close Serial connection with arduino
33+ window .destroy () #destro tkinter app
34+
35+ # MAIN
36+ window = tkinter .Tk () #create tkinter window
37+ window .title ("Arduino RGB Led Control" ) #give title
38+ window .configure (background = "white" ) #change background color
39+
40+ #Create buttons
41+ button_on = tkinter .Button (window , text = "ON" ,
42+ font = ('Verdana' ,16 ), padx = 50 , pady = 20 ,
43+ bg = "green" ,fg = "white" ,
44+ command = led_on )
45+
46+ button_off = tkinter .Button (window , text = "OFF" ,
47+ font = ('Verdana' ,16 ), padx = 50 , pady = 20 ,
48+ bg = "red" ,fg = "white" ,
49+ command = led_off )
50+
51+ button_exit = tkinter .Button (window , text = "Exit" ,
52+ font = ('Verdana' ,16 ),
53+ padx = 130 , pady = 20 ,
54+ command = close_window )
55+
56+ #Pack buttons
57+ button_on .grid (row = 1 ,column = 1 )
58+ button_off .grid (row = 1 ,column = 2 )
59+ button_off .config (state = "disabled" )
60+ button_exit .grid (row = 2 ,column = 1 , columnspan = 2 )
61+
62+ #Create interactive led using tk-tools
63+ led = tk_tools .Led (window , size = 100 )
64+ led .to_green () #set led to green switch off condition) --> Refer tk-tools docs
65+ led .grid (row = 0 ,column = 1 , columnspan = 2 )
66+
67+ #execute the loop
68+ window .mainloop ()
0 commit comments