Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino UNO Controlled HC-SR04 Ultrasonic Distance Measurement

Image 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

  • Pins: VCC, TRIG, ECHO, GND
  • Description: An ultrasonic distance sensor that can measure distances by emitting ultrasonic waves and measuring the time it takes for the echo to return.
  • Purpose: To measure distance in the circuit.

Arduino UNO

  • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13
  • Description: A microcontroller board based on the ATmega328P, widely used for building digital devices and interactive objects that can sense and control objects in the physical and digital world.
  • Purpose: To control the HC-SR04 sensor and process the distance measurements.

Wiring Details

HC-SR04 Ultrasonic Sensor

  • VCC: Connected to the 5V pin on the Arduino UNO for power supply.
  • TRIG: Connected to digital pin D5 on the Arduino UNO to trigger the ultrasonic pulse.
  • ECHO: Connected to digital pin D6 on the Arduino UNO to receive the echo signal.
  • GND: Connected to one of the GND pins on the Arduino UNO to complete the circuit.

Arduino UNO

  • 5V: Provides power to the HC-SR04 sensor.
  • D5: Sends trigger signal to the HC-SR04 sensor.
  • D6: Receives echo signal from the HC-SR04 sensor.
  • GND: Common ground with the HC-SR04 sensor.

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.