How to use the HC-SR04 Ultrasonic Sensor

Introduction

In this guide, I’ll show you how to use the HC-SR04 ultrasonic sensor to measure distances and detect objects using an Arduino or another microcontroller.

Video

I created a detailed video demonstrating everything I explain on this page. It's very informative and I spent alot of time on it - so you should totally watch it. :)

What is the HC-SR04 Ultrasonic Sensor?

The HC-SR04 ultrasonic sensor is a compact module designed for measuring distances between 2 cm and 400 cm. It works by emitting ultrasonic pulses and measuring the time taken for the echo to return.

Physical Overview

  • Dimensions:~21 mm (H) × 45 mm (W) × 15 mm (D) (excluding pins)
  • Front: Houses the transmitter, receiver, and ceramic resonator
  • Back: Contains the microcontroller that generates ultrasonic pulses, processes echoes, and outputs the results
  • Components:
    • Ultrasonic transceiver IC: Drives the transmitter and processes received signals
    • Voltage regulator IC: Ensures stable voltage for reliable operation

How It Works

The HC-SR04 emits eight short ultrasonic pulses at 40 kHz. These pulses bounce off nearby objects and return to the sensor. By measuring the time delay between transmission and reception, the sensor calculates the distance to the object.

Pin Configuration

We'll go over the pins in more detail later, but very breifly you'll find the following:

  • VCC → 5V power supply
  • GND → Ground
  • TRIG → Used to trigger the ultrasonic pulse
  • ECHO → Outputs the measured distance as a signal

Wiring for Demo

Setting Up the Sensor with an Arduino

Let’s connect the HC-SR04 to an Arduino Uno:

  1. Connect VCC to the 5V pin on the Arduino.
  2. Connect GND to GND.
  3. Connect TRIG to digital pin 9.
  4. Connect ECHO to digital pin 10.

Uploading the Code

Download the code and paste it into the Arduino IDE. Select your microcontroller board and upload the sketch.


Comments:


By Lewis February 24, 2026
In this video, I’ll show you how to set up sensorless homing using a TMC2209 stepper driver — no mechanical limit switches required! Perfect for 3D printers, CNC machines, robotics, and other DIY electronics projects. I’ll guide you through the wiring, Arduino code, and how to tune StallGuard for accurate, reliable homing. Whether you’re building a CoreXY, a custom motion control system, or just want to learn more about stepper motor drivers, this is a great place to start. Let’s build smarter! Why would we want to use sensorless homing? Well first the Pros…. There’s no need for extra mounts, additional components or running long dangling cables to one or both ends of the carriage or other moving component. And there are always cons, so in this case you’ll need a stallguard-capable driver (they cost a little more and are only manufactured by Trinamic) and accuracy isn’t great at slow speeds.
By Lewis February 1, 2026
I Recreated the Stranger Things Walkie-Talkies (THEY ACTUALLY WORK)
By Lewis December 17, 2025
How to build your own colour e-ink paper display smart dashboard. Powered by Raspberry Pi Zero 2 W you can show weather, photos, calendars, GitHub stats and more.
A resin 3D printer with an orange cover and a screen, beside a bottle of resin and a black base.
By Lewis November 28, 2025
PioCreat HALOT-X1 10.1” 16K Resin Printer Review
By Lewis November 7, 2024
Elegoo Mars 5 Ultra - 7" 9k AI Resin Printer
By Lewis June 27, 2024
3D Print and add some Resin to create some amazing pieces of stained 'glass'.
By Lewis Aburrow May 1, 2024
My Patreons sent in some questions. Because I'd asked them to do this - I thought I'd better write out answers....
By Lewis Aburrow May 1, 2024
CRAZY COASTERS with a TWIST! Laser or 3D Print some DIY Magic.
By Lewis April 2, 2024
KINETIC COASTERS with a TWIST! Laser or 3D Printable
By Lewis January 20, 2024
How To Make Neon LED Signs | Low Cost with Alexa, Hue & WiFi Controls