Assignments

Week 11: Interface and Application Programming

This week's group assignment required us to explore and compare as many tool options as possible for communicating with a microcontroller. We will be comparing the Arduino Serial Monitor with Tkinter.

Arduino Serial Monitor

Webserver for RFID module

To explore on how to program the Arduino IDE and work with communicating it with a microcontroller board using hte Arduino Serial Monitor, we decided to learn how to create a webserver which interacts with an RFID module. The webserver should be able print the UID of the RFID card scanned and refresh the page when a new one is being scanned. It will print the UID of the latest RFID card scanned.

The connections made:

RFID module Xiao board
VCC 3V3
GND GND
TXD D5
RXD D4

NOTE: set the mode of the RFID module to UART mode. Learn more about it here.

We uploaded the following code to the XIAO ESP32-C3.

#include <WiFi.h>
        #include <WebServer.h>
        #include <Adafruit_PN532.h>

        // Wi-Fi credentials
        const char* ssid = "STUDENT";
        const char* password = "LetMeIn!";

        // Web server on port 80
        WebServer server(80);

        // Create PN532 instance on Serial1 (UART)
        #define PN532_RX D5
        #define PN532_TX D4
        HardwareSerial mySerial(1);
        Adafruit_PN532 nfc(PN532_TX, &mySerial);

        // To store latest UID
        String lastUID = "No card scanned yet";

        void handleRoot() {
        String html = "<!DOCTYPE html><html><head><meta http-equiv='refresh' content='2'/>"
                        "<title>RFID Log</title></head><body>"
                        "<h1>Last scanned RFID UID:</h1>"
                        "<p>" + lastUID + "</p>"
                        "</body></html>";
        server.send(200, "text/html", html);
        }

        void setup() {
        Serial.begin(115200);
        delay(1000);

        // Start Wi-Fi
        Serial.println("Connecting to WiFi...");
        WiFi.begin(ssid, password);
        while (WiFi.status() != WL_CONNECTED) {
            delay(500);
            Serial.print(".");
        }
        Serial.println("\nConnected to WiFi");
        Serial.print("IP address: ");
        Serial.println(WiFi.localIP());

        // Start web server
        server.on("/", handleRoot);
        server.begin();
        Serial.println("Web server started");

        // Start RFID on Serial1
        mySerial.begin(115200, SERIAL_8N1, PN532_RX, PN532_TX);
        nfc.begin();

        uint32_t versiondata = nfc.getFirmwareVersion();
        if (!versiondata) {
            Serial.println("Didn't find PN532");
            while (1) delay(10);
        }

        // Configure board to read RFID tags
        nfc.SAMConfig();
        Serial.println("Waiting for RFID card...");
        }

        void loop() {
        server.handleClient();

        // Check for an RFID card
        uint8_t success;
        uint8_t uid[7];    // Buffer to store UID
        uint8_t uidLength; // UID length

        success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);

        if (success) {
            Serial.print("RFID UID: ");
            String uidStr = "";
            for (uint8_t i = 0; i < uidLength; i++) {
            uidStr += String(uid[i], HEX);
            uidStr += " ";
            }
            Serial.println(uidStr);
            lastUID = uidStr;
            delay(1500); // debounce time
        }
        }
        

This is the result:

It was a success!

Tkinter

Tkinter is a built-in Python library that facilitates the creation of graphical user interfaces (GUIs) for applications. It provides a variety of widgets such as buttons, text boxes, labels, and menus, allowing developers to design interactive interfaces for their programs.

We downloaded the Python application first.

This is the code we uploaded generated with the help of AI, in order to create a webserver which controls an LED.

import serial
        from tkinter import *
        from tkinter import font

        def led_on():
        arduino_data.write(b'1')

        def led_off():
        arduino_data.write(b'0')

        # Set up the Tkinter window
        led_control_window = Tk()
        led_control_window.title('LED Control')

        # Set background color to pink
        led_control_window.configure(bg='pink')

        # Use a different font for buttons and labels
        my_font = font.Font(family='Helvetica', size=14, weight='bold')

        # Create the "Turn LED On" button with a different style
        btn_on = Button(led_control_window, text='Turn LED On', command=led_on, font=my_font, bg='lightgreen', fg='black', padx=20, pady=10)
        btn_on.pack(pady=10)

        # Create the "Turn LED Off" button with a different style
        btn_off = Button(led_control_window, text='Turn LED Off', command=led_off, font=my_font, bg='lightcoral', fg='black', padx=20, pady=10)
        btn_off.pack(pady=10)

        # Initialize the serial connection to Arduino
        arduino_data = serial.Serial('COM13', 9600)

        # Run the Tkinter event loop
        led_control_window.mainloop()
        

It was a success as it was able to control an LED without any problem.

Comparision table

Arduino Serial Monitor Tkinter
Platform Desktop Desktop
Language C++ Python
Communication Method Serial via USB Serial via USB
Pros Easy to use, great for debugging User-friendly interface, visually interactive
Cons Not user-friendly for non-developers Requires Python setup and programming

Thank You!