Python Forum
[Tkinter] Tkinter widgets driving Arduino uno output pins
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Tkinter widgets driving Arduino uno output pins
#1
Program is partial working. I am able to turn pin 13 on and off. What I am trying to do is have "true" feed back when the pin changes state. I am trying to send pin 13 status ("1" or "0") to a tkinter label widget in python (below the "Exit" button) If I run using Arduino serial monitor everything works correctly. I get the text "Hello" and when I push the on button I get a "1" (no quotes) and when I push the off button I get a "0" (no quotes). Pushing Exit quits the program. Since I wrote this I am now getting "PY_VAR1 in my label field. I believe this is a python problem, I will also post to Arduino. Thanks for you help in advance

Jim


Python 3.6
[python]
----------------------------------------
#! /usr/bin/env python

import serial
import time
from tkinter import *


ArduinoSerial = serial.Serial('com3', 9600, timeout=.1)
time.sleep(2)
global status1

def led_on():
ArduinoSerial.write(b'1')
status1 = (ArduinoSerial.readline() .decode('utf-8').strip())
print(status1)

def led_off():
ArduinoSerial.write(b'0')
status1 = (ArduinoSerial.readline().decode('utf-8').strip())
print(status1)

def led_Exit():
ArduinoSerial.write(b'0')
status1 = "Quit"
print(status1)
quit()

root = Tk()
status1 = StringVar()
root.title("Arduino Push Buttons")
btn1 = Button(root, text="Led on", command=led_on)
btn2 = Button(root, text="Led off", command=led_off)
btn3 = Button(root, text="Exit", command=led_Exit)
msg1 = Label (root, textvariable = status1, relief=RAISED,width = 20)
btn1.pack()
btn2.pack()
btn3.pack()
msg1.pack()
status1.set(StringVar())
root.mainloop()

/python]
Python 3.6
-----------------------------------------------------------

Arduino 1.6.12

int data;

void setup()
{
Serial.begin(9600); //initialize serial COM at 9600 baudrate
pinMode(13, OUTPUT); //make the LED pin (13) as output
digitalWrite (13, LOW);
}
void loop()
{
if (Serial.available()> 0)
{
data = Serial.read();
}
if (data == '1')
{
digitalWrite (13, HIGH);
Serial.println(digitalRead(13));
data = '2';
}
else if (data == '0')
{
digitalWrite (13, LOW);
Serial.println(digitalRead(13));
data = '2';
}
}

----------------------------------------------
Reply
#2
please re-post using code tags! see BBCODE
Reply
#3
Program is partial working. I am able to turn pin 13 on and off. What I am trying to do is have "true" feed back when the pin changes state. I am trying to send pin 13 status ("1" or "0") to a tkinter label widget in python (below the "Exit" button) If I run using serial monitor everything works correctly. I get the text "Hello" and when I push the on button I get a "1" (no quotes) and when I push the off button I get a "0" (no quotes). Pushing Exit quits the program. Since I wrote this I am getting "PY_VAR1 in my label field. I believe this is a python problem, I will also post to Arduino. Thanks for you help in advance

Jim

#! /usr/bin/env python

import serial
import time
from tkinter import *


ArduinoSerial = serial.Serial('com3', 9600, timeout=.1)
time.sleep(2)
global status1

def led_on():
    ArduinoSerial.write(b'1')
    status1 = (ArduinoSerial.readline() .decode('utf-8').strip())
    print(status1)

def led_off():
    ArduinoSerial.write(b'0')
    status1 = (ArduinoSerial.readline().decode('utf-8').strip())
    print(status1)

def led_Exit():
    ArduinoSerial.write(b'0')
    status1 = "Quit"
    print(status1)
    quit()

root = Tk()
status1 = StringVar()
root.title("Arduino Push Buttons")
btn1 = Button(root, text="Led on", command=led_on)
btn2 = Button(root, text="Led off", command=led_off)
btn3 = Button(root, text="Exit", command=led_Exit)
msg1 = Label (root, textvariable = status1, relief=RAISED,width = 20)
btn1.pack()
btn2.pack()
btn3.pack()
msg1.pack()
status1.set(StringVar())
root.mainloop()

  1. ARDUINO CODE
    
    int data;
    
    void setup() 
    { 
      Serial.begin(9600); //initialize serial COM at 9600 baudrate
      pinMode(13, OUTPUT); //make the LED pin (13) as output
      digitalWrite (13, LOW);
    } 
    void loop() 
    {
      if (Serial.available()> 0)
      {
        data = Serial.read();
      }
       if (data == '1')
        {
        digitalWrite (13, HIGH); 
        Serial.println(digitalRead(13));
        data = '2';
        }
       else if (data == '0')
        {
        digitalWrite (13, LOW);
        Serial.println(digitalRead(13));
        data = '2';
        }
    }
    ARDUINO CODE
Reply
#4
After several weeks of reading web pages and books, I have the Python / Arduino serial interface working. It was all easy until I tried to get actual (live) status of Arduino digital outputs. It was finally solved using the “.set()” function, which was suggested by “Don Rozenberg the author of PAGE - Python Automatic GUI Generator". Thanks to all for the feedback I received. I have attached the Python and Arduino scripts.

