Week 14 Interface and Application Programming
Click Here to Access the Group Assignment
Write an application for the embedded board that you made. that interfaces a user with an input and/or output device(s)
This week, I chose to use the board I used for Input and Output week. I wanted to control the motor with an application.

This is the board I chose to use.
Code
For the Xiao RP2040 Microcontroller, I used ChatGPT to generate code. This code reads serial commands, (“ON”/“OFF”) from USB and controls a PWM output to switch the DC motor on or off.
Here is the code below:
from machine import Pin, PWM
import sys
import select
motor = PWM(Pin(28))
motor.freq(2000)
while True:
if sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
cmd = sys.stdin.readline().strip()
if cmd == "ON":
motor.duty_u16(65535)
elif cmd == "OFF":
motor.duty_u16(0)
For the interface, it provides a simple desktop interface with ON/OFF buttons that send serial commands to the microcontroller over USB to control the motor.
Below is the code:
import serial
import tkinter as tk
# CHANGE THIS to your port!
ser = serial.Serial('COM8', 115200)
def motor_on():
ser.write(b'ON\n')
def motor_off():
ser.write(b'OFF\n')
root = tk.Tk()
root.title("Motor Control")
btn_on = tk.Button(root, text="ON", command=motor_on, width=15, height=3)
btn_on.pack(pady=10)
btn_off = tk.Button(root, text="OFF", command=motor_off, width=15, height=3)
btn_off.pack(pady=10)
root.mainloop()
Running the Code
First, I plug in the Xiao RP2040. I made sure the microcontroller is connected to COM8, otherwise, I would need to move the microcontroller to COM8, or modify the code to whichever COM the microcontroller is connected to.
To run the Python GUI, in the VSCode terminal, where my python file lives, I ran:

After entering that into the terminal, my application window pops up.

This is the application.
Testing the Application
This is the application running. As you can see, the on button and off button turn the DC motor on and off, respectively.
Reflection
Overall, I found this week to be very refreshing. Machine week was very difficult, and the molding and casting group work took longer than expected. I completed this week very quickly, and found myself able to relax.