Skip to content

Commit cd26457

Browse files
authored
Add files via upload
1 parent 4e65fb4 commit cd26457

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

EX1_led_control/e1_led_control.ino

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*Control an LED to be ON/OFF via arduino using PytHon GUI
2+
Note: Disconnect from Python IDE before uploading this sketch to your arduino
3+
*/
4+
5+
//Declare variable
6+
char serialData;
7+
8+
//Setup your baudrate & arduino pins
9+
void setup()
10+
{
11+
pinMode(13, OUTPUT);// LED pin on arduino
12+
Serial.begin(9600);
13+
}
14+
15+
//Run your program again and again using loop
16+
void loop()
17+
{
18+
19+
if (Serial.available() > 0) // send data only when you receive data from python
20+
{
21+
serialData = Serial.read();//read serial data and assign to variable
22+
Serial.print(serialData); //Print to check your data
23+
24+
//Condition statement
25+
if(serialData == '0')
26+
{
27+
digitalWrite(13, HIGH); //Make your LED glow
28+
}
29+
else if(serialData == '1')
30+
{
31+
digitalWrite(13, LOW); //Make your LED off
32+
}
33+
34+
}
35+
}

EX1_led_control/ex1_led_control.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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()

EX1_led_control/output.png

11.4 KB
Loading

0 commit comments

Comments
 (0)