What is an Ultrasonic Sensor?
The HC-SR04 ultrasonic sensor is a popular distance sensor commonly used with Arduino and other microcontroller platforms. It’s used to measure distances between the sensor and an object by emitting ultrasonic waves and calculating the time it takes for the waves to bounce back.
Here’s a step-by-step guide on how to use an HC-SR04 sensor with Arduino:
Components you’ll need:
- Arduino.
- HC-SR04 ultrasonic sensor.
- Jumper wires.
- Breadboard.
Connections:
- Connect the VCC pin of the HC-SR04 to the 5V output on your Arduino.
- Connect the GND (ground) pin of the HC-SR04 to the GND (ground) on your Arduino.
- Connect the TRIG (trigger) pin of the HC-SR04 to a digital pin on your Arduino (e.g., D12).
- Connect the ECHO pin of the HC-SR04 to another digital pin on your Arduino (e.g., D11).

The Code:
Here’s an example Arduino sketch to read distance measurements using the HC-SR04 sensor:
// Define the pins for the trigger and echo const int trigPin = 12; const int echoPin = 11; void setup() { // Initialize serial communication for debugging Serial.begin(9600); // Define the trigger pin as an OUTPUT pinMode(trigPin, OUTPUT); // Define the echo pin as an INPUT pinMode(echoPin, INPUT); } void loop() { // Trigger a pulse by setting the trigger pin HIGH for 10 microseconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Measure the duration of the pulse on the echo pin long duration = pulseIn(echoPin, HIGH); // Convert the duration into distance in centimeters // (speed of sound is approximately 343 m/s) float distanceCM = (duration / 2.0) * 0.0343; // Print the distance to the Serial Monitor Serial.print("Distance: "); Serial.print(distanceCM); Serial.println(" cm"); // Wait a moment before taking the next measurement delay(1000); }
In this code:
- We define the trigger and echo pins and set them as OUTPUT and INPUT, respectively.
- In the
loop()
function, we trigger the sensor by setting the trigger pin HIGH for 10 microseconds and then LOW. - We measure the duration of the pulse on the echo pin using
pulseIn()
. - We convert the duration into distance in centimeters using the speed of sound (approximately 343 meters per second).
- Finally, we print the distance to the Serial Monitor and wait for a second before taking the next measurement.
Upload this sketch to your Arduino, open the Serial Monitor, and you should see distance measurements displayed in centimeters.
Conclusion
The HC-SR04 ultrasonic sensor is a versatile and cost-effective tool for measuring distances with precision when interfaced with an Arduino. The combination of the HC-SR04 and Arduino opens up a world of possibilities for innovation and automation in the realm of electronics and robotics.
Happy Tinkering!
Recommendations:
The Elegoo Super Starter Kit
If you don’t already own any Arduino hardware, we highly recommend this kit as it has everything you need to start programming with Arduino. You can find out more about this kit, including a list of its components here: Elegoo Super Starter Kit
You can find this kit on Amazon here: Elegoo Super Starter Kit
The 0.96-inch Mini-OLED Display
We highly recommend this mini-OLED bundle of five 0.96-inch OLED displays. We have bought these before and they all worked perfectly. You can read more about the mini-OLED here: Mini-OLED
You can find this bundle on Amazon here: OLED Displays
Elegoo Nano (Arduino Compatible)
We have bought these Nano boards many times and can highly recommend them. There are three Nano boards in this pack making them a total bargain for everyone.
You can find this pack on Amazon here: Arduino Nano