Industrial FABLAB UCuenca

Week 15 - Interface and Application Programming

Object detection interface for the conveyor belt using XIAO ESP32-C3, HC-SR04, a custom generic board, and Blynk.

Group Assignment

The group assignment reviewed interface options and compared how applications communicate with embedded devices. The complete group documentation is available on the lab page.

View Full Group Assignment Page

Assignment Objectives

The individual assignment was to write an application that interfaces a user with an input or output device that I made. For this work I reused the conveyor belt developed in the machine/mechanical design assignment and added an interface to monitor object detection in real time.

XIAO ESP32-C3 HC-SR04 ultrasonic sensor Custom generic PCB Blynk IoT dashboard Conveyor belt sensing
The final interface shows distance, process status, cycle count, cycle time, average cycle time, cycles per minute, and a reset command from the Blynk dashboard.

System Overview

The system detects when an object passes through the conveyor belt sensing area. The HC-SR04 measures distance, the XIAO ESP32-C3 processes the signal, and the Blynk web dashboard displays the process data. This turns the conveyor belt into a small monitoring station for counting cycles and measuring how long an object stays in the detection area.

Part Function
Conveyor belt Moves the object through the detection zone.
HC-SR04 ultrasonic sensor Measures distance and detects whether an object is present.
XIAO ESP32-C3 Reads the sensor, calculates cycle data, and connects to WiFi.
Generic custom PCB Provides organized pin access, power rails, and the voltage divider for the sensor echo signal.
Blynk Works as the application interface for real-time monitoring and reset control.
Assembled XIAO ESP32-C3 generic PCB
Custom generic board assembled with the XIAO ESP32-C3 and labeled pin headers.
XIAO ESP32-C3 wired to the test system
The board connected to the sensor and powered through USB during the dashboard test.

Board and Sensor Preparation

I used the generic board made in the previous input/output work as the electronic base for the interface assignment. The XIAO ESP32-C3 was soldered to the board and the HC-SR04 ultrasonic sensor was connected to the available pins. Since the HC-SR04 echo signal can return 5 V, I used a resistor voltage divider before connecting the echo line to the ESP32-C3 input pin.

HC-SR04 XIAO ESP32-C3 / PCB
VCC 5V
GND GND
TRIG D5
ECHO D6 through voltage divider
Milled generic PCB before assembly
Milled generic PCB used as the electronics interface board.
Arduino IDE and Blynk dashboard test
Testing the board while the Arduino IDE and Blynk dashboard were running.
Generic PCB with voltage divider design
PCB design with power rails, pin labels, and voltage divider area for sensor protection.

Detailed Interface Tool Comparison

Before choosing the final interface platform, I compared two possible tools for the conveyor monitoring system: MIT App Inventor and Blynk. Both tools can be used to create interfaces for embedded systems, but they solve the problem in different ways. The comparison focused on dashboard creation, real-time data visualization, connection with the XIAO ESP32-C3, development time, and suitability for the final project.

MIT App Inventor is a strong option for building mobile applications with visual blocks, especially when the goal is to create buttons, labels, screens, and phone-based interactions. Blynk, on the other hand, is focused on IoT interfaces and already includes dashboard tools such as datastreams, charts, gauges, labels, switches, and device templates. For this assignment, the interface needed to behave more like an industrial monitoring dashboard than a conventional mobile app.

Interface Evidence

The following images document the interface environment of each program. App Inventor was evaluated through its block-based logic workspace, while Blynk was evaluated through its dashboard console with native widgets.

MIT App Inventor block-based interface evidence
MIT App Inventor interface evidence: block-based programming logic for the mobile application.
Blynk dashboard interface evidence
Blynk interface evidence: native dashboard widgets for digital pins and real-time monitoring.
Criteria MIT App Inventor Blynk
Main purpose Mobile app creation using visual blocks and screen components. IoT dashboard creation for connected devices and real-time data.
Dashboard widgets Basic interface elements are available, but production-style dashboards usually require external services or custom logic. Native widgets are available for labels, charts, gauges, buttons, switches, and live status values.
Real-time data Possible, but it needs extra communication setup such as APIs, databases, MQTT, or other external tools. Real-time values are handled directly through datastreams and virtual pins.
Connection with ESP32 Requires additional communication design between the ESP32 and the app. The ESP32 can connect directly using the Blynk library and authentication token.
Charts and indicators Not native as an IoT dashboard workflow; chart visualization would need extra development. Charts, gauges, labels, and device status are native dashboard elements.
Development speed Fast for simple mobile screens, but slower for a complete cloud dashboard. Faster for this project because the dashboard structure already exists.
Limitations The interface would depend on external tools to store, process, or visualize dashboard data. It depends on the Blynk platform and requires careful protection of authentication credentials.
Fit for this assignment Good for learning app logic, but less direct for the required monitoring dashboard. Strong fit because the project needs real-time conveyor monitoring, charts, status labels, and reset control.

Why I Decided to Use Blynk

I decided to use Blynk because the assignment required an interface that could display live sensor values and process information without building a complete dashboard infrastructure from zero. In MIT App Inventor, I would have needed external tools to create the dashboard behavior, such as a database, web service, or communication layer for real-time visualization. Blynk already provides these dashboard functions natively.

