Integrating the HC-SR04 ultrasonic sensor with an Arduino UNO allows for precise distance measurements, making it invaluable for projects like obstacle-avoiding robots and distance monitoring systems. This step-by-step guide will walk you through the process of setting up and programming the HC-SR04 sensor with your Arduino.
![]() |
HC-SR04 ultrasonic sensor with an Arduino UNO |
Understanding the HC-SR04 Ultrasonic Sensor
The HC-SR04 sensor determines distance by emitting ultrasonic waves at 40 kHz through its transmitter. When these waves encounter an object, they reflect back to the sensor's receiver. By calculating the time taken for the echo to return, the sensor can determine the distance to the object using the formula.
![]() |
Dimension and specification of HC-SR04 ultrasonic sensor |
Step-by-Step Instructions
Step 1: Components Required
To test the HC-SR04 ultrasonic sensor with an Arduino, you’ll need:
![]() |
Components required for this project |
✅ Arduino UNO board
✅ HC-SR04 ultrasonic sensor
✅ Jumper Wires
✅ Breadboard
✅ USB cable for Arduino
Step 2: Wiring the HC-SR04 to the Arduino UNO
Here’s how to connect the HC-SR04 ultrasonic sensor to your Arduino:
![]() |
Connection between HC-SR04 to the Arduino UNO |
✅VCC to 5V: Connect the VCC pin of the HC-SR04 to the 5V pin on the Arduino.
✅GND to GND: Connect the GND pin of the sensor to the GND pin on the Arduino.
✅Trig to Digital Pin 9: Connect the Trig pin to Digital Pin 9 on the Arduino.
✅Echo to Digital Pin 10: Connect the Echo pin to Digital Pin 10 on the Arduino.
Step 3: Programming the Arduino
Now, Open the Arduino IDE and input the following code to measure distances using the HC-SR04 sensor or you can download the code HERE.
#define TRIG_PIN 9 #define ECHO_PIN 10 void setup() { Serial.begin(9600); pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); } void loop() { digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); long duration = pulseIn(ECHO_PIN, HIGH); float distance_cm = duration * 0.034 / 2; Serial.print("Distance: "); Serial.print(distance_cm); Serial.println(" cm"); delay(500); }
![]() |
Open Arduino IDE and copy-paste the code |
Step 4: Expected Output
After successfully uploading the code to your Arduino UNO, open the Serial Monitor to observe real-time distance readings in centimetres. To assess the sensor's accuracy, position objects at various distances from the HC-SR04 sensor and monitor the corresponding measurements displayed. This hands-on approach allows you to evaluate the sensor's performance across different ranges.
By following this guide, you've successfully integrated the HC-SR04 ultrasonic sensor with the Arduino UNO to measure distances. This foundational setup can be expanded for applications like obstacle avoidance in robotics, liquid-level detection, and more. Experiment with different objects and environments to fully explore the sensor's capabilities.
Tools and components
Shopee:
Lazada:
Aliexpress:
Video
🔔 If you found this guide helpful, share it with fellow makers! For more tutorials, subscribe to our YouTube channel and follow us on social media. 🚀
No comments:
Post a Comment