/* simple gui to test the serial communication between an Arduino and the PC It has 3 buttons: - up counter - down counter - adc read setup: 1. programm your Arduino 2. addapt the serial port in your python script to the serial port used on your PC 3. run the python script PC: python with tkinter and pyserial MC: any Arduino 22.05.07 first version by mchris serial communication with python and Arduino: https://create.arduino.cc/projecthub/ansh2919/serial-communication-between-python-and-arduino-e7cce0 */ int Zaehler = 0; void setup() { Serial.begin(115200); } void loop() { while (!Serial.available()); uint8_t c = Serial.read(); switch (c) { case 'h': { Zaehler++; Serial.println(Zaehler); } break; case 'r': { Zaehler--; Serial.println(Zaehler); } break; case 'a': { Serial.println(analogRead(A0)); } break; } } // Python gui script for PC control /* import matplotlib from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk from matplotlib.figure import Figure import tkinter as tk from tkinter import ttk import serial McSeriallPort='/dev/ttyACM0' arduino = serial.Serial(port= McSeriallPort , baudrate=115200, timeout=.1) LARGE_FONT= ("Verdana", 12) Hauptfenster = tk.Tk() Hauptfenster.geometry("800x600") Hauptfenster.title('serielle Kommunikation Zaehler') Anzeiger_portName=ttk.Label(Hauptfenster, text="benutzer serieller Anschluss: "+McSeriallPort) Anzeiger_portName.place(x=20,y=10) Werte=[] def neueWerte(canvasRef,plotRef,comand): arduino.write(bytes(comand, 'utf-8')) data = arduino.readline() print(int(data)) Werte.append(int(data)) print(Werte) plotRef.cla() # clear graph plotRef.plot(Werte,'-o') plotRef.grid() canvasRef.draw() canvasRef.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1) #===================== GRAPH ======================================== Unterfenster = tk.Frame(Hauptfenster) Unterfenster.place(x=20,y=50); f = Figure(figsize=(6,4), dpi=100) a = f.add_subplot(111) a.plot([1,2,3,4,5,6,7,8],[1,2,1,3,8,9,3,5]) a.grid() canvas = FigureCanvasTkAgg(f, master=Unterfenster) canvas.draw() canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1) #===================== Knoepfe ====================================== KNOPF_adc_lesen = ttk.Button(Hauptfenster, text="adc lesen", command=lambda: neueWerte(canvas,a,'a')) KNOPF_adc_lesen.place(x=100, y=500) KNOPF_hoch = ttk.Button(Hauptfenster, text="hoch zaehlen", command=lambda: neueWerte(canvas,a,'h')) KNOPF_hoch.place(x=340, y=500) KNOPF_runter = ttk.Button(Hauptfenster, text="runter zaehlen", command=lambda: neueWerte(canvas,a,'r')) KNOPF_runter.place(x=500, y=500) Hauptfenster.mainloop() */