Python GUI (Tkinter) with Arduino

It looks so crazy but it works :)
import serial
import time
import tkinter
def quit():
global tkTop
ser.write(bytes('L', 'UTF-8'))
tkTop.destroy()

def set_button1_state():
b += 1
varLabel.set("LED ON ")
ser.write(bytes('H', 'UTF-8'))
varLabel2.set(b)
print(b)

def set_button2_state():
varLabel.set("LED OFF")
ser.write(bytes('L', 'UTF-8')
ser = serial.Serial('com7', 9600)
print("Reset Arduino")
time.sleep(3)
ser.write(bytes('L', 'UTF-8'))
import serial
import time
import tkinter


def quit():
global tkTop
ser.write(bytes('L', 'UTF-8'))
tkTop.destroy()

def set_button1_state():
global b
b += 1
varLabel.set("LED ON ")
ser.write(bytes('H', 'UTF-8'))
varLabel2.set(b)
print(b)

def set_button2_state():
varLabel.set("LED OFF")
ser.write(bytes('L', 'UTF-8'))

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

tkTop = tkinter.Tk()
tkTop.geometry('300x200')
tkTop.title("IoT24hours")
label3 = tkinter.Label(text = 'Building Python GUI to interface an arduino,'
'\n and control an LED',font=("Courier", 12,'bold')).pack()
tkTop.counter = 0
b = tkTop.counter

varLabel = tkinter.IntVar()
tkLabel = tkinter.Label(textvariable=varLabel, )
tkLabel.pack()

varLabel2 = tkinter.IntVar()
tkLabel2 = tkinter.Label(textvariable=varLabel2, )
tkLabel2.pack()

button1 = tkinter.IntVar()
button1state = tkinter.Button(tkTop,
text="ON",
command=set_button1_state,
height = 4,
fg = "black",
width = 8,
bd = 5,
activebackground='green'
)
button1state.pack(side='top', ipadx=10, padx=10, pady=15)

button2 = tkinter.IntVar()
button2state = tkinter.Button(tkTop,
text="OFF",
command=set_button2_state,
height = 4,
fg = "black",
width = 8,
bd = 5
)
button2state.pack(side='top', ipadx=10, padx=10, pady=15)

tkButtonQuit = tkinter.Button(
tkTop,
text="Quit",
command=quit,
height = 4,
fg = "black",
width = 8,
bg = 'yellow',
bd = 5
)
tkButtonQuit.pack(side='top', ipadx=10, padx=10, pady=15)

tkinter.mainloop()
const int ledPin = 13; // pin the LED is attached to
int incomingByte; // variable stores serial data
void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
}
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
if (incomingByte == 'H') {
digitalWrite(ledPin, HIGH);
Serial.println("Getting H"); //print out to serial monitor to check state
}
//กำหนดเงื่อนไขถ้ารับค่า H เข้ามา แล้วสั่งให้หลอด LED สว่าง จากนั้นทำการปริ้นซ์ คำว่า "Getting H" ออกทางซีเรียลมอนิเตอร์ เพื่อดีบักหรือตรวจสอบดูค่า
if (incomingByte == 'L') {
digitalWrite(ledPin, LOW);
Serial.println("Getting L"); //print out to serial monitor to check state
}
I use Eclipse IDE for writing arduino code instead of Arduino IDE. I enjoy and love to use it so much ahaha.
// Arduino IDE:
// File -> Examples -> 04.Communication -> PhysicalPixel

const int ledPin = 13; // pin the LED is attached to
int incomingByte; // variable stores serial data

void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
}

void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
// if it's a capital H (ASCII 72), turn on the LED:
if (incomingByte == 'H') {
digitalWrite(ledPin, HIGH);
Serial.println("Getting H"); //print out to serial monitor to check state
}
// if it's an L (ASCII 76) turn off the LED:
if (incomingByte == 'L') {
digitalWrite(ledPin, LOW);
Serial.println("Getting L"); //print out to serial monitor to check state
}
}
}
  1. Pressing buttons from GUI to send data to an arduino board via serial communication.
  2. Receiving input data from GUI buttons we pressed before to use that data to apply to if- else conditions to control an LED, or any devices you desire.

--

--

--

Full Stack Python Developers

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

AWS Lambda : The Base Image Matters

Moving to Cloud

Improve Oracle database performance — Rules of Thumb, Part 2

How ITIL4 and SRE align with DevOps

How to Try it within 5 minutes (Private Key JWT Client Authentication Support in WSO2 Identity…

Why Consulting Firms Are a Good Choice for a Software Engineer

Step by Step Guide on setting up SMB, RDP, SSH and HTTP HoneyPot in Raspberry Pi

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
stackpython

stackpython

Full Stack Python Developers

More from Medium

Python GUI colorchooser 🎨

Play Music Using Python Subprocess

Building GUI with Tkinter

Controlling home lights and appliances with Python