← UNNC-FabLab · Group assignments

UNIT 09 · FAB ACADEMY 2026

Group Work 2026 - Measure an Input Device

Assignment: Measure an input device's analog levels and digital signals.

Input Devices

Input devices allow electronic systems to receive information from their surroundings. In microcontroller projects, sensors convert physical phenomena, such as distance, temperature, or light, into electrical signals that can be processed by a controller. Understanding these signals is important when developing and troubleshooting embedded systems.

Introduction to Sensors

Sensors can be classified by the type of electrical output they provide:

In this experiment, we used an HC-SR04 ultrasonic distance sensor connected to a Seeed Studio XIAO ESP32-S3. The sensor provides a digital pulse whose width represents the measured distance.

Testing an Ultrasonic Sensor Input Using XIAO ESP32-S3 (Oscilloscope)

The HC-SR04 uses approximately 40 kHz ultrasonic sound to detect nearby objects. After receiving a short trigger pulse, it emits an ultrasonic burst and sets its Echo output HIGH for a duration corresponding to the sound's round-trip travel time. The distance can be calculated using:

Distance (cm) = Echo duration (µs) × 0.0343 ÷ 2

The division by two accounts for the sound travelling from the sensor to the object and returning to the sensor. The nominal measurement range is approximately 2–400 cm.

Components and Equipment

Circuit Connection

We connected the ultrasonic sensor to the XIAO board using the pin assignments from our 2026 program:

HC-SR04 pin Connection Purpose
VCC 5 V supply Sensor power
GND Common GND Shared reference
Trig XIAO D3 Trigger output
Echo XIAO D6 through a 5 V-to-3.3 V level shifter or voltage divider Digital input

Important: The XIAO ESP32-S3 uses 3.3 V GPIO and must not receive the HC-SR04's 5 V Echo signal directly. The sensor and microcontroller must share a common ground. The D6 pin is also associated with UART0 TX, so USB CDC on boot is enabled when using the original 2026 program.

Arduino Code for the Input Test

The following standalone test reproduces the ultrasonic measurement logic of our 2026 XIAO project, without the unrelated camera and LED functions. It sends a 12 µs trigger pulse, measures the Echo pulse duration, and prints the calculated distance to the Serial Monitor.

#include <Arduino.h>

constexpr uint8_t PIN_TRIG = D3;
constexpr uint8_t PIN_ECHO = D6;
constexpr uint32_t ECHO_TIMEOUT_US = 30000;

void setup() {
  Serial.begin(115200);
  pinMode(PIN_TRIG, OUTPUT);
  digitalWrite(PIN_TRIG, LOW);
  pinMode(PIN_ECHO, INPUT_PULLDOWN);
}

void loop() {
  digitalWrite(PIN_TRIG, LOW);
  delayMicroseconds(5);
  digitalWrite(PIN_TRIG, HIGH);
  delayMicroseconds(12);
  digitalWrite(PIN_TRIG, LOW);

  uint32_t echoUs = pulseIn(PIN_ECHO, HIGH, ECHO_TIMEOUT_US);

  if (echoUs > 0) {
    float distanceCm = echoUs * 0.0343f / 2.0f;
    if (distanceCm >= 2.0f && distanceCm <= 400.0f) {
      Serial.printf("Echo: %lu us, Distance: %.2f cm\n",
                    (unsigned long)echoUs, distanceCm);
    } else {
      Serial.println("Distance outside measurement range");
    }
  } else {
    Serial.println("No valid echo detected");
  }

  delay(300);
}

Oscilloscope Measurement

To examine the input signal, we used the RIGOL DS2102A oscilloscope to observe the trigger and Echo waveforms:

  1. Connect the oscilloscope probe ground clips to the circuit's common GND.
  2. Connect Channel 1 to the XIAO D3 / Trig signal.
  3. Connect Channel 2 to the level-shifted D6 / Echo signal, or to the sensor-side Echo signal when using a probe and voltage setting suitable for 5 V logic.
  4. Set the channels to DC coupling and check that the probe attenuation setting matches the probes used.
  5. Start with a vertical scale around 1 V/div for the 3.3 V signals. Use a microsecond-scale timebase to inspect the trigger pulse and a millisecond-scale timebase to inspect the Echo pulse.
  6. Set the trigger source to Channel 1 with a rising-edge trigger. Adjust the horizontal scale to capture both signals.
  7. Move an object closer to or farther from the ultrasonic sensor and compare the Echo pulse widths.
Input Devices experiment documentation

The measurement video below shows how the Echo pulse duration changes as a hand moves in front of the sensor.