WEEK 09 — Input Devices

Testing HC-SR04 with XIAO ESP32-C3 using Multimeter and Oscilloscope06

Group Assignment – Input Devices Analysis

Testing HC-SR04 with XIAO ESP32-C3 using Multimeter and Oscilloscope

Objective

The objective of this assignment is to analyze an input device by probing both analog levels and digital signals using a multimeter and an oscilloscope.

Components Used

  • HC-SR04 Ultrasonic Sensor
  • XIAO ESP32-C3
  • Breadboard and jumper wires
  • Digital Multimeter
  • Oscilloscope
Image 1

🔄 Workflow – Testing HC-SR04 with XIAO ESP32-C3

Step-by-step process to probe analog and digital signals using a multimeter and an oscilloscope.

1️⃣ Component Preparation

Gather all required components: HC-SR04 sensor, XIAO ESP32-C3, breadboard, jumper wires, multimeter, and oscilloscope.

2️⃣ Circuit Assembly

Connect the sensor to the microcontroller:

  • VCC → 5V / 3.3V
  • GND → GND
  • TRIG → Digital Output Pin
  • ECHO → Digital Input Pin

Important: Use a voltage divider if required for the ECHO pin.

Image 1 Image 1

3️⃣ Code Upload

Upload a basic program to the XIAO ESP32-C3 to generate the TRIG pulse and read the ECHO signal.

💻 Final Code – HC-SR04 with XIAO ESP32-C3

/**
 * FabAcademy - Input Devices
 * Final Code: Ultrasonic Distance Measurement
 */

// Pin Configuration
const int TRIG_PIN = 4; // XIAO Pin D2
const int ECHO_PIN = 5; // XIAO Pin D3

void setup() {
  // Serial communication for data visualization
  Serial.begin(115200);
  
  // Initialize Pins
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  
  Serial.println("System Online: HC-SR04 + ESP32-C3");
}

void loop() {
  long duration;
  float distanceCm;

  // Clear trigger to ensure a clean HIGH pulse
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);

  // Send 10 microsecond pulse
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // Measure the duration of the Echo pulse in microseconds
  // timeout set to 30000us (approx 5 meters)
  duration = pulseIn(ECHO_PIN, HIGH, 30000);

  // Physics: Distance = (Time * Speed of Sound) / 2
  // Speed of Sound = 0.0343 cm/us
  distanceCm = (duration * 0.0343) / 2;

  // Data Output
  if (duration == 0 || distanceCm > 400) {
    Serial.println("Status: Out of range / No echo");
  } else {
    Serial.print("Distance: ");
    Serial.print(distanceCm);
    Serial.println(" cm");
  }

  // Sampling rate control (10 readings per second)
  delay(100);
}

4️⃣ Multimeter Setup

Set the multimeter to measure DC voltage. Verify:

  • Power supply voltage
  • TRIG idle state
  • Basic signal presence

5️⃣ Multimeter Measurements

Measure voltage levels at different pins and confirm proper electrical connections.

Note that rapid signals may not be accurately displayed.

6️⃣ Oscilloscope Setup

Connect oscilloscope probes:

  • Channel 1 → TRIG pin
  • Channel 2 → ECHO pin
  • GND → Common ground

7️⃣ Signal Observation

Observe the signals:

  • TRIG: short square pulse (~10 µs)
  • ECHO: variable pulse width depending on distance

8️⃣ Data Analysis

Measure the duration of the ECHO pulse using the oscilloscope and relate it to distance.

Circuit Connections

HC-SR04 Pin
VCC 5V / 3.3V
GND GND
TRIG D2 (Output)
ECHO D3 (Input)

Note: Use a voltage divider for the ECHO pin if necessary.

Image 1 Image 1

Working Principle

  1. Send a 10 µs pulse to TRIG
  2. Ultrasonic wave is emitted
  3. Signal reflects from object
  4. ECHO outputs HIGH pulse proportional to distance

Measurements with Multimeter

  • VCC → GND: ~5V / 3.3V
  • TRIG idle: ~0V
  • ECHO: fluctuating voltage

The multimeter is useful for static measurements but cannot accurately detect fast signals.

Measurements with Oscilloscope

  • TRIG: Square pulse (~10 µs)
  • ECHO: Variable pulse width depending on distance

The oscilloscope allows real-time visualization of signals and precise timing analysis.

Distance Calculation

d = (t × v) / 2

Where:
d = distance
t = time of pulse
v = speed of sound (~343 m/s)

Results

  • Stable TRIG digital signal observed
  • ECHO pulse width varies with distance
  • Accurate timing only visible with oscilloscope

Comparison

Tool Advantage Limitation
Multimeter Easy voltage measurement Cannot detect fast signals
Oscilloscope Real-time waveform analysis Requires setup

What We Learned

  • Input devices can output digital signals with time-based data
  • HC-SR04 works using pulse duration
  • Multimeter has limitations with dynamic signals
  • Oscilloscope is essential for signal analysis

Reflection

This assignment helped me understand how input devices communicate using real signals. By using the oscilloscope, I could visualize how distance is encoded in pulse duration. I also learned the limitations of the multimeter and the importance of proper signal analysis.