Last Updated or created 2026-07-20
Test for UART serial communication for my whack-a-mole game
CODE for Client
import board
import busio
import time
uart = busio.UART(
tx=board.GP16,
rx=board.GP17,
baudrate=115200,
timeout=1,
)
time.sleep(2)
commands = ["PING", "HELLO", "TEST"]
while True:
for cmd in commands:
print("Sending:", cmd)
uart.write((cmd + "\n").encode())
response = uart.readline()
if response:
print("Response:", response.decode().strip())
else:
print("No response")
time.sleep(1)
CODE for server
import board
import busio
import digitalio
import time
# UART pins
uart = busio.UART(
tx=board.GP0,
rx=board.GP1,
baudrate=115200,
timeout=0.1
)
# Onboard LED
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
print("UART LED server ready")
while True:
data = uart.readline()
if data:
command = data.decode("utf-8").strip()
print("Received:", command)
if command == "PING":
# Blink LED
led.value = True
time.sleep(0.2)
led.value = False
# Reply
uart.write(b"PONG\n")
else:
uart.write(b"UNKNOWN\n")
time.sleep(0.01)
