Testing HC-SR04 with XIAO ESP32-C3 using Multimeter and Oscilloscope06
Testing HC-SR04 with XIAO ESP32-C3 using Multimeter and Oscilloscope
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.
Step-by-step process to probe analog and digital signals using a multimeter and an oscilloscope.
Gather all required components: HC-SR04 sensor, XIAO ESP32-C3, breadboard, jumper wires, multimeter, and oscilloscope.
Connect the sensor to the microcontroller:
Important: Use a voltage divider if required for the ECHO pin.
Upload a basic program to the XIAO ESP32-C3 to generate the TRIG pulse and read the ECHO signal.
/** * 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); }
Set the multimeter to measure DC voltage. Verify:
Measure voltage levels at different pins and confirm proper electrical connections.
Note that rapid signals may not be accurately displayed.
Connect oscilloscope probes:
Observe the signals:
Measure the duration of the ECHO pulse using the oscilloscope and relate it to distance.
| 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.
The multimeter is useful for static measurements but cannot accurately detect fast signals.
The oscilloscope allows real-time visualization of signals and precise timing analysis.
d = (t × v) / 2
Where:
d = distance
t = time of pulse
v = speed of sound (~343 m/s)
| Tool | Advantage | Limitation |
|---|---|---|
| Multimeter | Easy voltage measurement | Cannot detect fast signals |
| Oscilloscope | Real-time waveform analysis | Requires setup |
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.