Develop a Python application using Tkinter and PySerial to send text to a 16x2 LCD via an Arduino.





import serial
import tkinter
print("pyserial version:", serial.__version__)
print("Tkinter version:", tkinter.TkVersion)
root = tkinter.Tk()
root.title("Test")
root.mainloop()

Code Snippet: LCD 'Ready' Test code on Arduino IDE
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
lcd.init();
lcd.backlight();
Serial.begin(9600);
lcd.clear();
lcd.print("Ready...");
delay(2000);
lcd.clear();
}
void loop() {
if (Serial.available()) {
lcd.clear();
String input = Serial.readStringUntil('\n');
if (input.length() <= 16) {
lcd.setCursor(0, 0);
lcd.print(input);
} else {
lcd.setCursor(0, 0);
lcd.print(input.substring(0, 16));
lcd.setCursor(0, 1);
lcd.print(input.substring(16, 32));
}
}
}


Input: Text via Tkinter GUI.
import tkinter as tk
from tkinter import messagebox
import serial
import time
try:
ser = serial.Serial('COM5', 9600, timeout=1) # Matches your port
time.sleep(2) # Wait for RP2040 to initialize
except serial.SerialException as e:
messagebox.showerror("Error", f"Serial port not found: {e}")
exit()
root = tk.Tk()
root.title("RP2040 LCD Control")
root.geometry("300x200")
label = tk.Label(root, text="Enter text for LCD (max 32 chars):")
label.pack(pady=5)
entry = tk.Entry(root, width=30)
entry.pack(pady=5)
status_label = tk.Label(root, text="Ready")
status_label.pack(pady=5)
def send_text():
try:
text = entry.get()
if len(text) > 32:
text = text[:32]
ser.write(f"TEXT:{text}\n".encode())
ser.flush()
response = ser.readline().decode().strip()
if response == "OK":
status_label.config(text=f"Sent: {text}")
entry.delete(0, tk.END)
else:
messagebox.showerror("Error", "LCD write failed")
except serial.SerialException as e:
messagebox.showerror("Error", f"Serial communication failed: {e}")
def clear_lcd():
try:
ser.write(b"TEXT:\n")
ser.flush()
response = ser.readline().decode().strip()
if response == "OK":
status_label.config(text="LCD cleared")
else:
messagebox.showerror("Error", "LCD clear failed")
except serial.SerialException as e:
messagebox.showerror("Error", f"Serial communication failed: {e}")
send_button = tk.Button(root, text="Send to LCD", command=send_text)
send_button.pack(pady=5)
clear_button = tk.Button(root, text="Clear LCD", command=clear_lcd)
clear_button.pack(pady=5)
root.mainloop()
try:
ser.close()
except:
pass
