Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO Controlled HC-SR04 Ultrasonic Distance Measurement

View and Edit Project In Cirkit DesignerImage of Arduino UNO Controlled HC-SR04 Ultrasonic Distance Measurement

Circuit Documentation

Summary of the Circuit

This circuit integrates an HC-SR04 Ultrasonic Sensor with an Arduino UNO microcontroller. The HC-SR04 sensor is used to measure distance through ultrasonic waves, and the Arduino UNO serves as the control unit to process the sensor data and execute the embedded code. The sensor is powered by the Arduino and communicates with it through digital input/output pins.

Component List

HC-SR04 Ultrasonic Sensor

Arduino UNO

Wiring Details

HC-SR04 Ultrasonic Sensor

Arduino UNO

Documented Code

void setup() {
  // Initialize the sensor's trigger pin as an output:
  pinMode(5, OUTPUT);
  // Initialize the sensor's echo pin as an input:
  pinMode(6, INPUT);
}

void loop() {
  // Variables for the duration and the distance:
  long duration, distance;
  
  // Clear the trigger pin by setting it to LOW:
  digitalWrite(5, LOW);
  delayMicroseconds(2);
  
  // Trigger the sensor by setting the trigger pin high for 10 microseconds:
  digitalWrite(5, HIGH);
  delayMicroseconds(10);
  digitalWrite(5, LOW);
  
  // Read the echo pin; duration will be the time (in microseconds) for the echo to return:
  duration = pulseIn(6, HIGH);
  
  // Calculate the distance (in cm) based on the speed of sound (340 m/s):
  distance = duration * 0.034 / 2;
  
  // Print the distance to the Serial Monitor:
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  
  // Delay for a short period to avoid overlapping measurements:
  delay(100);
}

The provided code initializes the HC-SR04 sensor and continuously measures the distance by sending ultrasonic pulses and calculating the time it takes for the echo to return. The distance is then printed to the Serial Monitor.