Reason Impact on the Project
Native dashboard widgets I could create labels, charts, gauges, and reset controls directly inside the platform.
Direct ESP32 integration The XIAO ESP32-C3 can send values to the dashboard using the Blynk library and virtual pins.
Less external infrastructure I did not need to build or connect a separate database, broker, or custom web dashboard.
Better fit for final project monitoring The conveyor system needs distance, cycle count, cycle time, average time, process status, and reset control in one interface.

Blynk Setup Step by Step

Blynk was selected because it allows a fast IoT dashboard without building a complete web server from zero. The XIAO ESP32-C3 sends data to Blynk using virtual pins, and the web panel becomes the user interface.

  1. Create a new Blynk template named LEAN STATION using ESP32 and WiFi as the hardware configuration.
  2. Add a device from the template and copy the template ID, template name, and authentication token into the firmware.
  3. Create the datastreams used by the dashboard: cycle count, cycle time, average time, process status, cycles per minute, distance, and reset.
  4. Open the web dashboard editor and place the widgets: labels, gauges, chart, and reset button.
  5. Assign each widget to its corresponding virtual pin.
  6. Upload the firmware to the XIAO ESP32-C3 and verify that the device appears online in Blynk.
  7. Move an object through the conveyor sensor area and verify that the dashboard updates in real time.
Blynk device template and authentication token
Template and device page in Blynk for the LEAN STATION project.
Blynk datastreams configured for the dashboard
Datastreams created for the values sent from the XIAO ESP32-C3.
Blynk dashboard widget editor
Web panel editor with widgets for status, counters, cycle time, distance, chart, and reset.
Blynk chart configuration for cycle time
Chart configuration using the Cycle Time datastream to visualize the conveyor process.

Datastream Mapping

Each value from the firmware was mapped to a Blynk virtual pin. This structure made the interface easier to debug because each widget had a specific role in the monitoring system.

Virtual Pin Datastream Purpose
V0 Cycle Count Total number of objects detected by the station.
V1 Cycle Time Time that an object remains in the sensor detection area.
V2 Average Time Average cycle time calculated from the detected cycles.
V3 Process Status Text status: Object detected, Area free, or Data reset.
V4 Cycles Per Minute Production rate calculated over a one-minute window.
V5 Distance Real-time distance measured by the HC-SR04.
V6 Reset Button used to clear counters and timing values from the dashboard.

Firmware Logic

The firmware reads the ultrasonic sensor every 300 ms. When the measured distance is below the threshold, the system changes to Object detected and starts timing. When the object leaves the area, the program calculates the cycle time, updates the counter, sends the new values to Blynk, and changes the status to Area free.

#define BLYNK_TEMPLATE_ID "TMPL2_cwfk2-a"
#define BLYNK_TEMPLATE_NAME "LEAN STATION"
#define BLYNK_AUTH_TOKEN "PASTE_YOUR_BLYNK_AUTH_TOKEN_HERE"

#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

#define TRIG_PIN D5
#define ECHO_PIN D6

float detectionThreshold = 15.0;

void monitorProcess() {
  distanceCM = readDistance();
  Blynk.virtualWrite(V5, distanceCM);

  objectDetected = distanceCM <= detectionThreshold;

  if (objectDetected && !previousState) {
    startTime = millis();
    Blynk.virtualWrite(V3, "Object detected");
  }

  if (!objectDetected && previousState) {
    cycleTime = (millis() - startTime) / 1000.0;
    cycleCount++;
    totalTime += cycleTime;
    averageTime = totalTime / cycleCount;

    Blynk.virtualWrite(V0, cycleCount);
    Blynk.virtualWrite(V1, cycleTime);
    Blynk.virtualWrite(V2, averageTime);
    Blynk.virtualWrite(V3, "Area free");
  }

  previousState = objectDetected;
}

The public code file uses placeholders for the WiFi credentials and Blynk token. This avoids publishing private access information while keeping the logic reproducible.

Download Arduino IDE Code (.ino) Download Code as TXT

Code Explanation by Sections

The program is divided into small sections so the interface logic is easier to understand and debug. The same code uploaded to the XIAO ESP32-C3 is available in the downloads section in Arduino IDE format and TXT format.

1. Blynk and WiFi Configuration

This block defines the Blynk template ID, template name, and authentication token. In the public version, the token and WiFi credentials are replaced with placeholders, so each user must paste their own values before uploading.

#define BLYNK_TEMPLATE_ID "TMPL2_cwfk2-a"
#define BLYNK_TEMPLATE_NAME "LEAN STATION"
#define BLYNK_AUTH_TOKEN "PASTE_YOUR_BLYNK_AUTH_TOKEN_HERE"

char ssid[] = "YOUR_WIFI_SSID";
char pass[] = "YOUR_WIFI_PASSWORD";

2. Sensor Pins and Process Variables

The HC-SR04 trigger and echo pins are assigned to D5 and D6. The detection threshold defines the distance where an object is considered present in the conveyor belt sensing area.

