15. Interface and Application Programming

This week I used both the Arduino IDE and Python to create a simple interface that controls an LED on the Arduino Uno using serial communication.

Research

Before this week, I had never made an app or even knew how to make one. I was excited — and also somehow scared — by the daunting challenge of... an app!? But it turned out okay.

We did some soft building before actually getting to the fun or cool stuff that apps do. We just experimented using Python IDLE, played around on the computer, and used the terminal to communicate with it. We didn’t get involved with a microcontroller just yet.

Group Work

Sending Serial Data in Arduino IDE

We wrote custom code for the Arduino Uno using the Arduino IDE to listen for serial input at a baud rate of 9600. The program was set to turn the onboard LED on when it received the number 1, and turn it off when it received the number 0 through the serial monitor.

We found a helpful code example on the Arduino Forum through a quick online search. After uploading the program, we tested the behavior using the serial monitor. Images showing the LED in both on and off states are included above.

void setup() { Serial.begin(9600); // Initialize serial communication at 9600 baud pinMode(LED_BUILTIN, OUTPUT); // Set the built-in LED pin as an output } void loop() { if (Serial.available() > 0) { // Check if any data is available in the serial buffer char receivedChar = Serial.read(); // Read the character from the serial buffer if (receivedChar == '1') { // Check if the received character is '1' digitalWrite(LED_BUILTIN, HIGH); // Turn on the built-in LED } else if (receivedChar == '0') { // Optional: Turn off LED if '0' is received digitalWrite(LED_BUILTIN, LOW); // Turn off the built-in LED } } delay(100); // Add a small delay to avoid overwhelming the serial monitor }

Sending Serial Data with Python

To send data to the Arduino from Python, we first downloaded and installed Python from the official website.

Download command: Python Download Link

Required libraries (install via terminal or command prompt):

Tkinter is a built-in Python library for creating graphical user interfaces. It allows you to build windows, buttons, labels, and other components that enable interaction with your programs. It is one of the most commonly used GUI frameworks in Python.

After identifying the correct COM port in Arduino IDE (ours was COM25), we closed the Arduino IDE and opened Python IDLE. We entered a Python script (shown in the screenshot below) that communicates with the Arduino over the serial port.

The Arduino was already programmed to respond to serial messages with LED control using the 0 and 1 commands. This time, the Python script created a simple GUI window with on and off buttons. Clicking these buttons sent the appropriate signals to the Arduino and turned the onboard LED on and off.

Our instructor recommended saving the Python script as a reusable template, especially if planning to expand GUI-based serial interaction in the future.

Python Code Used

import tkinter import serial # Connect to microcontroller port = serial.Serial('COM6', 9600, timeout=1) # Turn on light def handleOn(): port.write(b'1') # Turn off light def handleOff(): port.write(b'0') # Create window window = tkinter.Tk() window.title("LED Control") # Add label label = tkinter.Label(window, text='BUTTON BUTTON BUTTON') label.pack() # On button on_button = tkinter.Button(window, text='On', command=handleOn) on_button.pack() # Off button off_button = tkinter.Button(window, text='Off', command=handleOff) off_button.pack() # Start the GUI loop window.mainloop()

My Own App