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

How to Use HC-SR04 Ultrasonic Sensor: Examples, Pinouts, and Specs

Image of HC-SR04 Ultrasonic SensorDesign with HC-SR04 Ultrasonic Sensor in Cirkit Designer

Introduction

The HC-SR04 Ultrasonic Sensor is a popular and cost-effective solution for non-contact distance measurements. It operates by emitting ultrasonic waves from its transmitter, which then reflect off objects and are captured by its receiver. The time interval between emission and reception of the waves is used to calculate the distance to the object. This sensor is widely used in robotics, obstacle avoidance systems, and various automation projects.

Explore Projects Built with HC-SR04 Ultrasonic Sensor

Use Cirkit Designer to design, explore, and prototype these projects online. Some projects support real-time simulation. Click "Open Project" to start designing instantly!
Image of Arduino UNO Based Ultrasonic Distance Measurement with HC-SR04 and Bluetooth Communication via HC-05: A project utilizing HC-SR04 Ultrasonic Sensor in a practical application
Arduino UNO Based Ultrasonic Distance Measurement with HC-SR04 and Bluetooth Communication via HC-05

This circuit features an Arduino UNO microcontroller interfaced with an HC-SR04 Ultrasonic Sensor and an HC-05 Bluetooth module. The Arduino is configured to trigger the ultrasonic sensor to measure distance and communicate the data wirelessly via the HC-05 module. Power is supplied to both the sensor and the Bluetooth module from the Arduino's 5V output, and ground connections are shared among all components.

Open Project in Cirkit Designer
Image of Arduino Mega 2560 Bluetooth-Controlled Ultrasonic Distance Measurement: A project utilizing HC-SR04 Ultrasonic Sensor in a practical application
Arduino Mega 2560 Bluetooth-Controlled Ultrasonic Distance Measurement

This circuit features an Arduino Mega 2560 microcontroller interfaced with an HC-05 Bluetooth Module and an HC-SR04 Ultrasonic Sensor. The HC-05 is powered by the Arduino's VIN pin and is grounded to the Arduino's GND, enabling wireless communication capabilities. The HC-SR04 is powered by the Arduino's 5V output and uses two digital PWM pins (D7 for TRIG and D6 for ECHO) to measure distances via ultrasonic waves.

Open Project in Cirkit Designer

Common Applications and Use Cases

Technical Specifications

Key Technical Details

Pin Configuration and Descriptions

Pin Number Pin Name Description
1 VCC Power supply (5V DC)
2 Trig Trigger input (10µs pulse)
3 Echo Echo output (pulse width represents distance)
4 GND Ground

Usage Instructions

How to Use the HC-SR04 in a Circuit

  1. Connect the VCC pin to a 5V power supply.
  2. Connect the GND pin to the ground of the power supply.
  3. Connect the Trig pin to a digital I/O pin on your microcontroller.
  4. Connect the Echo pin to another digital I/O pin on your microcontroller.

Important Considerations and Best Practices

Example Code for Arduino UNO

// Define the Trig and Echo pins
const int trigPin = 9;
const int echoPin = 10;

// Define variables for duration and distance
long duration;
int distance;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
  // Define pin modes
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  // Clear the trigPin by setting it LOW
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

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

  // Read the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

  // Calculate the distance
  distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)

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

  // Delay 50ms before the next measurement
  delay(50);
}

Troubleshooting and FAQs

Common Issues

Solutions and Tips for Troubleshooting

FAQs

Q: Can the HC-SR04 sensor measure distances beyond 4 meters? A: No, the maximum reliable range for the HC-SR04 is 4 meters. Beyond this range, the readings may not be accurate.

Q: Is the HC-SR04 waterproof? A: No, the standard HC-SR04 is not waterproof. Specialized versions or enclosures are needed for waterproofing.

Q: Can I use the HC-SR04 with a 3.3V microcontroller? A: While the sensor is designed for 5V operation, some users have successfully interfaced it with 3.3V systems. However, level shifting may be required for reliable operation.

Q: How can I increase the measurement angle of the sensor? A: The measurement angle is fixed due to the design of the sensor. To cover a wider area, you may need to use multiple sensors or mechanically rotate a single sensor.