#define TRIG_PIN D5
#define ECHO_PIN D6

float detectionThreshold = 15.0;
float distanceCM = 0;
bool objectDetected = false;
bool previousState = false;

3. Distance Reading

The function sends the ultrasonic pulse, measures the return time with pulseIn(), and converts the duration into centimeters. If no pulse is received, the function returns a large value to avoid a false detection.

float readDistance() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  long duration = pulseIn(ECHO_PIN, HIGH, 30000);
  if (duration == 0) return 999;

  return duration * 0.0343 / 2.0;
}

4. Object Detection and Cycle Measurement

The main monitoring function reads the distance, sends it to Blynk on V5, detects transitions between free area and object present, and calculates the cycle time when the object leaves the sensor area.

if (objectDetected == true && previousState == false) {
  startTime = millis();
  Blynk.virtualWrite(V3, "Object detected");
}

if (objectDetected == false && previousState == true) {
  cycleTime = (millis() - startTime) / 1000.0;
  cycleCount++;
  totalTime += cycleTime;
  averageTime = totalTime / cycleCount;

  Blynk.virtualWrite(V0, cycleCount);
  Blynk.virtualWrite(V1, cycleTime);
  Blynk.virtualWrite(V2, averageTime);
  Blynk.virtualWrite(V3, "Area free");
}

5. Reset Button from Blynk

The reset button uses virtual pin V6. When the button sends a value of 1, the counters and timing variables are cleared and the dashboard values are updated.

BLYNK_WRITE(V6) {
  if (param.asInt() == 1) {
    cycleCount = 0;
    cyclesPerMinute = 0;
    totalTime = 0;
    averageTime = 0;
    cycleTime = 0;
    Blynk.virtualWrite(V3, "Data reset");
  }
}

Dashboard Testing

After uploading the firmware, I verified the readings in the Arduino Serial Monitor and compared them with the Blynk dashboard. When the ultrasonic sensor detected an object on the conveyor belt, the status changed to Object detected, the distance gauge updated, and the cycle measurements were plotted in the chart.

Arduino IDE serial monitor and Blynk dashboard
Arduino IDE and Blynk running at the same time to compare serial readings with dashboard data.
Blynk dashboard showing object detected
Dashboard showing object detected, cycle time, average time, cycles per minute, and distance.
Blynk dashboard result with graph and distance
Dashboard result with chart data and real-time distance visualization.
Close dashboard view
Close view of the interface after detecting an object on the conveyor station.
Video test showing Arduino IDE data and the Blynk dashboard during the process.
Dashboard demo showing the interface response while values are updated.

Physical Test on the Conveyor

The final test connected the electronic interface to the conveyor belt. The ultrasonic sensor was positioned so it could detect an object passing over the belt. This connected the physical machine from the previous assignment with the application layer from this week.

Conveyor belt sensor test with object detection.
Hardware and dashboard test showing the interaction between the object and the interface.

Problems and Solutions

Problem 1: HC-SR04 Echo Voltage

The HC-SR04 sensor can output a 5 V echo signal, while the XIAO ESP32-C3 input pins work at 3.3 V logic. Connecting the echo pin directly could damage or stress the microcontroller input.

Solution

I used the generic PCB with a voltage divider so the echo signal is reduced before entering D6. This made the sensor safer to use with the ESP32-C3.

Problem 2: Unstable Object Detection

During testing, the ultrasonic distance changed quickly when the object was moving on the conveyor belt. This could create false readings or short detection events.

Solution

I used a detection threshold and detected only state transitions: when the object enters the area and when it leaves. This made the cycle count more reliable than counting every distance reading.

Problem 3: Dashboard Values Not Updating

At first, some Blynk widgets did not show the expected information because each widget must be connected to the correct virtual pin and datastream type.

Solution

I organized the virtual pins from V0 to V6 and assigned one function to each datastream: counter, cycle time, average time, process status, cycles per minute, distance, and reset.

Problem 4: Publishing Private Access Data

The working sketch contains a Blynk authentication token and WiFi credentials, which should not be published in a public Fab Academy page.

Solution

The downloadable files were prepared with placeholders for the private token and WiFi data. The logic remains the same, but the user must insert their own credentials before uploading.

Downloads

These files document the application code and the generic PCB used to connect the XIAO ESP32-C3 with the ultrasonic sensor. The code files are provided in Arduino IDE format and TXT format.

Generic PCB with voltage divider
Generic PCB image with voltage divider for the HC-SR04 echo signal.

Application Code

Download Arduino IDE Code (.ino) Download Code as TXT

PCB Files

Download PCB Image (PNG) Download KiCad PCB Drawing (SVG)

Results and Reflection

The interface successfully connected the user with the conveyor belt sensing system. Blynk made it possible to see the process status, distance, cycle count, cycle time, average time, and cycles per minute from a web dashboard. The reset button also allowed the user to clear the measurements without reprogramming the board.

The most important learning was that the interface is part of the machine logic, not only a screen. The dashboard helped me understand the behavior of the sensor and made the conveyor process easier to evaluate. This same workflow can be expanded for the final project by adding more sensors, alerts, and control actions.