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

Arduino UNO Based Ultrasonic Distance Measurement System

Image of Arduino UNO Based Ultrasonic Distance Measurement System

Circuit Documentation

Summary of the Circuit

This circuit integrates an HC-SR04 Ultrasonic Sensor with an Arduino UNO to measure distances. The HC-SR04 sensor emits an ultrasonic pulse and receives the echo. The time interval between sending and receiving the pulse is used to calculate the distance to an object. The Arduino UNO is programmed to control the sensor, perform the time-to-distance conversion, and output the distance measurements to the serial monitor.

Component List

HC-SR04 Ultrasonic Sensor

  • Description: A sensor that measures distance by emitting and receiving ultrasonic waves.
  • Pins: VCC, TRIG, ECHO, GND

Arduino UNO

  • Description: A microcontroller board based on the ATmega328P, widely used for building digital devices and interactive objects.
  • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13

Comment

  • Description: A placeholder or annotation within the circuit design, typically not a physical component.

Wiring Details

HC-SR04 Ultrasonic Sensor

  • VCC: Connected to the 5V output on the Arduino UNO.
  • TRIG: Connected to digital pin D3 on the Arduino UNO.
  • ECHO: Connected to digital pin D2 on the Arduino UNO.
  • GND: Connected to one of the GND pins on the Arduino UNO.

Arduino UNO

  • 5V: Provides power to the HC-SR04 Ultrasonic Sensor.
  • D3: Configured as an output to trigger the HC-SR04 sensor.
  • D2: Configured as an input to read the echo from the HC-SR04 sensor.
  • GND: Common ground with the HC-SR04 Ultrasonic Sensor.

Documented Code

/**
 * This example demonstrates how to collect distance measurements from an HC-SR04
 * ultrasonic distance sensor connected to an Arduino UNO, and prints the distance
 * measurements to the serial monitor.
 *
 * This example was written by Cirkit Design LLC and adapted from Maker Guides.
 */

// Define Trig and Echo pins.
const int TRIG_PIN = 3;
const int ECHO_PIN = 2;

void setup() {
  // Define inputs and outputs to HC-SR04 sensor.
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  Serial.begin(9600);
}

void loop() {
  int distanceCentimeters = getDistanceCentimeters();

  // Print the distance on the Serial Monitor.
  Serial.print("Distance = ");
  Serial.print(distanceCentimeters);
  Serial.println(" cm");

  delay(50);
}

int getDistanceCentimeters() {
  // Clear the trigPin by setting it LOW.
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(5);

  // Trigger the sensor by setting the trigPin high for 10 microseconds.
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds.
  long durationMicroseconds = pulseIn(ECHO_PIN, HIGH);
  // Calculate the distance in centimeters. Note that at 20 degrees Celcius, the speed of sound
  // is roughly 0.034 cm / us.
  int distanceCentimeters = durationMicroseconds * 0.034 / 2;

  return distanceCentimeters;
}

The code is written in Arduino sketch format (.ino) and is designed to be uploaded to the Arduino UNO. It initializes the sensor, triggers the ultrasonic pulse, measures the time taken for the echo to return, and calculates the distance in centimeters. The distance is then printed to the serial monitor every 50 milliseconds.