HC-SR04 Ultrasonic Sensor + Arduino UNO: a Tutorial

by AustinS89 in Circuits > Arduino

301 Views, 0 Favorites, 0 Comments

HC-SR04 Ultrasonic Sensor + Arduino UNO: a Tutorial

capture - 2024-07-29T125949.021.png

The HC-SR04 ultrasonic sensor is a popular choice for distance measurement and proximity detection. In this guide, I'll show you how to wire it up to an Arduino UNO and write code to read distance measurements.

Before we dive in, I designed this circuit and wrote the code using Cirkit Designer, a web-based electronics design tool.

You can view and edit this circuit in Cirkit Designer here: https://app.cirkitdesigner.com/project/0dd59d0d-f4d1-4646-8630-167baa0fe1b0

Supplies

For this project, you'll need:

  • HC-SR04 Ultrasonic Sensor
  • Arduino UNO
  • Breadboard and Jumper Wires

Wiring

capture - 2024-07-29T125949.021.png

Here's how to connect the HC-SR04 sensor to the Arduino UNO:

  1. Connect the 5V pin on the Arduino to the VCC pin on the HC-SR04 sensor.
  2. Connect the GND pin on the Arduino to the GND pin on the HC-SR04 sensor.
  3. Connect digital pin 3 on the Arduino to the TRIG pin on the HC-SR04 sensor.
  4. Connect digital pin 2 on the Arduino to the ECHO pin on the HC-SR04 sensor.

Ensure your Arduino UNO is connected to your computer via USB to upload the code.

Code

Now, let's upload the code to the Arduino to read distance measurements:

  1. Open this project in the Cirkit Designer Web Editor: https://app.cirkitdesigner.com/project/0dd59d0d-f4d1-4646-8630-167baa0fe1b0
  2. Navigate to the Code tab in the Cirkit Designer Editor.
  3. Click Verify to ensure that the code compiles without errors.
  4. Click Upload to load the code onto your Arduino UNO.
  5. Open the Serial Monitor (the button is located in the top-right of the screen).
  6. Set the baud rate to 9600 to view the distance measurements.

The code provided initializes the HC-SR04 sensor and reads the distance measurements, which are then printed to the Serial Monitor.

Here is the full 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;
}

Conclusion

After completing these steps, you'll have a functional setup that can measure distances using the HC-SR04 sensor and display the results on the Serial Monitor. This project is an excellent foundation for any Arduino-based project that requires distance sensing.