Skip to content

Week 14: Interface and Application Programming

Assignment:

  • Group: compare as many tool options as possible.
  • Individual: write an application for the embedded board that you made, that interfaces a user with an input and/or output device.

This week I built a live distance interface for my own XIAO RP2040 board: the board reads an HC-SR04 ultrasonic sensor and a desktop window shows the distance, a colour-coded zone and a live graph. Before that, I made an interactive projection with an ESP32 and TouchDesigner, kept below as an earlier experiment.

Group assignment

Our group work for this week is documented on the RIIDL Fab Lab group page: Interface and Application Programming.

What I learned: we explored different tools and workflows for creating and processing digital files: Fusion 360 for CAD modelling and preparing files for fabrication, and CopperCAM as part of the PCB fabrication workflow. Comparing them showed me that each tool covers one step of the chain from design to machine, and that choosing a tool means looking at what it takes in and what it hands on to the next step. For my individual interface I wrote a small Python desktop app (Tkinter + pySerial) that reads my own board's serial output directly. An earlier TouchDesigner experiment is kept at the bottom of this page.

Individual assignment: live distance interface for my board

The interface on the laptop, with my board and the HC-SR04 on the desk and a ruler in front of the sensor

My milled XIAO RP2040 board (Week 8) reads the HC-SR04 ultrasonic sensor and sends the distance to the laptop over USB serial. The interface window, titled "HC-SR04 live distance – XIAO RP2040", connects to the board's port (COM12) and updates live.

What the interface shows

The interface showing 8.7 cm in the red TOO CLOSE zone

  • The current distance as a large number at the top.
  • Three zones drawn as a bar next to a picture of the sensor: red TOO CLOSE, blue NEAR and green CLEAR, with a white marker showing where the object is.
  • The board's LED colour, which follows the zone: red when the object is too close, blue when it is near.
  • A live graph along the bottom, showing how the distance changed over the last few seconds.
  • "no echo" with a hint to point the sensor at something, when nothing is in range.

The interface showing "no echo" when nothing is in front of the sensor

Result

In the video I move an object along a ruler in front of the sensor. The number, the zone, the marker and the graph all follow it, for example 8.3 cm in the red TOO CLOSE zone and 18.2 cm in the blue NEAR zone.

How it works

  1. Board → laptop: the XIAO measures the echo time, converts it to centimetres and sends one reading per line over USB serial.
  2. Laptop: the interface opens the board's COM port, reads each line, and redraws the number, zone, marker and graph.
  3. Zones and LED: the same distance decides the zone on screen and the colour of the board's LED, so the screen and the board always agree.

Wiring for this test

HC-SR04 pin XIAO RP2040 pin
VCC 5V
GND GND
TRIG D0 (GPIO26)
ECHO D2 (GPIO28), through a 1 kΩ / 2 kΩ divider (5 V echo → ~3.3 V)

The zone colour is shown on the XIAO's own RGB user LED (red GPIO17, green GPIO16, blue GPIO25; active-low).

Code

The board runs MicroPython for this test. Two files:

1. On the board: hcsr04_distance_zones.py (saved on the XIAO as main.py). It measures about 15 times a second, prints one line like 9.3 cm (or out of range / no echo), and sets the RGB LED: below 10 cm red, 10–20 cm blue, above 20 cm green.

# HC-SR04 distance on XIAO RP2040, ~15 readings/s. TRIG -> D0 (GPIO26), ECHO -> divider -> D2 (GPIO28).
# On-board RGB LED: < 10 cm red, 10-20 cm blue, > 20 cm green, off when no echo.
import machine, time

trig = machine.Pin(26, machine.Pin.OUT, value=0)   # D0
echo = machine.Pin(28, machine.Pin.IN)             # D2

# On-board RGB LED, active-low (0 = on)
red = machine.Pin(17, machine.Pin.OUT, value=1)
green = machine.Pin(16, machine.Pin.OUT, value=1)
blue = machine.Pin(25, machine.Pin.OUT, value=1)