Python ver 37-32, tkinter. TkVersion 8.6

#! /usr/bin/env python
# Arduino_Driver.py

import serial
import time
from tkinter import *

ArduinoSerial = serial.Serial('COM4', 9600, timeout=.1) # open serial port
time.sleep(2)

def led_on():
    ArduinoSerial.write(b'1') # set Arduino output pin 13 high
    var1.set(ArduinoSerial.readline().decode('utf-8').strip())# get Arduino output pin 13 status
    if var1.get() == '1':
        var1.set('Pin 13 is ON')

def led_off():
    ArduinoSerial.write(b'0') # set Arduino output pin 13 low
    var1.set(ArduinoSerial.readline().decode('utf-8').strip()) # get Arduino output pin 13 status
    if var1.get() == '0':
        var1.set('Pin 13 is OFF')

def led_Exit():
    ArduinoSerial.write(b'0') # set Arduino output pin 13 low and quit
    ArduinoSerial.close() # close serial port
    quit()

root = Tk()

font1 = "-family {Courier New} -size 10 -weight bold -slant roman -underline 0 -overstrike 0"
font2 = "-family {Segoe UI} -size 12 -weight bold -slant roman -underline 0 -overstrike 0"
font3 = "-family {@Arial Unicode MS} -size 12 -weight bold -slant roman -underline 1 -overstrike 0"
font4 = "-family {Segoe UI} -size 10 -weight bold -slant roman -underline 0 -overstrike 0"

var1 = StringVar()
var1.set('OFF')
root.geometry("430x200+600+200")
root.title("                                         Arduino Output Control")
root.configure(background="#d9d9d9")


btn1 = Button(root, text='Press for Led ON', font = font1, bg = 'light green', highlightbackground= 'black',   \
        borderwidth = 3, activebackground = 'light gray', relief=RAISED, command=led_on) # activate Arduino pin 13
btn1.place(relx=0.34, rely=0.10, height=30, width=150)

btn2 = Button(root, text='Press for Led OFF', font = font1, bg = 'red2', fg = 'white', highlightbackground= 'black',   \
        borderwidth = 3, activebackground = 'light gray', relief=RAISED, command=led_off) # deactivate Arduino pin 13
btn2.place(relx=0.34, rely=0.30, height=30, width=150)

btn3 = Button(root, text='Press to Exit', font = font3, bg = 'maroon1', highlightbackground= 'black',   \
        borderwidth = 3, activebackground = 'gray', relief=RAISED,command=led_Exit) # close serial port and quit program
btn3.place(relx=0.34, rely=0.50, height=30, width=150)

lbl1 = Label (root, textvariable = var1, font = font2, bg = 'light blue', fg = 'black', highlightbackground= 'black',   \
        borderwidth = 3, relief=RAISED) # message label for status of arduino pin 13
lbl1.place(relx=0.34, rely=0.70, height=30, width=150)

root.mainloop()
Arduino ver 1.8.5, Arduino UNO

int data;

void setup() 
{ 
  Serial.begin(9600); //initialize serial COM at 9600 baudrate
  pinMode(13, OUTPUT); //make the LED pin (13) as output
  digitalWrite (13, LOW);
} 
void loop() 
{
  if (Serial.available()> 0)
  {
    data = Serial.read();
  }
   if (data == '1')
    {
    digitalWrite (13, HIGH); 
    Serial.println(digitalRead(13));
    data = '2';
    }
   else if (data == '0')
    {
    digitalWrite (13, LOW);
    Serial.println(digitalRead(13));
    data = '2';
    }
}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter | entry output. Sap2ch 1 347 Sep-25-2021, 12:38 AM
Last Post: Yoriz
  .get() from generated Entry widgets in tkinter snakes 4 1,129 May-03-2021, 11:26 PM
Last Post: snakes
  Super basic tkinter arduino issue Kurta 3 910 Jan-07-2021, 05:22 PM
Last Post: deanhystad
  [Tkinter] acceleration of data output in treeview tkinter Vladimir1984 4 1,370 Nov-21-2020, 03:43 PM
Last Post: Vladimir1984
  Displaying output in GUI ( Tkinter) Zouloutamtam 7 6,980 Sep-29-2020, 02:08 PM
Last Post: Zouloutamtam
  Using Tkinter widgets on child window chewy1418 8 3,963 Feb-27-2020, 10:34 PM
Last Post: Marbelous
  Active tkinter text output during loop dvanommen 2 6,698 Oct-18-2019, 02:23 PM
Last Post: dvanommen
  sQlite3 output to tkinter treeview - how do I set / increase width of the output? dewijones67 5 4,257 Jan-23-2019, 08:45 AM
Last Post: Larz60+
  [Tkinter] tkinter - unexpected output - don't know how func is triggered before calling omm 8 2,295 Dec-11-2018, 08:09 PM
Last Post: Gribouillis
  Read Pins by UART Serial Communication Tonyita 0 1,282 May-06-2018, 10:13 PM
Last Post: Tonyita

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020