def show(led):
    for p in (red, green, blue):
        p.value(0 if p is led else 1)


while True:
    trig.value(1)
    time.sleep_us(10)                               # 10 us trigger pulse
    trig.value(0)
    t = machine.time_pulse_us(echo, 1, 30000)       # echo high time; 30 ms timeout (~5 m)
    if t < 0:
        show(None)
        print("out of range / no echo")
    else:
        cm = t * 0.0343 / 2                         # speed of sound 343 m/s, there and back
        show(red if cm < 10 else blue if cm <= 20 else green)
        print("%.1f cm" % cm)
    time.sleep_ms(66)                               # ~15 Hz

2. On the laptop: distance_visualizer.py. This is the interface window. Run it with python distance_visualizer.py COM12 (needs pip install pyserial; Tkinter comes with Python). The interface works in two parts:

  • A background thread keeps the serial port open at 115200 baud and stores the newest reading. If the line doesn't start with a number followed by "cm", it counts as "no echo". If the port is busy or unplugged, it shows "not available – retrying" instead of crashing.
  • The drawing loop runs about 60 times a second. It smooths the value (shown += (cm − shown) × 0.25) so the marker glides instead of jumping. It then draws the zone bands, a ruler, the animated ultrasonic wave fronts, the object marker, the big number and a 10-second graph.
  • Mouse wheel or +/− changes the scale from 30 to 400 cm; Esc closes the window.

AI assistance: I used Claude (Anthropic's AI assistant) to help write the interface and the board sketch for it. I described what I wanted, then tested it on my own board with the sensor and a ruler and checked that the readings, zones and LED colour matched. The board, the wiring and the testing are my own work.

Earlier experiment: popcorn projection with an ESP32 and TouchDesigner

1. ESP32 + ultrasonic sensor code setup

This stage focused on establishing communication between the ultrasonic sensor and the ESP32 WROOM Dev Kit using the Arduino IDE. The HC-SR04 ultrasonic sensor was programmed to continuously measure the distance between the installation and the approaching user. Instead of sending formatted text like “20 cm,” the code was intentionally optimized to output only raw numerical distance values through the serial monitor. This simplified the data pipeline and made it easier for TouchDesigner to directly interpret the incoming sensor data without additional string processing. The ESP32 acted as the bridge between the physical interaction and the projected digital environment. Proper pin mapping and serial baud-rate synchronization were important to ensure stable real-time communication. This setup formed the core interactive input system for the installation.

alt text alt text

2. Physical projection mapping setup

This image shows the physical setup used for the projection mapping experiment. A projector was carefully positioned above the installation space to project visuals directly onto a bowl placed beneath it. The bowl acted as the physical surface onto which animated popcorn visuals were mapped, creating the illusion that the bowl was actually filled with moving popcorn. The positioning and angle of the projector were adjusted manually to align the visuals accurately with the geometry of the bowl. This setup transformed a simple static object into an interactive illusion-based installation. The overall environment was intentionally kept dark to enhance the brightness, contrast, and realism of the projected visuals. This stage was essential in combining digital media with physical objects to create an immersive spatial experience.

alt text alt text

3. TouchDesigner interactive workflow

This stage involved integrating the sensor data into TouchDesigner to control the interactive projection in real time. The numerical values coming from the ESP32 through serial communication were imported into TouchDesigner and used as live input parameters. As a user approached the installation or interacted near the sensor, the ultrasonic readings changed dynamically, triggering corresponding visual behavior inside the projection system. The projected popcorn visuals responded based on proximity, making the installation feel reactive and alive. TouchDesigner was used because of its node-based workflow and real-time visual processing capabilities, which allowed rapid experimentation with interaction, projection mapping, and animation control. This integration connected physical interaction, sensor data, and digital visuals into one cohesive immersive experience.

alt text alt text

4. Result